content / server.js
TDN-M's picture
Create server.js
39a05a8 verified
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}`);
});