Spaces:
Running
Running
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 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 ce problème en français en utilisant des formules mathématiques LaTeX quand nécessaire. | |
Présente ta réponse de manière claire et structurée.""" | |
], | |
config=types.GenerateContentConfig( | |
thinking_config=types.ThinkingConfig( | |
thinking_budget=8000 | |
), | |
tools=[types.Tool( | |
code_execution=types.ToolCodeExecution | |
)] | |
) | |
) | |
for chunk in response: | |
for part in chunk.candidates[0].content.parts: | |
# Gestion des modes (thinking/answering) | |
if hasattr(part, 'thought') and part.thought: | |
if mode != "thinking": | |
yield f'data: {json.dumps({"mode": "thinking"})}\n\n' | |
mode = "thinking" | |
yield f'data: {json.dumps({"content": part.thought})}\n\n' | |
elif part.text is not None: | |
if mode != "answering": | |
yield f'data: {json.dumps({"mode": "answering"})}\n\n' | |
mode = "answering" | |
yield f'data: {json.dumps({"content": part.text})}\n\n' | |
# Gestion du code exécutable | |
elif hasattr(part, 'executable_code') and part.executable_code is not None: | |
if mode != "answering": | |
yield f'data: {json.dumps({"mode": "answering"})}\n\n' | |
mode = "answering" | |
# Formater le code pour l'affichage | |
code_content = f"```python\n{part.executable_code.code}\n```" | |
yield f'data: {json.dumps({"content": code_content})}\n\n' | |
# Gestion des résultats de l'exécution du code | |
elif hasattr(part, 'code_execution_result') and part.code_execution_result is not None: | |
if mode != "answering": | |
yield f'data: {json.dumps({"mode": "answering"})}\n\n' | |
mode = "answering" | |
yield f'data: {json.dumps({"content": f"```\n{part.code_execution_result.output}\n```"})}\n\n' | |
# Gestion des images inline | |
elif hasattr(part, 'inline_data') and part.inline_data is not None: | |
if mode != "answering": | |
yield f'data: {json.dumps({"mode": "answering"})}\n\n' | |
mode = "answering" | |
# Convertir l'image en base64 pour l'affichage HTML | |
image_data = part.inline_data.data | |
image_format = part.inline_data.mime_type.split('/')[-1] | |
image_src = f"data:{part.inline_data.mime_type};base64,{image_data}" | |
image_tag = f'<img src="{image_src}" alt="Generated Image" style="max-width:100%;">' | |
yield f'data: {json.dumps({"content": image_tag})}\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(): | |
# Version similaire avec le modèle flash | |
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-flash-preview-04-17", | |
contents=[ | |
{'inline_data': {'mime_type': 'image/png', 'data': img_str}}, | |
"""Résous ce problème en français en utilisant des formules mathématiques LaTeX quand nécessaire. | |
Présente ta réponse de manière claire et structurée.""" | |
], | |
config=types.GenerateContentConfig( | |
tools=[types.Tool( | |
code_execution=types.ToolCodeExecution | |
)] | |
) | |
) | |
for chunk in response: | |
for part in chunk.candidates[0].content.parts: | |
# Gestion des modes (thinking/answering) | |
if hasattr(part, 'thought') and part.thought: | |
if mode != "thinking": | |
yield f'data: {json.dumps({"mode": "thinking"})}\n\n' | |
mode = "thinking" | |
yield f'data: {json.dumps({"content": part.thought})}\n\n' | |
elif part.text is not None: | |
if mode != "answering": | |
yield f'data: {json.dumps({"mode": "answering"})}\n\n' | |
mode = "answering" | |
yield f'data: {json.dumps({"content": part.text})}\n\n' | |
# Gestion du code exécutable | |
elif hasattr(part, 'executable_code') and part.executable_code is not None: | |
if mode != "answering": | |
yield f'data: {json.dumps({"mode": "answering"})}\n\n' | |
mode = "answering" | |
# Formater le code pour l'affichage | |
code_content = f"```python\n{part.executable_code.code}\n```" | |
yield f'data: {json.dumps({"content": code_content})}\n\n' | |
# Gestion des résultats de l'exécution du code | |
elif hasattr(part, 'code_execution_result') and part.code_execution_result is not None: | |
if mode != "answering": | |
yield f'data: {json.dumps({"mode": "answering"})}\n\n' | |
mode = "answering" | |
yield f'data: {json.dumps({"content": f"```\n{part.code_execution_result.output}\n```"})}\n\n' | |
# Gestion des images inline | |
elif hasattr(part, 'inline_data') and part.inline_data is not None: | |
if mode != "answering": | |
yield f'data: {json.dumps({"mode": "answering"})}\n\n' | |
mode = "answering" | |
# Convertir l'image en base64 pour l'affichage HTML | |
image_data = part.inline_data.data | |
image_format = part.inline_data.mime_type.split('/')[-1] | |
image_src = f"data:{part.inline_data.mime_type};base64,{image_data}" | |
image_tag = f'<img src="{image_src}" alt="Generated Image" style="max-width:100%;">' | |
yield f'data: {json.dumps({"content": image_tag})}\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) |