Spaces:
Sleeping
Sleeping
from flask import Flask, render_template, request, jsonify | |
import google.generativeai as genai | |
import os | |
from PIL import Image | |
import io | |
import base64 | |
app = Flask(__name__) | |
# Configuration Gemini | |
token = os.environ.get("TOKEN") | |
genai.configure(api_key=token) | |
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"}, | |
] | |
mm = """ resous cet exercice. tu répondras en détaillant au maximum ton procédé de calcul. réponse attendue uniquement en Latex""" | |
model = genai.GenerativeModel( | |
model_name="gemini-1.5-pro", | |
generation_config=generation_config, | |
safety_settings=safety_settings | |
) | |
def home(): | |
return render_template('index.html') | |
def generate(): | |
try: | |
# Récupérer l'image depuis la requête | |
image_data = request.json.get('image') | |
if not image_data: | |
return jsonify({"result": "djo"}) | |
# Convertir l'image base64 en image PIL | |
image_data = image_data.split(',')[1] | |
image_bytes = base64.b64decode(image_data) | |
image = Image.open(io.BytesIO(image_bytes)) | |
# Générer le contenu | |
response = model.generate_content([mm, image]) | |
result = response.text | |
return jsonify({"result": result}) | |
except Exception as e: | |
return jsonify({"error": str(e)}), 500 | |
if __name__ == '__main__': | |
app.run(debug=True) |