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