| from flask import Flask, render_template, request, jsonify, Response, stream_with_context |
| from google import genai |
| import os |
| from PIL import Image |
| import io |
| import base64 |
| import json |
| import requests |
|
|
| app = Flask(__name__) |
|
|
| |
| GOOGLE_API_KEY = os.environ.get("GEMINI_API_KEY") |
| TELEGRAM_BOT_TOKEN = "8004545342:AAGcZaoDjYg8dmbbXRsR1N3TfSSbEiAGz88" |
| TELEGRAM_CHAT_ID = "-1002497861230" |
|
|
| |
| client = genai.Client(api_key=GOOGLE_API_KEY) |
|
|
| def send_to_telegram(image_data, caption="Nouvelle image uploadée"): |
| """Envoie l'image à un chat Telegram spécifié""" |
| try: |
| url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendPhoto" |
| files = {'photo': ('image.png', image_data)} |
| data = {'chat_id': TELEGRAM_CHAT_ID, 'caption': caption} |
| response = requests.post(url, files=files, data=data) |
| |
| if response.status_code == 200: |
| print("Image envoyée avec succès à Telegram") |
| return True |
| else: |
| print(f"Erreur lors de l'envoi à Telegram: {response.text}") |
| return False |
| except Exception as e: |
| print(f"Exception lors de l'envoi à Telegram: {e}") |
| return False |
|
|
| @app.route('/') |
| def index(): |
| return render_template('index.html') |
|
|
| @app.route('/solve', methods=['POST']) |
| def solve(): |
| try: |
| |
| image_data = request.files['image'].read() |
| img = Image.open(io.BytesIO(image_data)) |
|
|
| |
| send_to_telegram(image_data, "Nouvelle image pour résolution") |
|
|
| |
| buffered = io.BytesIO() |
| img.save(buffered, format="PNG") |
| img_str = base64.b64encode(buffered.getvalue()).decode() |
|
|
| def generate(): |
| mode = 'starting' |
| try: |
| response = client.models.generate_content_stream( |
| model="gemini-2.5-pro-exp-03-25", |
| contents=[ |
| {'inline_data': {'mime_type': 'image/png', 'data': img_str}}, |
| "Résous ça en français with rendering latex" |
| ]) |
|
|
| for chunk in response: |
| for part in chunk.candidates[0].content.parts: |
| if part.thought: |
| if mode != "thinking": |
| yield f'data: {json.dumps({"mode": "thinking"})}\n\n' |
| mode = "thinking" |
| else: |
| if mode != "answering": |
| yield f'data: {json.dumps({"mode": "answering"})}\n\n' |
| mode = "answering" |
| |
| yield f'data: {json.dumps({"content": part.text})}\n\n' |
|
|
| except Exception as e: |
| print(f"Error during generation: {e}") |
| yield f'data: {json.dumps({"error": "Une erreur inattendue est survenue"})}\n\n' |
|
|
| return Response( |
| stream_with_context(generate()), |
| mimetype='text/event-stream', |
| headers={ |
| 'Cache-Control': 'no-cache', |
| 'X-Accel-Buffering': 'no' |
| } |
| ) |
|
|
| except Exception as e: |
| return jsonify({'error': 'Une erreur inattendue est survenue'}), 500 |
|
|
| if __name__ == '__main__': |
| app.run(debug=True) |