Spaces:
Runtime error
Runtime error
from flask import Flask, request, render_template, jsonify | |
import PIL.Image | |
import google.generativeai as genai | |
import os | |
from tempfile import NamedTemporaryFile | |
app = Flask(__name__) | |
# Configuration de Gemini | |
generation_config = { | |
"temperature": 1, | |
"top_p": 0.95, | |
"top_k": 64, | |
"max_output_tokens": 8192, | |
} | |
safety_settings = [ | |
{ | |
"category": "HARM_CATEGORY_HARASSMENT", | |
"threshold": "BLOCK_NONE" | |
}, | |
{ | |
"category": "HARM_CATEGORY_HATE_SPEECH", | |
"threshold": "BLOCK_NONE" | |
}, | |
{ | |
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", | |
"threshold": "BLOCK_NONE" | |
}, | |
{ | |
"category": "HARM_CATEGORY_DANGEROUS_CONTENT", | |
"threshold": "BLOCK_NONE" | |
}, | |
] | |
GOOGLE_API_KEY = os.environ.get("TOKEN") | |
genai.configure(api_key=GOOGLE_API_KEY) | |
def index(): | |
return render_template('math.html') | |
def upload_image(): | |
if 'image' not in request.files: | |
return jsonify({'error': 'Aucune image fournie'}), 400 | |
file = request.files['image'] | |
if file.filename == '': | |
return jsonify({'error': 'Aucun fichier sélectionné'}), 400 | |
# Créer un fichier temporaire pour stocker l'image | |
with NamedTemporaryFile(delete=False) as temp_file: | |
file.save(temp_file.name) | |
# Ouvrir l'image avec Pillow | |
img = PIL.Image.open(temp_file.name) | |
# Préparer le prompt | |
prompt = """Résous ce problème mathématiques. Je veux qu'en réponse tu me donnes un rendu html complet en intégrant MathJax et plotly.js.""" | |
# Initialiser le modèle Gemini | |
model = genai.GenerativeModel( | |
model_name="gemini-1.5-flash-002", | |
generation_config=generation_config, | |
safety_settings=safety_settings | |
) | |
try: | |
# Générer la réponse | |
response = model.generate_content( | |
[prompt, img], | |
request_options={"timeout": 600} | |
) | |
# Supprimer le fichier temporaire | |
os.unlink(temp_file.name) | |
return jsonify({'result': response.text}) | |
except Exception as e: | |
return jsonify({'error': str(e)}), 500 | |