Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,85 +1,70 @@
|
|
1 |
-
from flask import Flask,
|
2 |
-
import
|
3 |
-
import
|
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 |
-
#
|
11 |
-
|
12 |
-
"temperature": 1,
|
13 |
-
"max_output_tokens": 8192,
|
14 |
-
}
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
22 |
|
23 |
-
|
|
|
|
|
24 |
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
#
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
model_name="gemini-1.5-pro-002",
|
32 |
-
generation_config=generation_config,
|
33 |
-
safety_settings=safety_settings
|
34 |
-
)
|
35 |
try:
|
36 |
-
|
37 |
-
|
|
|
38 |
except Exception as e:
|
39 |
-
return str(e)
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
#
|
42 |
-
|
|
|
|
|
43 |
try:
|
44 |
-
|
45 |
-
|
46 |
-
|
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 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
@app.route('/upload', methods=['POST'])
|
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 |
-
|
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 |
-
|
79 |
-
|
|
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
1 |
+
from flask import Flask, send_file, jsonify
|
2 |
+
import fitz # PyMuPDF pour manipuler le PDF
|
3 |
+
import tempfile
|
4 |
import os
|
|
|
|
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
|
8 |
+
# Chemin vers le PDF source
|
9 |
+
PDF_FILE = "static/pdf/large_file.pdf"
|
|
|
|
|
|
|
10 |
|
11 |
+
# Fonction pour générer une image temporaire d'une page du PDF
|
12 |
+
def generate_temp_image(page_number, dpi):
|
13 |
+
try:
|
14 |
+
# Ouvre le PDF
|
15 |
+
with fitz.open(PDF_FILE) as doc:
|
16 |
+
# Vérifie que le numéro de page est valide
|
17 |
+
if page_number < 1 or page_number > len(doc):
|
18 |
+
raise ValueError(f"Numéro de page invalide : {page_number}")
|
19 |
|
20 |
+
# Extrait la page et génère une image
|
21 |
+
page = doc[page_number - 1]
|
22 |
+
pixmap = page.get_pixmap(dpi=dpi)
|
23 |
|
24 |
+
# Crée un fichier temporaire pour stocker l'image
|
25 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
|
26 |
+
pixmap.save(temp_file.name)
|
27 |
+
return temp_file.name
|
28 |
+
except Exception as e:
|
29 |
+
raise RuntimeError(f"Erreur lors de la génération de l'image : {e}")
|
30 |
|
31 |
+
# Endpoint pour récupérer une image basse résolution
|
32 |
+
@app.route("/low_res/<int:page_number>")
|
33 |
+
def get_low_res(page_number):
|
34 |
+
temp_path = None
|
|
|
|
|
|
|
|
|
35 |
try:
|
36 |
+
# Génère une image basse résolution
|
37 |
+
temp_path = generate_temp_image(page_number, dpi=72)
|
38 |
+
return send_file(temp_path, mimetype="image/jpeg")
|
39 |
except Exception as e:
|
40 |
+
return jsonify({"error": str(e)}), 500
|
41 |
+
finally:
|
42 |
+
# Supprime le fichier temporaire après utilisation
|
43 |
+
if temp_path and os.path.exists(temp_path):
|
44 |
+
os.remove(temp_path)
|
45 |
|
46 |
+
# Endpoint pour récupérer une image haute résolution
|
47 |
+
@app.route("/high_res/<int:page_number>")
|
48 |
+
def get_high_res(page_number):
|
49 |
+
temp_path = None
|
50 |
try:
|
51 |
+
# Génère une image haute résolution
|
52 |
+
temp_path = generate_temp_image(page_number, dpi=300)
|
53 |
+
return send_file(temp_path, mimetype="image/jpeg")
|
|
|
|
|
|
|
|
|
|
|
54 |
except Exception as e:
|
55 |
+
return jsonify({"error": str(e)}), 500
|
56 |
+
finally:
|
57 |
+
# Supprime le fichier temporaire après utilisation
|
58 |
+
if temp_path and os.path.exists(temp_path):
|
59 |
+
os.remove(temp_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
|
|
|
|
61 |
|
62 |
+
from flask import render_template
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
+
@app.route("/viewer")
|
65 |
+
def viewer():
|
66 |
+
return render_template("viewer.html") # Assurez-vous que viewer.html est dans le dossier templates/
|
67 |
|
68 |
+
# Point d'entrée de l'application
|
69 |
+
if __name__ == "__main__":
|
70 |
+
app.run(debug=True)
|
|
|
|