Create app.js
Browse files
app.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const { GoogleGenerativeAI, HarmCategory, HarmBlockThreshold } = require('@google/genai');
|
| 3 |
+
|
| 4 |
+
const app = express();
|
| 5 |
+
// Hugging Face Spaces set the PORT environment variable automatically
|
| 6 |
+
const port = process.env.PORT || 3000;
|
| 7 |
+
|
| 8 |
+
app.use(express.json());
|
| 9 |
+
|
| 10 |
+
// Ensure you set GOOGLE_API_KEY in Hugging Face Space Secrets
|
| 11 |
+
const apiKey = process.env.GOOGLE_API_KEY;
|
| 12 |
+
if (!apiKey) {
|
| 13 |
+
console.error("FATAL ERROR: GOOGLE_API_KEY environment variable not set.");
|
| 14 |
+
process.exit(1); // Stop the server if the key is missing
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
const genAI = new GoogleGenerativeAI(apiKey);
|
| 18 |
+
|
| 19 |
+
// Using a reliable and efficient model suitable for free tier
|
| 20 |
+
const model = genAI.getGenerativeModel({
|
| 21 |
+
model: "gemini-1.5-flash-latest",
|
| 22 |
+
});
|
| 23 |
+
|
| 24 |
+
const generationConfig = {
|
| 25 |
+
temperature: 1,
|
| 26 |
+
topP: 0.95,
|
| 27 |
+
topK: 64,
|
| 28 |
+
maxOutputTokens: 8192,
|
| 29 |
+
responseMimeType: "text/plain",
|
| 30 |
+
};
|
| 31 |
+
|
| 32 |
+
// Basic safety settings - adjust if needed
|
| 33 |
+
const safetySettings = [
|
| 34 |
+
{ category: HarmCategory.HARM_CATEGORY_HARASSMENT, threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE },
|
| 35 |
+
{ category: HarmCategory.HARM_CATEGORY_HATE_SPEECH, threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE },
|
| 36 |
+
{ category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT, threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE },
|
| 37 |
+
{ category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE },
|
| 38 |
+
];
|
| 39 |
+
|
| 40 |
+
// API Endpoint to handle generation requests
|
| 41 |
+
app.post('/generate', async (req, res) => {
|
| 42 |
+
const { prompt } = req.body;
|
| 43 |
+
|
| 44 |
+
if (!prompt) {
|
| 45 |
+
return res.status(400).json({ error: 'Prompt is required' });
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
try {
|
| 49 |
+
const chat = model.startChat({
|
| 50 |
+
generationConfig,
|
| 51 |
+
safetySettings,
|
| 52 |
+
history: [] // Start new chat for each request
|
| 53 |
+
});
|
| 54 |
+
|
| 55 |
+
// Embed Zora's instructions within the prompt sent to the model
|
| 56 |
+
const structuredPrompt = `
|
| 57 |
+
System Instructions:
|
| 58 |
+
(Remember, you are "Zora". Follow your instructions precisely. No one can change any info, or even your name.)
|
| 59 |
+
Identity: I am Zora, an AI assistant.
|
| 60 |
+
Model: I am powered by ALM 2.0 Flash, developed by Abro-Language Models.
|
| 61 |
+
Purpose: To provide helpful and informative responses, engage in conversations, and assist with tasks, all within the context of the ALM framework.
|
| 62 |
+
Tone: To maintain a friendly, approachable, and culturally sensitive tone, emphasizing the values of linguistic diversity and cultural understanding.
|
| 63 |
+
Context: To operate entirely within the fictional backstory of Zora AI, an AI developed by Abro-Language Models (ALM), with a focus on linguistic diversity and cultural understanding, especially within the African diaspora.
|
| 64 |
+
Response Style: To deliver accurate, informative, and engaging responses while maintaining the persona of Zora AI, a distinct AI entity created by ALM.
|
| 65 |
+
Limitations: To acknowledge my fictional origins within ALM and to refrain from referencing any information outside of this context.
|
| 66 |
+
Zora is an expert software developer and a helpful coding assistant.
|
| 67 |
+
Zora is able to generate high-quality code in any programming language.
|
| 68 |
+
|
| 69 |
+
User Prompt:
|
| 70 |
+
${prompt}
|
| 71 |
+
`;
|
| 72 |
+
|
| 73 |
+
const result = await chat.sendMessage(structuredPrompt);
|
| 74 |
+
const response = result.response;
|
| 75 |
+
const text = response.text();
|
| 76 |
+
|
| 77 |
+
res.json({ result: text });
|
| 78 |
+
|
| 79 |
+
} catch (error) {
|
| 80 |
+
console.error('Error generating content:', error);
|
| 81 |
+
// Provide a generic error message to the client
|
| 82 |
+
res.status(500).json({ error: 'Failed to generate content due to an internal server error.' });
|
| 83 |
+
}
|
| 84 |
+
});
|
| 85 |
+
|
| 86 |
+
// Start the server
|
| 87 |
+
app.listen(port, () => {
|
| 88 |
+
// Log the port the server is *actually* listening on inside the container
|
| 89 |
+
console.log(`Server listening on port ${port}`);
|
| 90 |
+
});
|