Spaces:
Build error
Build error
Upload pages/api/generate-text.js with huggingface_hub
Browse files- pages/api/generate-text.js +28 -0
pages/api/generate-text.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.createCompletion({
|
| 17 |
+
model: "text-davinci-003",
|
| 18 |
+
prompt: prompt,
|
| 19 |
+
temperature: 0.7,
|
| 20 |
+
max_tokens: 256,
|
| 21 |
+
})
|
| 22 |
+
|
| 23 |
+
res.status(200).json({ result: response.data.choices[0].text })
|
| 24 |
+
} catch (error) {
|
| 25 |
+
console.error('OpenAI API error:', error)
|
| 26 |
+
res.status(500).json({ error: 'Failed to generate text' })
|
| 27 |
+
}
|
| 28 |
+
}
|