Spaces:
Build error
Build error
from flask import Flask, render_template, request, jsonify | |
from google import genai | |
from google.genai import types | |
import os | |
from PIL import Image | |
import io | |
import base64 | |
app = Flask(__name__) | |
# Remplacez par votre clé API réelle | |
GOOGLE_API_KEY = "AIzaSyC_zxN9IHjEAxIoshWPzMfgb9qwMsu5t5Y" | |
client = genai.Client( | |
api_key=GOOGLE_API_KEY, | |
# Use `v1alpha` so you can see the `thought` flag. | |
http_options={'api_version': 'v1alpha'}, | |
) | |
def index(): | |
return render_template('index.html') | |
def solve(): | |
try: | |
image_data = request.files['image'].read() # Lire les données binaires de l'image | |
img = Image.open(io.BytesIO(image_data)) | |
# Convertir l'image en base64 pour l'envoyer à l'API Gemini | |
buffered = io.BytesIO() | |
img.save(buffered, format="PNG") | |
img_str = base64.b64encode(buffered.getvalue()).decode() | |
# Utiliser l'API Gemini pour générer la réponse | |
response = client.models.generate_content( | |
model="gemini-2.0-flash-thinking-exp-01-21", | |
config={'thinking_config': {'include_thoughts': True}}, | |
contents=[ | |
{'inline_data': {'mime_type': 'image/png', 'data': img_str}}, | |
"What's the area of the overlapping region?" | |
] | |
) | |
# Extraire les pensées et la réponse | |
thoughts = "" | |
answer = "" | |
for part in response.candidates[0].content.parts: | |
if part.thought: | |
thoughts += part.text + "\n" | |
else: | |
answer += part.text + "\n" | |
return jsonify({'thoughts': thoughts, 'answer': answer}) | |
except Exception as e: | |
return jsonify({'error': str(e)}), 500 | |
if __name__ == '__main__': | |
app.run(debug=True) |