Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,38 +3,55 @@ 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 |
-
|
12 |
-
|
13 |
}
|
14 |
|
15 |
safety_settings = [
|
16 |
-
|
17 |
-
"category": "
|
18 |
-
"threshold": "BLOCK_NONE"
|
19 |
-
|
20 |
-
{
|
21 |
-
"category": "HARM_CATEGORY_HATE_SPEECH",
|
22 |
-
"threshold": "BLOCK_NONE"
|
23 |
-
},
|
24 |
-
{
|
25 |
-
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
26 |
-
"threshold": "BLOCK_NONE"
|
27 |
-
},
|
28 |
-
{
|
29 |
-
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
30 |
-
"threshold": "BLOCK_NONE"
|
31 |
-
},
|
32 |
]
|
33 |
|
34 |
GOOGLE_API_KEY = os.environ.get("TOKEN")
|
35 |
|
36 |
genai.configure(api_key=GOOGLE_API_KEY)
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
@app.route('/')
|
39 |
def index():
|
40 |
return render_template('math.html')
|
@@ -43,41 +60,26 @@ def index():
|
|
43 |
def upload_image():
|
44 |
if 'image' not in request.files:
|
45 |
return jsonify({'error': 'Aucune image fournie'}), 400
|
46 |
-
|
47 |
file = request.files['image']
|
48 |
-
|
|
|
49 |
if file.filename == '':
|
50 |
return jsonify({'error': 'Aucun fichier sélectionné'}), 400
|
51 |
|
52 |
-
# Créer un fichier temporaire pour stocker l'image
|
53 |
with NamedTemporaryFile(delete=False) as temp_file:
|
54 |
file.save(temp_file.name)
|
55 |
-
|
56 |
-
# Ouvrir l'image avec Pillow
|
57 |
-
img = PIL.Image.open(temp_file.name)
|
58 |
-
|
59 |
-
# Préparer le prompt
|
60 |
-
prompt = """Résous ce problème mathématiques. Je veux qu'en réponse tu me donnes un rendu complet en utilisant du Latex."""
|
61 |
-
|
62 |
-
# Initialiser le modèle Gemini
|
63 |
-
model = genai.GenerativeModel(
|
64 |
-
model_name="gemini-1.5-pro-002",
|
65 |
-
generation_config=generation_config,
|
66 |
-
safety_settings=safety_settings
|
67 |
-
)
|
68 |
-
|
69 |
try:
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
# Supprimer le fichier temporaire
|
77 |
os.unlink(temp_file.name)
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
except Exception as e:
|
82 |
return jsonify({'error': str(e)}), 500
|
83 |
-
|
|
|
|
|
|
3 |
import google.generativeai as genai
|
4 |
import os
|
5 |
from tempfile import NamedTemporaryFile
|
6 |
+
from gradio_client import Client, handle_file # Importez gradio_client
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
|
10 |
# Configuration de Gemini
|
11 |
generation_config = {
|
12 |
+
"temperature": 1,
|
13 |
+
"max_output_tokens": 8192,
|
14 |
}
|
15 |
|
16 |
safety_settings = [
|
17 |
+
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
18 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
19 |
+
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
|
20 |
+
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
]
|
22 |
|
23 |
GOOGLE_API_KEY = os.environ.get("TOKEN")
|
24 |
|
25 |
genai.configure(api_key=GOOGLE_API_KEY)
|
26 |
|
27 |
+
# Fonction pour interroger Gemini
|
28 |
+
def query_gemini(image_path, prompt="Résous ce problème mathématiques. Je veux qu'en réponse tu me donnes un rendu complet en utilisant du Latex."):
|
29 |
+
img = PIL.Image.open(image_path)
|
30 |
+
model = genai.GenerativeModel(
|
31 |
+
model_name="gemini-1.5-pro-002",
|
32 |
+
generation_config=generation_config,
|
33 |
+
safety_settings=safety_settings
|
34 |
+
)
|
35 |
+
try:
|
36 |
+
response = model.generate_content([prompt, img], request_options={"timeout": 600})
|
37 |
+
return response.text
|
38 |
+
except Exception as e:
|
39 |
+
return str(e)
|
40 |
+
|
41 |
+
# Fonction pour interroger Qwen2
|
42 |
+
def query_qwen2(image_path, question="Résous ce problème mathématiques. Donne la réponse en utilisant LaTeX."):
|
43 |
+
try:
|
44 |
+
client = Client("Qwen/Qwen2-Math-Demo")
|
45 |
+
result = client.predict(
|
46 |
+
image=handle_file(image_path),
|
47 |
+
sketchpad=None,
|
48 |
+
question=question,
|
49 |
+
api_name="/math_chat_bot"
|
50 |
+
)
|
51 |
+
return result
|
52 |
+
except Exception as e:
|
53 |
+
return str(e)
|
54 |
+
|
55 |
@app.route('/')
|
56 |
def index():
|
57 |
return render_template('math.html')
|
|
|
60 |
def upload_image():
|
61 |
if 'image' not in request.files:
|
62 |
return jsonify({'error': 'Aucune image fournie'}), 400
|
63 |
+
|
64 |
file = request.files['image']
|
65 |
+
model_choice = request.form.get('model_choice', 'gemini') # Obtient le choix du modèle
|
66 |
+
|
67 |
if file.filename == '':
|
68 |
return jsonify({'error': 'Aucun fichier sélectionné'}), 400
|
69 |
|
|
|
70 |
with NamedTemporaryFile(delete=False) as temp_file:
|
71 |
file.save(temp_file.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
try:
|
73 |
+
if model_choice == "mariam's":
|
74 |
+
result = query_gemini(temp_file.name)
|
75 |
+
else:
|
76 |
+
result = query_qwen2(temp_file.name)
|
77 |
+
|
|
|
|
|
78 |
os.unlink(temp_file.name)
|
79 |
+
return jsonify({'result': result, 'model': model_choice})
|
80 |
+
|
|
|
81 |
except Exception as e:
|
82 |
return jsonify({'error': str(e)}), 500
|
83 |
+
|
84 |
+
|
85 |
+
|