Skip to content

Instantly share code, notes, and snippets.

@wise4rmgod
Last active September 8, 2023 11:57
Show Gist options
  • Save wise4rmgod/2adc72234e88dce9e0941f64736a3ab5 to your computer and use it in GitHub Desktop.
Save wise4rmgod/2adc72234e88dce9e0941f64736a3ab5 to your computer and use it in GitHub Desktop.
This is the complete codebase for creating an image generator using React and OpenAI
import React, { useState } from "react";
import axios from "axios";
const OPENAI_API_KEY = "YOUR_OPENAI_API_KEY"; // Replace with your actual API key
function App() {
const [prompt, setPrompt] = useState("A cute baby sea otter");
const [generatedImages, setGeneratedImages] = useState([]);
const [isLoading, setIsLoading] = useState(false);
async function generateImages() {
setIsLoading(true);
try {
const requestData = {
prompt: prompt,
n: 2,
size: "256x256", // Set the desired image size here
};
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${OPENAI_API_KEY}`,
};
const response = await axios.post(
"https://api.openai.com/v1/images/generations",
requestData,
{
headers: headers,
}
);
setGeneratedImages(response.data.data);
} catch (error) {
console.error("Error generating images:", error);
} finally {
setIsLoading(false);
}
}
return (
<div className="flex flex-col items-center justify-center min-h-screen">
<div>
<label htmlFor="prompt">Enter a Prompt: </label>
<input
type="text"
id="prompt"
value={prompt}
onChange={(e) => setPrompt(e.target.value)}
className="border rounded px-2 py-1"
/>
</div>
<button
onClick={generateImages}
disabled={isLoading}
className="mt-4 px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600"
>
{isLoading ? "Generating..." : "Generate Images"}
</button>
{isLoading && <p className="mt-4 text-gray-600">Loading...</p>}
{generatedImages.length > 0 && (
<div className="mt-4">
{generatedImages.map((image, index) => (
<div key={index} className="mt-4">
<img
src={image.url}
alt={`Generated Image ${index}`}
style={{ maxWidth: "100%", height: "auto" }}
/>
</div>
))}
</div>
)}
</div>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment