-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
96 lines (75 loc) · 2.98 KB
/
Copy pathapp.py
File metadata and controls
96 lines (75 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# from flask import Flask, render_template, request, flash, redirect, url_for
# from werkzeug.utils import secure_filename
# from summarizer import summarize_large_text, extract_text_from_pdf
# import os
# app = Flask(__name__)
# app.secret_key = "your_secret_key" # needed for flash messages
# UPLOAD_FOLDER = "uploads"
# ALLOWED_EXTENSIONS = {"pdf", "txt"}
# os.makedirs(UPLOAD_FOLDER, exist_ok=True)
# app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
# def allowed_file(filename):
# return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
# @app.route("/", methods=["GET", "POST"])
# def index():
# summary = ""
# if request.method == "POST":
# if "file" not in request.files:
# flash("No file part in the request")
# return redirect(request.url)
# file = request.files["file"]
# if file.filename == "":
# flash("No file selected")
# return redirect(request.url)
# if file and allowed_file(file.filename):
# filename = secure_filename(file.filename)
# path = os.path.join(app.config["UPLOAD_FOLDER"], filename)
# file.save(path)
# if filename.endswith(".pdf"):
# text = extract_text_from_pdf(path)
# elif filename.endswith(".txt"):
# with open(path, "r", encoding="utf-8") as f:
# text = f.read()
# else:
# flash("Unsupported file type")
# return redirect(request.url)
# summary = summarize_large_text(text)
# else:
# flash("Allowed file types are: pdf, txt")
# return redirect(request.url)
# return render_template("index.html", summary=summary)
# if __name__ == "__main__":
# app.run(debug=True)
from flask import Flask, render_template, request
from waitress import serve
from summarizer import extract_text_from_pdf, summarize_large_text
import os
app = Flask(__name__)
UPLOAD_FOLDER = 'uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/')
def index():
return render_template('index.html')
def home():
return "Hello from Waitress!"
if __name__ == '__main__':
serve(app, host='0.0.0.0', port=8080)
@app.route('/summarize', methods=['POST'])
def summarize():
print("Summarize route called!")
if 'file' not in request.files:
return "No file uploaded", 400
file = request.files['file']
if file.filename == '':
return "No file selected", 400
file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(file_path)
try:
text = extract_text_from_pdf(file_path)
summary = summarize_large_text(text)
finally:
if os.path.exists(file_path):
os.remove(file_path) # # Clean up only if file still exists
return render_template('index.html', summary=summary)
if __name__ == '__main__':
app.run(debug=True)