-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OpenAI DALL-E image generation feature
- Add image generation component - Create API route for OpenAI image generation - Update Next.js config for DALL-E image hosting - Add toggle between chat and image generation modes
- Loading branch information
1 parent
e22595d
commit 22966c2
Showing
4 changed files
with
141 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { NextResponse } from 'next/server' | ||
import OpenAI from 'openai' | ||
|
||
const openai = new OpenAI({ | ||
apiKey: process.env.OPENAI_API_KEY, | ||
}) | ||
|
||
export async function POST(req: Request) { | ||
try { | ||
const { prompt } = await req.json() | ||
|
||
const response = await openai.images.generate({ | ||
model: "dall-e-3", | ||
prompt: prompt, | ||
n: 1, | ||
size: "1024x1024", | ||
}) | ||
|
||
return NextResponse.json({ url: response.data[0].url }) | ||
} catch (error) { | ||
console.error('Error generating image:', error) | ||
return NextResponse.json( | ||
{ error: 'Failed to generate image' }, | ||
{ status: 500 } | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use client' | ||
|
||
import { useState } from 'react' | ||
import { Button } from '@/components/ui/button' | ||
import { Input } from '@/components/ui/input' | ||
import { Card } from '@/components/ui/card' | ||
import Image from 'next/image' | ||
|
||
export function ImageGeneration() { | ||
const [prompt, setPrompt] = useState('') | ||
const [isLoading, setIsLoading] = useState(false) | ||
const [generatedImage, setGeneratedImage] = useState<string | null>(null) | ||
|
||
const generateImage = async () => { | ||
if (!prompt.trim() || isLoading) return | ||
|
||
setIsLoading(true) | ||
try { | ||
const response = await fetch('/api/generate-image', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ prompt }), | ||
}) | ||
|
||
const data = await response.json() | ||
if (data.url) { | ||
setGeneratedImage(data.url) | ||
} | ||
} catch (error) { | ||
console.error('Error generating image:', error) | ||
} finally { | ||
setIsLoading(false) | ||
} | ||
} | ||
|
||
return ( | ||
<div className="flex flex-col space-y-4 p-4"> | ||
<div className="flex space-x-2"> | ||
<Input | ||
value={prompt} | ||
onChange={(e) => setPrompt(e.target.value)} | ||
placeholder="Describe the image you want to generate..." | ||
className="flex-1" | ||
onKeyPress={(e) => e.key === 'Enter' && generateImage()} | ||
/> | ||
<Button | ||
onClick={generateImage} | ||
disabled={isLoading} | ||
> | ||
{isLoading ? 'Generating...' : 'Generate'} | ||
</Button> | ||
</div> | ||
|
||
{generatedImage && ( | ||
<Card className="p-4"> | ||
<div className="relative w-full aspect-square"> | ||
<Image | ||
src={generatedImage} | ||
alt="Generated image" | ||
fill | ||
className="object-contain" | ||
/> | ||
</div> | ||
</Card> | ||
)} | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters