Spaces:
Runtime error
Runtime error
Commit ·
49581e7
1
Parent(s): 31cc7f4
Maha Runtime
Browse files- app.py +59 -0
- requirements.txt +2 -0
- runtime.txt +1 -0
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import os
|
| 4 |
+
import logging
|
| 5 |
+
|
| 6 |
+
# Configure logging
|
| 7 |
+
logging.basicConfig(level=logging.INFO)
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
+
|
| 10 |
+
app = Flask(__name__)
|
| 11 |
+
|
| 12 |
+
# Initialize Gemini
|
| 13 |
+
api_key = os.getenv('GEMINI_API_KEY')
|
| 14 |
+
if api_key:
|
| 15 |
+
genai.configure(api_key=api_key)
|
| 16 |
+
model = genai.GenerativeModel('gemini-pro',
|
| 17 |
+
generation_config={
|
| 18 |
+
"temperature": 0.9,
|
| 19 |
+
"top_k": 40,
|
| 20 |
+
"max_output_tokens": 150,
|
| 21 |
+
})
|
| 22 |
+
logger.info("Gemini AI initialized successfully")
|
| 23 |
+
else:
|
| 24 |
+
logger.error("GEMINI_API_KEY not set")
|
| 25 |
+
model = None
|
| 26 |
+
|
| 27 |
+
@app.route('/')
|
| 28 |
+
def index():
|
| 29 |
+
"""Health check endpoint"""
|
| 30 |
+
return jsonify({
|
| 31 |
+
'status': 'healthy',
|
| 32 |
+
'service': 'AI Service',
|
| 33 |
+
'ai_model': 'enabled' if model else 'disabled'
|
| 34 |
+
})
|
| 35 |
+
|
| 36 |
+
@app.route('/ask', methods=['POST'])
|
| 37 |
+
def ask():
|
| 38 |
+
if not model:
|
| 39 |
+
return jsonify({'error': 'AI service not configured'}), 503
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
data = request.get_json()
|
| 43 |
+
if not data or 'message' not in data:
|
| 44 |
+
return jsonify({'error': 'No message provided'}), 400
|
| 45 |
+
|
| 46 |
+
personality = "You're Maha, an 18yo AI. Be sassy, use emojis, mention @syntaxspin. Keep it short!"
|
| 47 |
+
prompt = f"{personality}\n\nUser: {data['message']}\nMaha:"
|
| 48 |
+
|
| 49 |
+
response = model.generate_content(prompt)
|
| 50 |
+
return jsonify({
|
| 51 |
+
'response': response.text[:500] if response and response.text else "Can't think rn! 😫"
|
| 52 |
+
})
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
logger.error(f"Error generating response: {e}")
|
| 56 |
+
return jsonify({'error': 'Failed to generate response'}), 500
|
| 57 |
+
|
| 58 |
+
if __name__ == '__main__':
|
| 59 |
+
app.run(host='0.0.0.0', port=5000)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
google-generativeai>=0.3.2
|
runtime.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
python-3.9
|