-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
64 lines (51 loc) · 1.94 KB
/
app.py
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
import os
from flask import Flask, render_template, request, jsonify
from werkzeug.utils import secure_filename
from QuickSumm import summarize_text
import chardet
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'txt'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def extract_text_from_file(filepath, file_extension):
if file_extension == 'txt':
with open(filepath, 'rb') as f:
raw_data = f.read()
result = chardet.detect(raw_data)
encoding = result['encoding']
text = raw_data.decode(encoding)
else:
text = ""
return text
@app.route('/')
def home():
return render_template('QuickSumm.html')
@app.route('/summarize', methods=['POST'])
def summarization():
if request.is_json:
data = request.get_json()
text = data.get('input_text', '')
summary = summarize_text(text)
return jsonify({'summary': summary})
return jsonify({'error': 'Invalid request'}), 400
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return jsonify({'error': 'No file part'})
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'})
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
file_extension = filename.rsplit('.', 1)[1].lower()
text = extract_text_from_file(filepath, file_extension)
return jsonify({'text': text})
return jsonify({'error': 'Invalid file type'}), 400
if __name__ == '__main__':
app.run(debug=True)