Daewolf commited on
Commit
231a9d3
·
verified ·
1 Parent(s): ac90d77

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -61
app.py CHANGED
@@ -1,61 +1,62 @@
1
- from flask import Flask, render_template, request, jsonify
2
- import PyPDF2
3
- import os
4
- import re
5
-
6
- app = Flask(__name__)
7
- UPLOAD_FOLDER = os.path.join('static', 'uploads')
8
- app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
9
-
10
- def nettoyer_texte(texte):
11
- if not texte:
12
- return ""
13
-
14
- # 1. Remplacer les retours à la ligne par des espaces
15
- texte = texte.replace('\n', ' ')
16
-
17
- # 2. Supprimer les numéros de page isolés (ex: " 12 ", " Page 12 ")
18
- # Cette regex cherche des chiffres entourés d'espaces ou en début de ligne
19
- texte = re.sub(r'\b\d+\b', '', texte)
20
-
21
- # 3. Supprimer les espaces multiples créés par le nettoyage
22
- texte = re.sub(r'\s+', ' ', texte).strip()
23
-
24
- return texte
25
-
26
- @app.route("/")
27
- def index():
28
- return render_template("index.html")
29
-
30
- @app.route("/upload", methods=["POST"])
31
- def upload_pdf():
32
- if 'file' not in request.files:
33
- return jsonify({"error": "Aucun fichier"})
34
- file = request.files['file']
35
- if file:
36
- if not os.path.exists(app.config['UPLOAD_FOLDER']):
37
- os.makedirs(app.config['UPLOAD_FOLDER'])
38
- filepath = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
39
- file.save(filepath)
40
-
41
- pages_data = []
42
- try:
43
- with open(filepath, 'rb') as f:
44
- reader = PyPDF2.PdfReader(f)
45
- for i, page in enumerate(reader.pages):
46
- t = page.extract_text()
47
- # On applique le nettoyage ici !
48
- t_propre = nettoyer_texte(t)
49
-
50
- if t_propre:
51
- pages_data.append({"num": i + 1, "texte": t_propre})
52
-
53
- return jsonify({
54
- "pdf_url": f"/static/uploads/{file.filename}",
55
- "pages": pages_data
56
- })
57
- except Exception as e:
58
- return jsonify({"error": str(e)})
59
-
60
- if __name__ == "__main__":
61
- app.run(debug=True)
 
 
1
+ from flask import Flask, render_template, request, jsonify
2
+ import PyPDF2
3
+ import os
4
+ import re
5
+
6
+ app = Flask(__name__)
7
+ UPLOAD_FOLDER = os.path.join('static', 'uploads')
8
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
9
+
10
+ def nettoyer_texte(texte):
11
+ if not texte:
12
+ return ""
13
+
14
+ # 1. Remplacer les retours à la ligne par des espaces
15
+ texte = texte.replace('\n', ' ')
16
+
17
+ # 2. Supprimer les numéros de page isolés (ex: " 12 ", " Page 12 ")
18
+ # Cette regex cherche des chiffres entourés d'espaces ou en début de ligne
19
+ texte = re.sub(r'\b\d+\b', '', texte)
20
+
21
+ # 3. Supprimer les espaces multiples créés par le nettoyage
22
+ texte = re.sub(r'\s+', ' ', texte).strip()
23
+
24
+ return texte
25
+
26
+ @app.route("/")
27
+ def index():
28
+ return render_template("index.html")
29
+
30
+ @app.route("/upload", methods=["POST"])
31
+ def upload_pdf():
32
+ if 'file' not in request.files:
33
+ return jsonify({"error": "Aucun fichier"})
34
+ file = request.files['file']
35
+ if file:
36
+ if not os.path.exists(app.config['UPLOAD_FOLDER']):
37
+ os.makedirs(app.config['UPLOAD_FOLDER'])
38
+ filepath = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
39
+ file.save(filepath)
40
+
41
+ pages_data = []
42
+ try:
43
+ with open(filepath, 'rb') as f:
44
+ reader = PyPDF2.PdfReader(f)
45
+ for i, page in enumerate(reader.pages):
46
+ t = page.extract_text()
47
+ # On applique le nettoyage ici !
48
+ t_propre = nettoyer_texte(t)
49
+
50
+ if t_propre:
51
+ pages_data.append({"num": i + 1, "texte": t_propre})
52
+
53
+ return jsonify({
54
+ "pdf_url": f"/static/uploads/{file.filename}",
55
+ "pages": pages_data
56
+ })
57
+ except Exception as e:
58
+ return jsonify({"error": str(e)})
59
+
60
+ if __name__ == "__main__":
61
+ # On force le port 7860 et l'hôte 0.0.0.0 pour Hugging Face
62
+ app.run(host="0.0.0.0", port=7860, debug=False)