shimanta420 commited on
Commit
d63450c
·
verified ·
1 Parent(s): 0321cd3

Upload pages/api/generate-image.js with huggingface_hub

Browse files
Files changed (1) hide show
  1. pages/api/generate-image.js +27 -0
pages/api/generate-image.js ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Configuration, OpenAIApi } from 'openai'
2
+
3
+ const configuration = new Configuration({
4
+ apiKey: process.env.OPENAI_API_KEY,
5
+ })
6
+ const openai = new OpenAIApi(configuration)
7
+
8
+ export default async function handler(req, res) {
9
+ if (req.method !== 'POST') {
10
+ return res.status(405).json({ message: 'Method not allowed' })
11
+ }
12
+
13
+ try {
14
+ const { prompt } = req.body
15
+
16
+ const response = await openai.createImage({
17
+ prompt: prompt,
18
+ n: 1,
19
+ size: "512x512",
20
+ })
21
+
22
+ res.status(200).json({ url: response.data.data[0].url })
23
+ } catch (error) {
24
+ console.error('OpenAI API error:', error)
25
+ res.status(500).json({ error: 'Failed to generate image' })
26
+ }
27
+ }