Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ from PIL import Image
|
|
6 |
import io
|
7 |
import base64
|
8 |
import json
|
|
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
|
@@ -23,6 +24,50 @@ def index():
|
|
23 |
def indexx():
|
24 |
return render_template('maj.html')
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
@app.route('/solve', methods=['POST'])
|
27 |
def solve():
|
28 |
try:
|
@@ -40,13 +85,13 @@ def solve():
|
|
40 |
model="gemini-2.5-pro-exp-03-25",
|
41 |
contents=[
|
42 |
{'inline_data': {'mime_type': 'image/png', 'data': img_str}},
|
43 |
-
"""Résous
|
44 |
-
Présente ta réponse de manière claire et structurée."""
|
45 |
],
|
46 |
config=types.GenerateContentConfig(
|
47 |
thinking_config=types.ThinkingConfig(
|
48 |
thinking_budget=8000
|
49 |
),
|
|
|
50 |
tools=[types.Tool(
|
51 |
code_execution=types.ToolCodeExecution
|
52 |
)]
|
@@ -55,45 +100,28 @@ def solve():
|
|
55 |
|
56 |
for chunk in response:
|
57 |
for part in chunk.candidates[0].content.parts:
|
58 |
-
# Gestion des modes (thinking/answering)
|
59 |
if hasattr(part, 'thought') and part.thought:
|
60 |
if mode != "thinking":
|
61 |
yield f'data: {json.dumps({"mode": "thinking"})}\n\n'
|
62 |
mode = "thinking"
|
63 |
-
|
64 |
-
elif part.text is not None:
|
65 |
if mode != "answering":
|
66 |
yield f'data: {json.dumps({"mode": "answering"})}\n\n'
|
67 |
mode = "answering"
|
68 |
-
yield f'data: {json.dumps({"content": part.text})}\n\n'
|
69 |
|
70 |
-
# Gestion
|
71 |
-
|
72 |
-
|
73 |
-
yield f'data: {json.dumps({"mode": "answering"})}\n\n'
|
74 |
-
mode = "answering"
|
75 |
-
# Formater le code pour l'affichage
|
76 |
-
code_content = f"```python\n{part.executable_code.code}\n```"
|
77 |
-
yield f'data: {json.dumps({"content": code_content})}\n\n'
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
if mode != "answering":
|
82 |
-
yield f'data: {json.dumps({"mode": "answering"})}\n\n'
|
83 |
-
mode = "answering"
|
84 |
-
yield f'data: {json.dumps({"content": f"```\n{part.code_execution_result.output}\n```"})}\n\n'
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
image_data = part.inline_data.data
|
93 |
-
image_format = part.inline_data.mime_type.split('/')[-1]
|
94 |
-
image_src = f"data:{part.inline_data.mime_type};base64,{image_data}"
|
95 |
-
image_tag = f'<img src="{image_src}" alt="Generated Image" style="max-width:100%;">'
|
96 |
-
yield f'data: {json.dumps({"content": image_tag})}\n\n'
|
97 |
|
98 |
except Exception as e:
|
99 |
print(f"Error during generation: {e}")
|
@@ -113,7 +141,6 @@ def solve():
|
|
113 |
|
114 |
@app.route('/solved', methods=['POST'])
|
115 |
def solved():
|
116 |
-
# Version similaire avec le modèle flash
|
117 |
try:
|
118 |
image_data = request.files['image'].read()
|
119 |
img = Image.open(io.BytesIO(image_data))
|
@@ -129,10 +156,10 @@ def solved():
|
|
129 |
model="gemini-2.5-flash-preview-04-17",
|
130 |
contents=[
|
131 |
{'inline_data': {'mime_type': 'image/png', 'data': img_str}},
|
132 |
-
"""Résous
|
133 |
-
Présente ta réponse de manière claire et structurée."""
|
134 |
],
|
135 |
config=types.GenerateContentConfig(
|
|
|
136 |
tools=[types.Tool(
|
137 |
code_execution=types.ToolCodeExecution
|
138 |
)]
|
@@ -141,45 +168,28 @@ def solved():
|
|
141 |
|
142 |
for chunk in response:
|
143 |
for part in chunk.candidates[0].content.parts:
|
144 |
-
# Gestion des modes (thinking/answering)
|
145 |
if hasattr(part, 'thought') and part.thought:
|
146 |
if mode != "thinking":
|
147 |
yield f'data: {json.dumps({"mode": "thinking"})}\n\n'
|
148 |
mode = "thinking"
|
149 |
-
|
150 |
-
elif part.text is not None:
|
151 |
if mode != "answering":
|
152 |
yield f'data: {json.dumps({"mode": "answering"})}\n\n'
|
153 |
mode = "answering"
|
154 |
-
yield f'data: {json.dumps({"content": part.text})}\n\n'
|
155 |
|
156 |
-
# Gestion
|
157 |
-
|
158 |
-
|
159 |
-
yield f'data: {json.dumps({"mode": "answering"})}\n\n'
|
160 |
-
mode = "answering"
|
161 |
-
# Formater le code pour l'affichage
|
162 |
-
code_content = f"```python\n{part.executable_code.code}\n```"
|
163 |
-
yield f'data: {json.dumps({"content": code_content})}\n\n'
|
164 |
|
165 |
-
|
166 |
-
|
167 |
-
if mode != "answering":
|
168 |
-
yield f'data: {json.dumps({"mode": "answering"})}\n\n'
|
169 |
-
mode = "answering"
|
170 |
-
yield f'data: {json.dumps({"content": f"```\n{part.code_execution_result.output}\n```"})}\n\n'
|
171 |
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
image_data = part.inline_data.data
|
179 |
-
image_format = part.inline_data.mime_type.split('/')[-1]
|
180 |
-
image_src = f"data:{part.inline_data.mime_type};base64,{image_data}"
|
181 |
-
image_tag = f'<img src="{image_src}" alt="Generated Image" style="max-width:100%;">'
|
182 |
-
yield f'data: {json.dumps({"content": image_tag})}\n\n'
|
183 |
|
184 |
except Exception as e:
|
185 |
print(f"Error during generation: {e}")
|
|
|
6 |
import io
|
7 |
import base64
|
8 |
import json
|
9 |
+
import re
|
10 |
|
11 |
app = Flask(__name__)
|
12 |
|
|
|
24 |
def indexx():
|
25 |
return render_template('maj.html')
|
26 |
|
27 |
+
def process_markdown_and_code(text):
|
28 |
+
"""Traite le texte pour identifier et formater le code et le markdown"""
|
29 |
+
# Convertit le texte en HTML formaté
|
30 |
+
# Cette fonction pourrait être étendue pour utiliser une bibliothèque de markdown
|
31 |
+
return text
|
32 |
+
|
33 |
+
def format_code_execution_result(response_parts):
|
34 |
+
"""Formate les résultats d'exécution de code pour l'affichage HTML"""
|
35 |
+
result = []
|
36 |
+
|
37 |
+
for part in response_parts:
|
38 |
+
# Traitement du texte (équivalent à display(Markdown(part.text)))
|
39 |
+
if hasattr(part, 'text') and part.text is not None:
|
40 |
+
result.append({
|
41 |
+
'type': 'markdown',
|
42 |
+
'content': part.text
|
43 |
+
})
|
44 |
+
|
45 |
+
# Traitement du code exécutable
|
46 |
+
if hasattr(part, 'executable_code') and part.executable_code is not None:
|
47 |
+
result.append({
|
48 |
+
'type': 'code',
|
49 |
+
'content': part.executable_code.code
|
50 |
+
})
|
51 |
+
|
52 |
+
# Traitement des résultats d'exécution
|
53 |
+
if hasattr(part, 'code_execution_result') and part.code_execution_result is not None:
|
54 |
+
result.append({
|
55 |
+
'type': 'execution_result',
|
56 |
+
'content': part.code_execution_result.output
|
57 |
+
})
|
58 |
+
|
59 |
+
# Traitement des images (équivalent à display(Image(data=part.inline_data.data)))
|
60 |
+
if hasattr(part, 'inline_data') and part.inline_data is not None:
|
61 |
+
# Encodage de l'image en base64 pour l'affichage HTML
|
62 |
+
img_data = base64.b64encode(part.inline_data.data).decode('utf-8')
|
63 |
+
result.append({
|
64 |
+
'type': 'image',
|
65 |
+
'content': img_data,
|
66 |
+
'format': 'png' # Supposé comme png par défaut
|
67 |
+
})
|
68 |
+
|
69 |
+
return result
|
70 |
+
|
71 |
@app.route('/solve', methods=['POST'])
|
72 |
def solve():
|
73 |
try:
|
|
|
85 |
model="gemini-2.5-pro-exp-03-25",
|
86 |
contents=[
|
87 |
{'inline_data': {'mime_type': 'image/png', 'data': img_str}},
|
88 |
+
"""Résous ça en français with rendering latex"""
|
|
|
89 |
],
|
90 |
config=types.GenerateContentConfig(
|
91 |
thinking_config=types.ThinkingConfig(
|
92 |
thinking_budget=8000
|
93 |
),
|
94 |
+
# Ajouter l'outil d'exécution de code
|
95 |
tools=[types.Tool(
|
96 |
code_execution=types.ToolCodeExecution
|
97 |
)]
|
|
|
100 |
|
101 |
for chunk in response:
|
102 |
for part in chunk.candidates[0].content.parts:
|
|
|
103 |
if hasattr(part, 'thought') and part.thought:
|
104 |
if mode != "thinking":
|
105 |
yield f'data: {json.dumps({"mode": "thinking"})}\n\n'
|
106 |
mode = "thinking"
|
107 |
+
else:
|
|
|
108 |
if mode != "answering":
|
109 |
yield f'data: {json.dumps({"mode": "answering"})}\n\n'
|
110 |
mode = "answering"
|
|
|
111 |
|
112 |
+
# Gestion des différents types de contenu
|
113 |
+
if hasattr(part, 'text') and part.text is not None:
|
114 |
+
yield f'data: {json.dumps({"content": part.text, "type": "text"})}\n\n'
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
+
if hasattr(part, 'executable_code') and part.executable_code is not None:
|
117 |
+
yield f'data: {json.dumps({"content": part.executable_code.code, "type": "code"})}\n\n'
|
|
|
|
|
|
|
|
|
118 |
|
119 |
+
if hasattr(part, 'code_execution_result') and part.code_execution_result is not None:
|
120 |
+
yield f'data: {json.dumps({"content": part.code_execution_result.output, "type": "result"})}\n\n'
|
121 |
+
|
122 |
+
if hasattr(part, 'inline_data') and part.inline_data is not None:
|
123 |
+
img_data = base64.b64encode(part.inline_data.data).decode('utf-8')
|
124 |
+
yield f'data: {json.dumps({"content": img_data, "type": "image"})}\n\n'
|
|
|
|
|
|
|
|
|
|
|
125 |
|
126 |
except Exception as e:
|
127 |
print(f"Error during generation: {e}")
|
|
|
141 |
|
142 |
@app.route('/solved', methods=['POST'])
|
143 |
def solved():
|
|
|
144 |
try:
|
145 |
image_data = request.files['image'].read()
|
146 |
img = Image.open(io.BytesIO(image_data))
|
|
|
156 |
model="gemini-2.5-flash-preview-04-17",
|
157 |
contents=[
|
158 |
{'inline_data': {'mime_type': 'image/png', 'data': img_str}},
|
159 |
+
"""Résous ça en français with rendering latex. utilise python pour les calculs"""
|
|
|
160 |
],
|
161 |
config=types.GenerateContentConfig(
|
162 |
+
# Ajouter l'outil d'exécution de code
|
163 |
tools=[types.Tool(
|
164 |
code_execution=types.ToolCodeExecution
|
165 |
)]
|
|
|
168 |
|
169 |
for chunk in response:
|
170 |
for part in chunk.candidates[0].content.parts:
|
|
|
171 |
if hasattr(part, 'thought') and part.thought:
|
172 |
if mode != "thinking":
|
173 |
yield f'data: {json.dumps({"mode": "thinking"})}\n\n'
|
174 |
mode = "thinking"
|
175 |
+
else:
|
|
|
176 |
if mode != "answering":
|
177 |
yield f'data: {json.dumps({"mode": "answering"})}\n\n'
|
178 |
mode = "answering"
|
|
|
179 |
|
180 |
+
# Gestion des différents types de contenu
|
181 |
+
if hasattr(part, 'text') and part.text is not None:
|
182 |
+
yield f'data: {json.dumps({"content": part.text, "type": "text"})}\n\n'
|
|
|
|
|
|
|
|
|
|
|
183 |
|
184 |
+
if hasattr(part, 'executable_code') and part.executable_code is not None:
|
185 |
+
yield f'data: {json.dumps({"content": part.executable_code.code, "type": "code"})}\n\n'
|
|
|
|
|
|
|
|
|
186 |
|
187 |
+
if hasattr(part, 'code_execution_result') and part.code_execution_result is not None:
|
188 |
+
yield f'data: {json.dumps({"content": part.code_execution_result.output, "type": "result"})}\n\n'
|
189 |
+
|
190 |
+
if hasattr(part, 'inline_data') and part.inline_data is not None:
|
191 |
+
img_data = base64.b64encode(part.inline_data.data).decode('utf-8')
|
192 |
+
yield f'data: {json.dumps({"content": img_data, "type": "image"})}\n\n'
|
|
|
|
|
|
|
|
|
|
|
193 |
|
194 |
except Exception as e:
|
195 |
print(f"Error during generation: {e}")
|