Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,68 @@
|
|
1 |
-
from flask import Flask,
|
2 |
-
import
|
3 |
-
import
|
|
|
4 |
import os
|
5 |
|
|
|
|
|
6 |
app = Flask(__name__)
|
7 |
|
8 |
-
#
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
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 |
-
|
25 |
-
|
26 |
-
|
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 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
@app.route("/")
|
65 |
-
def viewer():
|
66 |
-
return render_template("viewer.html") # Assurez-vous que viewer.html est dans le dossier templates/
|
67 |
|
68 |
-
|
69 |
-
if __name__ == "__main__":
|
70 |
app.run(debug=True)
|
|
|
1 |
+
from flask import Flask, render_template, redirect, url_for, request
|
2 |
+
import csv
|
3 |
+
import telegram
|
4 |
+
from dotenv import load_dotenv
|
5 |
import os
|
6 |
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
app = Flask(__name__)
|
10 |
|
11 |
+
# Configuration Telegram
|
12 |
+
TELEGRAM_TOKEN = "7126991043:AAEzeKswNo6eO7oJA49Hxn_bsbzgzUoJ-6A" # Votre jeton de bot Telegram
|
13 |
+
TELEGRAM_CHAT_ID = "-1002081124539" # L'identifiant du groupe
|
14 |
+
|
15 |
+
bot = telegram.Bot(token=TELEGRAM_TOKEN)
|
16 |
+
|
17 |
+
|
18 |
+
# Chargement du dataset de traductions
|
19 |
+
def load_translations(filename):
|
20 |
+
translations = []
|
21 |
+
with open(filename, 'r', encoding='utf-8') as file:
|
22 |
+
reader = csv.DictReader(file)
|
23 |
+
for i, row in enumerate(reader):
|
24 |
+
translations.append({
|
25 |
+
"id": i,
|
26 |
+
"fr": row["fr"],
|
27 |
+
"yi": row["yi"],
|
28 |
+
"likes": 0,
|
29 |
+
"dislikes": 0,
|
30 |
+
"feedback_sent": False
|
31 |
+
})
|
32 |
+
return translations
|
33 |
|
34 |
+
translations = load_translations('translations.csv') # Votre dataset au format csv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
|
|
|
|
|
|
36 |
|
37 |
+
@app.route('/')
|
38 |
+
def index():
|
39 |
+
return render_template('index.html', translations=translations)
|
|
|
|
|
|
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
@app.route('/vote/<int:id>/<string:action>')
|
43 |
+
def vote(id, action):
|
44 |
+
translation = next((t for t in translations if t["id"] == id), None)
|
45 |
+
if translation:
|
46 |
+
if action == "like":
|
47 |
+
translation["likes"] += 1
|
48 |
+
elif action == "dislike":
|
49 |
+
translation["dislikes"] += 1
|
50 |
+
return redirect(url_for('index'))
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
|
53 |
+
@app.route('/submit_feedback/<int:id>', methods=['POST'])
|
54 |
+
def submit_feedback(id):
|
55 |
+
translation = next((t for t in translations if t["id"] == id), None)
|
56 |
+
if translation and not translation["feedback_sent"]:
|
57 |
+
feedback = request.form['feedback']
|
58 |
+
message = f"Feedback sur la traduction #{translation['id']}:\n\n" \
|
59 |
+
f"Français: {translation['fr']}\n\n" \
|
60 |
+
f"Yipunu: {translation['yi']}\n\n" \
|
61 |
+
f"Avis de l'utilisateur:\n{feedback}"
|
62 |
+
bot.send_message(chat_id=TELEGRAM_CHAT_ID, text=message)
|
63 |
+
translation["feedback_sent"] = True
|
64 |
+
return redirect(url_for('index'))
|
65 |
|
|
|
|
|
|
|
66 |
|
67 |
+
if __name__ == '__main__':
|
|
|
68 |
app.run(debug=True)
|