Docfile commited on
Commit
f388c93
·
verified ·
1 Parent(s): 236c7d2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, jsonify
2
+ from google import genai
3
+ from google.genai import types
4
+ import os
5
+ from PIL import Image
6
+ import io
7
+ import base64
8
+
9
+ app = Flask(__name__)
10
+
11
+ # Remplacez par votre clé API réelle
12
+ GOOGLE_API_KEY = "YOUR_API_KEY"
13
+
14
+ client = genai.Client(
15
+ api_key=GOOGLE_API_KEY,
16
+ # Use `v1alpha` so you can see the `thought` flag.
17
+ http_options={'api_version': 'v1alpha'},
18
+ )
19
+
20
+ @app.route('/')
21
+ def index():
22
+ return render_template('index.html')
23
+
24
+ @app.route('/solve', methods=['POST'])
25
+ def solve():
26
+ try:
27
+ image_data = request.files['image'].read() # Lire les données binaires de l'image
28
+ img = Image.open(io.BytesIO(image_data))
29
+
30
+ # Convertir l'image en base64 pour l'envoyer à l'API Gemini
31
+ buffered = io.BytesIO()
32
+ img.save(buffered, format="PNG")
33
+ img_str = base64.b64encode(buffered.getvalue()).decode()
34
+
35
+ # Utiliser l'API Gemini pour générer la réponse
36
+ response = client.models.generate_content(
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
+ "What's the area of the overlapping region?"
42
+ ]
43
+ )
44
+
45
+ # Extraire les pensées et la réponse
46
+ thoughts = ""
47
+ answer = ""
48
+ for part in response.candidates[0].content.parts:
49
+ if part.thought:
50
+ thoughts += part.text + "\n"
51
+ else:
52
+ answer += part.text + "\n"
53
+
54
+ return jsonify({'thoughts': thoughts, 'answer': answer})
55
+
56
+ except Exception as e:
57
+ return jsonify({'error': str(e)}), 500
58
+
59
+ if __name__ == '__main__':
60
+ app.run(debug=True)