Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,12 +2,9 @@ from flask import Flask, render_template, request, send_file
|
|
2 |
import google.generativeai as genai
|
3 |
import os
|
4 |
import re
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
from rdkit.Chem import Draw
|
9 |
-
import tempfile
|
10 |
import uuid
|
|
|
11 |
|
12 |
# Configuration de l'API Gemini
|
13 |
token = os.environ.get("TOKEN")
|
@@ -32,10 +29,13 @@ model = genai.GenerativeModel(
|
|
32 |
|
33 |
app = Flask(__name__)
|
34 |
|
35 |
-
#
|
36 |
app.config['UPLOAD_FOLDER'] = 'static/temp_images'
|
|
|
37 |
if not os.path.exists(app.config['UPLOAD_FOLDER']):
|
38 |
os.makedirs(app.config['UPLOAD_FOLDER'])
|
|
|
|
|
39 |
|
40 |
# Fonction pour extraire et traiter les structures chimiques
|
41 |
def process_chemfig(text):
|
@@ -44,22 +44,32 @@ def process_chemfig(text):
|
|
44 |
image_paths = []
|
45 |
|
46 |
for match in matches:
|
47 |
-
# Supposons que la syntaxe est du SMILES simplifié ou une représentation compatible RDKit
|
48 |
try:
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
except Exception as e:
|
64 |
text = text.replace(f"\\chemfig{{{match}}}", f'Erreur lors du rendu de la structure : {e}')
|
65 |
|
@@ -92,13 +102,16 @@ def index():
|
|
92 |
generated_content = response.text
|
93 |
|
94 |
generated_content, image_paths = process_chemfig(generated_content)
|
95 |
-
|
96 |
return render_template('index.html', generated_content=generated_content, image_paths=image_paths)
|
97 |
|
98 |
-
#
|
99 |
@app.route('/temp_images/<filename>')
|
100 |
def temp_image(filename):
|
101 |
return send_file(os.path.join(app.config['UPLOAD_FOLDER'], filename), mimetype='image/png')
|
102 |
|
|
|
|
|
|
|
|
|
103 |
if __name__ == '__main__':
|
104 |
app.run(debug=True)
|
|
|
2 |
import google.generativeai as genai
|
3 |
import os
|
4 |
import re
|
5 |
+
import subprocess
|
|
|
|
|
|
|
|
|
6 |
import uuid
|
7 |
+
import tempfile
|
8 |
|
9 |
# Configuration de l'API Gemini
|
10 |
token = os.environ.get("TOKEN")
|
|
|
29 |
|
30 |
app = Flask(__name__)
|
31 |
|
32 |
+
# Dossiers pour les fichiers temporaires
|
33 |
app.config['UPLOAD_FOLDER'] = 'static/temp_images'
|
34 |
+
app.config['CHEM_RENDERS_FOLDER'] = 'static/chem_renders'
|
35 |
if not os.path.exists(app.config['UPLOAD_FOLDER']):
|
36 |
os.makedirs(app.config['UPLOAD_FOLDER'])
|
37 |
+
if not os.path.exists(app.config['CHEM_RENDERS_FOLDER']):
|
38 |
+
os.makedirs(app.config['CHEM_RENDERS_FOLDER'])
|
39 |
|
40 |
# Fonction pour extraire et traiter les structures chimiques
|
41 |
def process_chemfig(text):
|
|
|
44 |
image_paths = []
|
45 |
|
46 |
for match in matches:
|
|
|
47 |
try:
|
48 |
+
# Créer un fichier LaTeX temporaire
|
49 |
+
tex_filename = str(uuid.uuid4()) + '.tex'
|
50 |
+
tex_filepath = os.path.join(app.config['CHEM_RENDERS_FOLDER'], tex_filename)
|
51 |
+
with open(tex_filepath, 'w') as tex_file:
|
52 |
+
tex_file.write(r'\documentclass[margin=10pt]{standalone}\usepackage{chemfig}\begin{document}\chemfig{' + match + r'}\end{document}')
|
53 |
+
|
54 |
+
# Compiler le fichier LaTeX en PDF
|
55 |
+
pdf_filename = tex_filename[:-4] + '.pdf'
|
56 |
+
pdf_filepath = os.path.join(app.config['CHEM_RENDERS_FOLDER'], pdf_filename)
|
57 |
+
subprocess.run(['pdflatex', '-output-directory', app.config['CHEM_RENDERS_FOLDER'], tex_filepath], check=True)
|
58 |
+
|
59 |
+
# Convertir le PDF en SVG
|
60 |
+
svg_filename = tex_filename[:-4] + '.svg'
|
61 |
+
svg_filepath = os.path.join(app.config['CHEM_RENDERS_FOLDER'], svg_filename)
|
62 |
+
subprocess.run(['pdf2svg', pdf_filepath, svg_filepath], check=True)
|
63 |
+
|
64 |
+
# Remplacer le motif chemfig par la balise img
|
65 |
+
image_path = f'/chem_renders/{svg_filename}'
|
66 |
+
image_paths.append(image_path)
|
67 |
+
text = text.replace(f"\\chemfig{{{match}}}", f'<img src="{image_path}" style="max-width:100%; height:auto;" alt="Structure Chimique">')
|
68 |
+
|
69 |
+
# Supprimer les fichiers temporaires
|
70 |
+
os.remove(tex_filepath)
|
71 |
+
os.remove(pdf_filepath)
|
72 |
+
|
73 |
except Exception as e:
|
74 |
text = text.replace(f"\\chemfig{{{match}}}", f'Erreur lors du rendu de la structure : {e}')
|
75 |
|
|
|
102 |
generated_content = response.text
|
103 |
|
104 |
generated_content, image_paths = process_chemfig(generated_content)
|
|
|
105 |
return render_template('index.html', generated_content=generated_content, image_paths=image_paths)
|
106 |
|
107 |
+
# Routes pour servir les fichiers temporaires
|
108 |
@app.route('/temp_images/<filename>')
|
109 |
def temp_image(filename):
|
110 |
return send_file(os.path.join(app.config['UPLOAD_FOLDER'], filename), mimetype='image/png')
|
111 |
|
112 |
+
@app.route('/chem_renders/<filename>')
|
113 |
+
def chem_render(filename):
|
114 |
+
return send_file(os.path.join(app.config['CHEM_RENDERS_FOLDER'], filename), mimetype='image/svg+xml')
|
115 |
+
|
116 |
if __name__ == '__main__':
|
117 |
app.run(debug=True)
|