File size: 3,703 Bytes
b30ad3b
a10ea6b
 
5f9325a
b30ad3b
 
5f9325a
b30ad3b
5f9325a
b30ad3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f9325a
b30ad3b
3f35d09
b30ad3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f35d09
b30ad3b
 
 
 
 
3f35d09
b30ad3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a10ea6b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
@app.route('/', methods=['GET', 'POST'])
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
@app.route('/temp_images/<filename>')
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)