Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,15 +6,14 @@ from PIL import Image
|
|
| 6 |
import io
|
| 7 |
import base64
|
| 8 |
import time
|
|
|
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
|
| 12 |
-
# Remplacez par votre clé API réelle
|
| 13 |
GOOGLE_API_KEY = "AIzaSyC_zxN9IHjEAxIoshWPzMfgb9qwMsu5t5Y"
|
| 14 |
|
| 15 |
client = genai.Client(
|
| 16 |
api_key=GOOGLE_API_KEY,
|
| 17 |
-
# Use `v1alpha` so you can see the `thought` flag.
|
| 18 |
http_options={'api_version': 'v1alpha'},
|
| 19 |
)
|
| 20 |
|
|
@@ -32,29 +31,37 @@ def solve():
|
|
| 32 |
img.save(buffered, format="PNG")
|
| 33 |
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 34 |
|
| 35 |
-
response_stream = client.models.generate_content_stream(
|
| 36 |
-
model="gemini-2.0-flash-thinking-exp-01-21",
|
| 37 |
-
config={'thinking_config': {'include_thoughts': True}},
|
| 38 |
-
contents=[
|
| 39 |
-
{'inline_data': {'mime_type': 'image/png', 'data': img_str}},
|
| 40 |
-
"Résous ce problème?"
|
| 41 |
-
]
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
def generate():
|
| 45 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
for chunk in response_stream:
|
| 47 |
for part in chunk.candidates[0].content.parts:
|
| 48 |
-
if part.thought:
|
| 49 |
-
yield f
|
| 50 |
else:
|
| 51 |
-
yield f
|
| 52 |
-
|
|
|
|
| 53 |
except Exception as e:
|
| 54 |
print(f"Error during generation: {e}")
|
| 55 |
-
yield f
|
| 56 |
|
| 57 |
-
return Response(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
return jsonify({'error': str(e)}), 500
|
|
|
|
| 6 |
import io
|
| 7 |
import base64
|
| 8 |
import time
|
| 9 |
+
import json
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
| 12 |
|
|
|
|
| 13 |
GOOGLE_API_KEY = "AIzaSyC_zxN9IHjEAxIoshWPzMfgb9qwMsu5t5Y"
|
| 14 |
|
| 15 |
client = genai.Client(
|
| 16 |
api_key=GOOGLE_API_KEY,
|
|
|
|
| 17 |
http_options={'api_version': 'v1alpha'},
|
| 18 |
)
|
| 19 |
|
|
|
|
| 31 |
img.save(buffered, format="PNG")
|
| 32 |
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def generate():
|
| 35 |
try:
|
| 36 |
+
response_stream = client.models.generate_content_stream(
|
| 37 |
+
model="gemini-2.0-flash-thinking-exp-01-21",
|
| 38 |
+
config={'thinking_config': {'include_thoughts': True}},
|
| 39 |
+
contents=[
|
| 40 |
+
{'inline_data': {'mime_type': 'image/png', 'data': img_str}},
|
| 41 |
+
"Résous ce problème?"
|
| 42 |
+
]
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
for chunk in response_stream:
|
| 46 |
for part in chunk.candidates[0].content.parts:
|
| 47 |
+
if hasattr(part, 'thought') and part.thought:
|
| 48 |
+
yield f'data: {json.dumps({"thought": part.text})}\n\n'
|
| 49 |
else:
|
| 50 |
+
yield f'data: {json.dumps({"answer": part.text})}\n\n'
|
| 51 |
+
time.sleep(0.1) # Légère pause pour contrôler le flux
|
| 52 |
+
|
| 53 |
except Exception as e:
|
| 54 |
print(f"Error during generation: {e}")
|
| 55 |
+
yield f'data: {json.dumps({"error": str(e)})}\n\n'
|
| 56 |
|
| 57 |
+
return Response(
|
| 58 |
+
stream_with_context(generate()),
|
| 59 |
+
mimetype='text/event-stream',
|
| 60 |
+
headers={
|
| 61 |
+
'Cache-Control': 'no-cache',
|
| 62 |
+
'X-Accel-Buffering': 'no'
|
| 63 |
+
}
|
| 64 |
+
)
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
return jsonify({'error': str(e)}), 500
|