Spaces:
Sleeping
Sleeping
from flask import Flask, render_template, request, send_file | |
import google.generativeai as genai | |
import os | |
import re | |
from rdkit.Chem import MolFromSmiles, MolToImage | |
from rdkit.Chem.Draw import rdMolDraw2D | |
import tempfile | |
import uuid | |
# Configuration de l'API 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"}, | |
] | |
model = genai.GenerativeModel( | |
model_name="gemini-1.5-pro", | |
generation_config=generation_config, | |
safety_settings=safety_settings, | |
) | |
app = Flask(__name__) | |
# Dossier pour les images temporaires | |
app.config['UPLOAD_FOLDER'] = 'static/temp_images' | |
if not os.path.exists(app.config['UPLOAD_FOLDER']): | |
os.makedirs(app.config['UPLOAD_FOLDER']) | |
# Fonction pour extraire et traiter les structures chimiques | |
def process_chemfig(text): | |
chemfig_pattern = r"\\chemfig{(.*?)}" | |
matches = re.findall(chemfig_pattern, text) | |
image_paths = [] | |
for match in matches: | |
# Supposons que la syntaxe est du SMILES simplifié ou une représentation compatible RDKit | |
try: | |
# mol = MolFromSmiles(match) | |
mol=MolFromSmiles(match) | |
if mol: | |
d = rdMolDraw2D.MolDraw2DCairo(500,500) | |
d.DrawMolecule(mol) | |
d.FinishDrawing() | |
image_filename = str(uuid.uuid4()) + '.png' | |
image_path = os.path.join(app.config['UPLOAD_FOLDER'], image_filename) | |
d.WriteDrawingText(image_path) | |
image_paths.append(image_path) | |
text = text.replace(f"\\chemfig{{{match}}}", f'<img src="/temp_images/{image_filename}" style="width:100%; height:auto;" alt="Structure Chimique">') | |
else: | |
text = text.replace(f"\\chemfig{{{match}}}", f'Structure Chimique non valide : {match}') | |
except Exception as e: | |
text = text.replace(f"\\chemfig{{{match}}}", f'Erreur lors du rendu de la structure : {e}') | |
return text, image_paths | |
# Route principale | |
def index(): | |
generated_content = "" | |
image_paths = [] | |
if request.method == 'POST': | |
image_file = request.files.get('image') | |
mm = """ resous cet exercice. tu répondras en détaillant au maximum ton procédé de calcul. réponse attendue uniquement en Latex | |
""" | |
if image_file : | |
image_bytes = image_file.read() | |
parts=[ | |
{ | |
"mime_type":"image/jpeg", | |
"data": image_bytes | |
}, | |
mm | |
] | |
response = model.generate_content(parts) | |
generated_content = response.text | |
else: | |
text_input= request.form.get('text_input') | |
response = model.generate_content(mm+text_input) | |
generated_content = response.text | |
generated_content, image_paths = process_chemfig(generated_content) | |
return render_template('index.html', generated_content=generated_content, image_paths=image_paths) | |
# Route pour servir les images temporaires | |
def temp_image(filename): | |
return send_file(os.path.join(app.config['UPLOAD_FOLDER'], filename), mimetype='image/png') | |
if __name__ == '__main__': | |
app.run(debug=True) |