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,16 +85,15 @@ 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 |
-
Si nécessaire, utilise du code Python pour effectuer les calculs complexes.
|
45 |
-
Présente ta solution de façon claire et espacée."""
|
46 |
],
|
47 |
config=types.GenerateContentConfig(
|
48 |
thinking_config=types.ThinkingConfig(
|
49 |
thinking_budget=8000
|
50 |
),
|
|
|
51 |
tools=[types.Tool(
|
52 |
-
code_execution=types.ToolCodeExecution
|
53 |
)]
|
54 |
)
|
55 |
)
|
@@ -58,32 +102,30 @@ def solve():
|
|
58 |
for part in chunk.candidates[0].content.parts:
|
59 |
if hasattr(part, 'thought') and part.thought:
|
60 |
if mode != "thinking":
|
61 |
-
yield 'data:
|
62 |
mode = "thinking"
|
63 |
-
elif hasattr(part, 'executable_code') and part.executable_code:
|
64 |
-
if mode != "executing_code":
|
65 |
-
yield 'data: ' + json.dumps({"mode": "executing_code"}) + '\n\n'
|
66 |
-
mode = "executing_code"
|
67 |
-
code_block_open = "```python\n"
|
68 |
-
code_block_close = "\n```"
|
69 |
-
yield 'data: ' + json.dumps({"content": code_block_open + part.executable_code.code + code_block_close}) + '\n\n'
|
70 |
-
elif hasattr(part, 'code_execution_result') and part.code_execution_result:
|
71 |
-
if mode != "code_result":
|
72 |
-
yield 'data: ' + json.dumps({"mode": "code_result"}) + '\n\n'
|
73 |
-
mode = "code_result"
|
74 |
-
result_block_open = "Résultat d'exécution:\n```\n"
|
75 |
-
result_block_close = "\n```"
|
76 |
-
yield 'data: ' + json.dumps({"content": result_block_open + part.code_execution_result.output + result_block_close}) + '\n\n'
|
77 |
else:
|
78 |
if mode != "answering":
|
79 |
-
yield 'data:
|
80 |
mode = "answering"
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
except Exception as e:
|
85 |
print(f"Error during generation: {e}")
|
86 |
-
yield 'data:
|
87 |
|
88 |
return Response(
|
89 |
stream_with_context(generate()),
|
@@ -114,13 +156,12 @@ def solved():
|
|
114 |
model="gemini-2.5-flash-preview-04-17",
|
115 |
contents=[
|
116 |
{'inline_data': {'mime_type': 'image/png', 'data': img_str}},
|
117 |
-
"""Résous
|
118 |
-
Si nécessaire, utilise du code Python pour effectuer les calculs complexes.
|
119 |
-
Présente ta solution de façon claire et espacée."""
|
120 |
],
|
121 |
config=types.GenerateContentConfig(
|
|
|
122 |
tools=[types.Tool(
|
123 |
-
code_execution=types.ToolCodeExecution
|
124 |
)]
|
125 |
)
|
126 |
)
|
@@ -129,32 +170,30 @@ def solved():
|
|
129 |
for part in chunk.candidates[0].content.parts:
|
130 |
if hasattr(part, 'thought') and part.thought:
|
131 |
if mode != "thinking":
|
132 |
-
yield 'data:
|
133 |
mode = "thinking"
|
134 |
-
elif hasattr(part, 'executable_code') and part.executable_code:
|
135 |
-
if mode != "executing_code":
|
136 |
-
yield 'data: ' + json.dumps({"mode": "executing_code"}) + '\n\n'
|
137 |
-
mode = "executing_code"
|
138 |
-
code_block_open = "```python\n"
|
139 |
-
code_block_close = "\n```"
|
140 |
-
yield 'data: ' + json.dumps({"content": code_block_open + part.executable_code.code + code_block_close}) + '\n\n'
|
141 |
-
elif hasattr(part, 'code_execution_result') and part.code_execution_result:
|
142 |
-
if mode != "code_result":
|
143 |
-
yield 'data: ' + json.dumps({"mode": "code_result"}) + '\n\n'
|
144 |
-
mode = "code_result"
|
145 |
-
result_block_open = "Résultat d'exécution:\n```\n"
|
146 |
-
result_block_close = "\n```"
|
147 |
-
yield 'data: ' + json.dumps({"content": result_block_open + part.code_execution_result.output + result_block_close}) + '\n\n'
|
148 |
else:
|
149 |
if mode != "answering":
|
150 |
-
yield 'data:
|
151 |
mode = "answering"
|
152 |
-
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
|
155 |
except Exception as e:
|
156 |
print(f"Error during generation: {e}")
|
157 |
-
yield 'data:
|
158 |
|
159 |
return Response(
|
160 |
stream_with_context(generate()),
|
|
|
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 |
)]
|
98 |
)
|
99 |
)
|
|
|
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}")
|
128 |
+
yield f'data: {json.dumps({"error": str(e)})}\n\n'
|
129 |
|
130 |
return Response(
|
131 |
stream_with_context(generate()),
|
|
|
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"""
|
|
|
|
|
160 |
],
|
161 |
config=types.GenerateContentConfig(
|
162 |
+
# Ajouter l'outil d'exécution de code
|
163 |
tools=[types.Tool(
|
164 |
+
code_execution=types.ToolCodeExecution
|
165 |
)]
|
166 |
)
|
167 |
)
|
|
|
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}")
|
196 |
+
yield f'data: {json.dumps({"error": str(e)})}\n\n'
|
197 |
|
198 |
return Response(
|
199 |
stream_with_context(generate()),
|