File size: 1,080 Bytes
3ebc508 60fc9ed 3ebc508 f5c7eed |
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 |
# app.py - Flask Backend
from flask import Flask, request, jsonify
import google.generativeai as genai
from dotenv import load_dotenv
import os
from flask_cors import CORS
# Load environment variables
load_dotenv()
# Initialize Flask app
app = Flask(__name__)
CORS(app) # Enable CORS for all routes
# Configure Gemini
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
model = genai.GenerativeModel('gemini-2.5-flash')
@app.route('/chat', methods=['POST'])
def chat():
try:
data = request.json
user_message = data.get('message')
if not user_message:
return jsonify({"error": "No message provided"}), 400
# Generate response using Gemini
response = model.generate_content(user_message)
return jsonify({
"response": response.text
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/')
def serve_index():
return app.send_static_file('index.html')
if __name__ == '__main__':
app.run(host="0.0.0.0", port=7860) |