Spaces:
Sleeping
Sleeping
| from flask import Flask, render_template, request, jsonify, Response, stream_with_context | |
| from google import genai | |
| from google.genai import types | |
| import os | |
| from PIL import Image | |
| import io | |
| import base64 | |
| import json | |
| app = Flask(__name__) | |
| GOOGLE_API_KEY = os.environ.get("GEMINI_API_KEY") | |
| client = genai.Client( | |
| api_key=GOOGLE_API_KEY, | |
| ) | |
| def index(): | |
| #return "La plateforme est en maintenance." | |
| return render_template('index.html') | |
| def indexx(): | |
| return render_template('maj.html') | |
| def solve(): | |
| try: | |
| image_data = request.files['image'].read() | |
| img = Image.open(io.BytesIO(image_data)) | |
| 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 with rendering latex""" | |
| ] | |
| ) | |
| #Resous cette exercice. ça doit être bien présentable et espacé. je veux un jolie rendu | |
| 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": str(e)})}\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': str(e)}), 500 | |
| def solved(): | |
| try: | |
| image_data = request.files['image'].read() | |
| img = Image.open(io.BytesIO(image_data)) | |
| 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.0-flash-thinking-exp-01-21", | |
| contents=[ | |
| {'inline_data': {'mime_type': 'image/png', 'data': img_str}}, | |
| """ | |
| **Tâche :** Analyse l'image fournie et résous le problème ou réponds à la question qu'elle contient. | |
| **Instructions :** | |
| 1. **Langue :** Rédige l'intégralité de ta réponse en français. | |
| 2. **Détail :** Fournis une solution ou une explication détaillée, étape par étape, pour assurer une compréhension complète. | |
| 3. **Format Mathématique :** Utilise la syntaxe LaTeX (par exemple, `$ ... $` pour les éléments en ligne, `$$ ... $$` pour toutes les équations, formules, variables et notations mathématiques ou scientifiques. | |
| 4. **Présentation :** Structure la réponse de manière claire et aérée. Utilise des sauts de ligne et une organisation logique pour une lisibilité optimale. | |
| 5. **Format de Sortie :** Ta réponse doit contenir **uniquement** le contenu de la solution/explication formatée comme demandé ci-dessus. N'inclus **strictement aucun** préambule de document LaTeX (comme `\\documentclass{...}`, `\\usepackage{...}`, `\\begin{document}`, etc.) ni la structure englobante `\\begin{document} ... \\end{document}`. La sortie doit être prête à être insérée directement dans un corps de document LaTeX existant. | |
| **Contenu de l'Image :** [Le modèle utilisera l'image fournie ici comme contexte principal] | |
| """ | |
| ] | |
| ) | |
| #Resous cette exercice. ça doit être bien présentable et espacé. je veux un jolie rendu | |
| 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": str(e)})}\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': str(e)}), 500 | |
| if __name__ == '__main__': | |
| app.run(debug=True) |