-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
71 lines (54 loc) · 2.2 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
65
66
67
68
69
70
71
import os
from flask import Flask, request, render_template, jsonify
import pdfplumber
from transformers import pipeline
import torch
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size
# Ensure upload directory exists
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
# Initialize the summarization pipeline
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
def extract_text_from_pdf(pdf_path):
text = ""
with pdfplumber.open(pdf_path) as pdf:
for page in pdf.pages:
text += page.extract_text() or ""
return text
def generate_review(text):
# Split text into chunks that fit within model's max token limit
max_chunk_length = 1024
chunks = [text[i:i + max_chunk_length] for i in range(0, len(text), max_chunk_length)]
summaries = []
for chunk in chunks:
if len(chunk.strip()) > 100: # Only process chunks with substantial content
summary = summarizer(chunk, max_length=130, min_length=30, do_sample=False)
summaries.append(summary[0]['summary_text'])
return " ".join(summaries)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
if file and file.filename.lower().endswith('.pdf'):
filepath = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(filepath)
try:
# Extract text from PDF
text = extract_text_from_pdf(filepath)
# Generate review
review = generate_review(text)
# Clean up
os.remove(filepath)
return jsonify({'review': review})
except Exception as e:
return jsonify({'error': str(e)}), 500
return jsonify({'error': 'Invalid file type'}), 400
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)