|
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__) |
|
|
|
|
|
generation_config = { |
|
"temperature": 1, |
|
"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) |
|
|
|
@app.route('/') |
|
def index(): |
|
return render_template('math.html') |
|
|
|
@app.route('/upload', methods=['POST']) |
|
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 |
|
|
|
|
|
with NamedTemporaryFile(delete=False) as temp_file: |
|
file.save(temp_file.name) |
|
|
|
|
|
img = PIL.Image.open(temp_file.name) |
|
|
|
|
|
prompt = """Résous ce problème mathématiques. Je veux qu'en réponse tu me donnes un rendu complet en utilisant du Latex.""" |
|
|
|
|
|
model = genai.GenerativeModel( |
|
model_name="gemini-1.5-pro-002", |
|
generation_config=generation_config, |
|
safety_settings=safety_settings |
|
) |
|
|
|
try: |
|
|
|
response = model.generate_content( |
|
[prompt, img], |
|
request_options={"timeout": 600} |
|
) |
|
|
|
|
|
os.unlink(temp_file.name) |
|
|
|
return jsonify({'result': response.text}) |
|
|
|
except Exception as e: |
|
return jsonify({'error': str(e)}), 500 |
|
|
|
|