Skip to content

Commit cb5b23b

Browse files
committed
published
0 parents  commit cb5b23b

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Image Generator using OpenAI API
2+
3+
This is a simple image generator using [OpenAI API](https://openai.com/api/). you can generate images by entering short description of the image or by entering a keyword.
4+
5+
## Demo
6+
7+
### [https://sauravhathi.github.io/image-generator-using-openai-api/](https://sauravhathi.github.io/image-generator-using-openai-api/)
8+
9+
## How to use
10+
11+
- type a short description of the image or a keyword
12+
- click on the generate button and wait for the images to load
13+
- you will get 3 images generated by the API
14+
15+
## API Reference
16+
17+
[OpenAI API](https://openai.com/api/)
18+
19+
## Run Locally
20+
21+
Clone the project
22+
23+
```bash
24+
git clone https://github.com/sauravhathi/image-generator-using-openai-api.git
25+
```
26+
27+
Go to the project directory
28+
29+
```bash
30+
cd image-generator-openai
31+
```
32+
33+
Open `index.html` in your browser
34+
35+
## Authors
36+
37+
- [@sauravhathi](https://www.github.com/sauravhathi)
38+
39+
## License
40+
41+
[MIT](https://choosealicense.com/licenses/mit/)

index.html

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<script src="https://cdn.tailwindcss.com"></script>
8+
<title>Image Gererator using OpenAI API</title>
9+
<style>
10+
.min-height {
11+
min-height: 100vh;
12+
}
13+
</style>
14+
</head>
15+
<body class="max-w-4xl mx-auto">
16+
<div
17+
class="min-height flex flex-col justify-center px-4 py-12 sm:px-6 lg:px-8 -mt-20"
18+
>
19+
<div class="text-center">
20+
<h1 class="text-4xl font-bold mb-10">Image Generator</h1>
21+
<p class="text-md lg:text-xl mb-10">
22+
This is a simple image generator using
23+
<a href="https://openai.com/api/" class="font-semibold"
24+
>OpenAI API.</a
25+
>
26+
you can generate images by entering short description of the image or
27+
by entering a keyword.
28+
</p>
29+
</div>
30+
<div class="flex flex-row justify-center">
31+
<input
32+
type="text"
33+
id="text"
34+
class="shadow-lg outline-none w-full rounded-l-lg px-4 py-4 text-gray-600 border-r-2 border-gray-200"
35+
placeholder="An Impressionist oil painting of sunflowers in a purple vase…"
36+
/>
37+
<button
38+
id="btn"
39+
class="font-bold py-4 px-8 rounded-r-lg shadow-lg"
40+
onclick="generateImage()"
41+
>
42+
Generate
43+
</button>
44+
</div>
45+
<div id="image" class="grid grid-cols-2 lg:grid-cols-3 gap-1 mt-4"></div>
46+
<footer
47+
class="text-center mt-10 border-t-2 border-gray-200 pt-4"
48+
id="checkAuthor"
49+
>
50+
<p class="text-sm">
51+
Made with ❤️ by
52+
<a href="https://github.com/sauravhathi" class="font-semibold"
53+
>@sauravhathi</a
54+
>
55+
</p>
56+
</footer>
57+
</div>
58+
</body>
59+
<script>
60+
const api = "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";
61+
const url = "https://api.openai.com/v1/images/generations";
62+
const text = document.getElementById("text");
63+
const image = document.getElementById("image");
64+
const btn = document.getElementById("btn");
65+
66+
function generateImage() {
67+
if (text.value === "") {
68+
alert("Please enter a value");
69+
} else {
70+
const data = {
71+
prompt: text.value,
72+
n: 3,
73+
size: "1024x1024",
74+
};
75+
fetch(url, {
76+
method: "POST",
77+
headers: {
78+
"Content-Type": "application/json",
79+
Authorization: `Bearer ${api}`,
80+
},
81+
body: JSON.stringify(data),
82+
})
83+
.then((res) => res.json())
84+
.then((data) => {
85+
console.log(data);
86+
const arraySize = data.data.length;
87+
for (let i = 0; i < arraySize; i++) {
88+
image.innerHTML += `<img src="${data.data[i].url}" alt="image" class="w-100 p-2">`;
89+
}
90+
});
91+
}
92+
}
93+
94+
const checkAuthor = document.getElementById("checkAuthor");
95+
if (checkAuthor.children[0].children[0].textContent === "@sauravhathi") {
96+
} else {
97+
window.location.href = "https://github.com/sauravhathi";
98+
}
99+
100+
text.addEventListener("input", function () {
101+
if (text.value === "") {
102+
btn.classList.remove("bg-slate-900", "text-slate-50");
103+
text.classList.add("border-r-2", "border-gray-200");
104+
} else {
105+
text.classList.remove("border-r-2", "border-gray-200");
106+
btn.classList.add("bg-slate-900", "text-slate-50");
107+
}
108+
});
109+
</script>
110+
</html>

0 commit comments

Comments
 (0)