| | import os |
| | import google.generativeai as genai |
| | from flask import Flask, request, jsonify, Response |
| | from flask_cors import CORS |
| |
|
| | app = Flask(__name__) |
| | CORS(app) |
| |
|
| | |
| | GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY") |
| | if not GOOGLE_API_KEY: |
| | raise ValueError("GOOGLE_API_KEY nicht gesetzt!") |
| |
|
| | genai.configure(api_key=GOOGLE_API_KEY) |
| | model = genai.GenerativeModel('gemini-1.5-pro') |
| | chat = model.start_chat(history=[]) |
| |
|
| | |
| | @app.route('/', methods=['GET']) |
| | def index(): |
| | return Response( |
| | "<h2>Moejra Chat API</h2><p>Die API ist erreichbar. Sende POST-Anfragen an <code>/chat</code>.</p>", |
| | mimetype='text/html' |
| | ) |
| |
|
| | |
| | @app.route('/chat', methods=['POST']) |
| | def handle_chat(): |
| | try: |
| | user_input = request.json.get('text') |
| | response = chat.send_message(user_input) |
| | return jsonify({ |
| | "text": response.text, |
| | "images": [] |
| | }) |
| | except Exception as e: |
| | return jsonify({"error": str(e)}), 500 |
| |
|
| | if __name__ == '__main__': |
| | app.run(host='0.0.0.0', port=7860) |
| |
|