Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, render_template, jsonify
|
2 |
+
import PIL.Image
|
3 |
+
import google.generativeai as genai
|
4 |
+
import os
|
5 |
+
from tempfile import NamedTemporaryFile
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
|
9 |
+
# Configuration de Gemini
|
10 |
+
generation_config = {
|
11 |
+
"temperature": 1,
|
12 |
+
"top_p": 0.95,
|
13 |
+
"top_k": 64,
|
14 |
+
"max_output_tokens": 8192,
|
15 |
+
}
|
16 |
+
|
17 |
+
safety_settings = [
|
18 |
+
{
|
19 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
20 |
+
"threshold": "BLOCK_NONE"
|
21 |
+
},
|
22 |
+
{
|
23 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
24 |
+
"threshold": "BLOCK_NONE"
|
25 |
+
},
|
26 |
+
{
|
27 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
28 |
+
"threshold": "BLOCK_NONE"
|
29 |
+
},
|
30 |
+
{
|
31 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
32 |
+
"threshold": "BLOCK_NONE"
|
33 |
+
},
|
34 |
+
]
|
35 |
+
|
36 |
+
GOOGLE_API_KEY = os.environ.get("TOKEN")
|
37 |
+
|
38 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
39 |
+
|
40 |
+
@app.route('/')
|
41 |
+
def index():
|
42 |
+
return render_template('math.html')
|
43 |
+
|
44 |
+
@app.route('/upload', methods=['POST'])
|
45 |
+
def upload_image():
|
46 |
+
if 'image' not in request.files:
|
47 |
+
return jsonify({'error': 'Aucune image fournie'}), 400
|
48 |
+
|
49 |
+
file = request.files['image']
|
50 |
+
|
51 |
+
if file.filename == '':
|
52 |
+
return jsonify({'error': 'Aucun fichier sélectionné'}), 400
|
53 |
+
|
54 |
+
# Créer un fichier temporaire pour stocker l'image
|
55 |
+
with NamedTemporaryFile(delete=False) as temp_file:
|
56 |
+
file.save(temp_file.name)
|
57 |
+
|
58 |
+
# Ouvrir l'image avec Pillow
|
59 |
+
img = PIL.Image.open(temp_file.name)
|
60 |
+
|
61 |
+
# Préparer le prompt
|
62 |
+
prompt = """Résous ce problème mathématiques. Je veux qu'en réponse tu me donnes un rendu html complet en intégrant MathJax et plotly.js."""
|
63 |
+
|
64 |
+
# Initialiser le modèle Gemini
|
65 |
+
model = genai.GenerativeModel(
|
66 |
+
model_name="gemini-1.5-flash-002",
|
67 |
+
generation_config=generation_config,
|
68 |
+
safety_settings=safety_settings
|
69 |
+
)
|
70 |
+
|
71 |
+
try:
|
72 |
+
# Générer la réponse
|
73 |
+
response = model.generate_content(
|
74 |
+
[prompt, img],
|
75 |
+
request_options={"timeout": 600}
|
76 |
+
)
|
77 |
+
|
78 |
+
# Supprimer le fichier temporaire
|
79 |
+
os.unlink(temp_file.name)
|
80 |
+
|
81 |
+
return jsonify({'result': response.text})
|
82 |
+
|
83 |
+
except Exception as e:
|
84 |
+
return jsonify({'error': str(e)}), 500
|
85 |
+
|