Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const axios = require('axios');
|
| 3 |
+
const cors = require('cors');
|
| 4 |
+
const path = require('path');
|
| 5 |
+
require('dotenv').config();
|
| 6 |
+
|
| 7 |
+
const app = express();
|
| 8 |
+
const port = process.env.PORT || 8000; // Use Hugging Face's default port or fallback to 8000
|
| 9 |
+
|
| 10 |
+
app.use(cors());
|
| 11 |
+
app.use(express.json());
|
| 12 |
+
app.use(express.static(path.join(__dirname, 'public'))); // Serve static files from 'public' folder
|
| 13 |
+
|
| 14 |
+
// Serve index.html for the root route
|
| 15 |
+
app.get('/', (req, res) => {
|
| 16 |
+
res.sendFile(path.join(__dirname, 'public', 'index.html'));
|
| 17 |
+
});
|
| 18 |
+
|
| 19 |
+
// API endpoint for generating content
|
| 20 |
+
app.post('/api/generate', async (req, res) => {
|
| 21 |
+
const { prompt } = req.body;
|
| 22 |
+
|
| 23 |
+
try {
|
| 24 |
+
const response = await axios.post(
|
| 25 |
+
'https://api.groq.com/v1/chat/completions',
|
| 26 |
+
{
|
| 27 |
+
model: 'mixtral-8x7b-32768',
|
| 28 |
+
messages: [{ role: 'user', content: prompt }],
|
| 29 |
+
max_tokens: 1000,
|
| 30 |
+
temperature: 0.7,
|
| 31 |
+
top_p: 0.9
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
headers: {
|
| 35 |
+
'Authorization': `Bearer ${process.env.GROQ_API_KEY}`,
|
| 36 |
+
'Content-Type': 'application/json'
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
);
|
| 40 |
+
|
| 41 |
+
const content = response.data.choices[0].message.content;
|
| 42 |
+
res.json({ content });
|
| 43 |
+
} catch (error) {
|
| 44 |
+
console.error('Error calling Groq API:', error);
|
| 45 |
+
res.status(500).json({ error: 'Failed to generate content' });
|
| 46 |
+
}
|
| 47 |
+
});
|
| 48 |
+
|
| 49 |
+
app.listen(port, () => {
|
| 50 |
+
console.log(`Server running at http://localhost:${port}`);
|
| 51 |
+
});
|