Spaces:
Running
Running
Ari
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -11,11 +11,9 @@ from reportlab.pdfgen import canvas
|
|
11 |
|
12 |
nltk.download('punkt')
|
13 |
|
14 |
-
# Load the models and tokenizers
|
15 |
tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn")
|
16 |
model = AutoModelForSeq2SeqLM.from_pretrained("facebook/bart-large-cnn")
|
17 |
|
18 |
-
# Convert DOCX to PDF using reportlab
|
19 |
def docx_to_pdf(docx_file, output_pdf="converted_doc.pdf"):
|
20 |
doc = Document(docx_file)
|
21 |
full_text = []
|
@@ -33,8 +31,7 @@ def docx_to_pdf(docx_file, output_pdf="converted_doc.pdf"):
|
|
33 |
pdf.save()
|
34 |
return output_pdf
|
35 |
|
36 |
-
#
|
37 |
-
def pdf_to_text(text, PDF, min_length=20):
|
38 |
try:
|
39 |
file_extension = os.path.splitext(PDF.name)[1].lower()
|
40 |
|
@@ -47,8 +44,8 @@ def pdf_to_text(text, PDF, min_length=20):
|
|
47 |
inputs = tokenizer([text], max_length=1024, truncation=True, return_tensors="pt")
|
48 |
min_length = int(min_length)
|
49 |
|
50 |
-
summary_ids = model.generate(inputs["input_ids"], num_beams=2, min_length=min_length, max_length=min_length+
|
51 |
-
output_text = tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
52 |
|
53 |
pdf = FPDF()
|
54 |
pdf.add_page()
|
@@ -66,21 +63,19 @@ def pdf_to_text(text, PDF, min_length=20):
|
|
66 |
except Exception as e:
|
67 |
return None, f"An error occurred: {str(e)}", None
|
68 |
|
69 |
-
|
70 |
-
def process_sample_document(min_length=20):
|
71 |
sample_document_path = "Marbury v. Madison.pdf"
|
72 |
|
73 |
with open(sample_document_path, "rb") as f:
|
74 |
return pdf_to_text("", f, min_length)
|
75 |
|
76 |
-
# Gradio interface
|
77 |
with gr.Blocks() as iface:
|
78 |
with gr.Row():
|
79 |
-
process_sample_button = gr.Button("Summarize
|
80 |
|
81 |
text_input = gr.Textbox(label="Input Text")
|
82 |
file_input = gr.File(label="Upload PDF or DOCX")
|
83 |
-
slider = gr.Slider(minimum=10, maximum=
|
84 |
|
85 |
audio_output = gr.Audio(label="Generated Audio")
|
86 |
summary_output = gr.Textbox(label="Generated Summary")
|
|
|
11 |
|
12 |
nltk.download('punkt')
|
13 |
|
|
|
14 |
tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn")
|
15 |
model = AutoModelForSeq2SeqLM.from_pretrained("facebook/bart-large-cnn")
|
16 |
|
|
|
17 |
def docx_to_pdf(docx_file, output_pdf="converted_doc.pdf"):
|
18 |
doc = Document(docx_file)
|
19 |
full_text = []
|
|
|
31 |
pdf.save()
|
32 |
return output_pdf
|
33 |
|
34 |
+
def pdf_to_text(text, PDF, min_length=80): # Increase default min_length by 4 times
|
|
|
35 |
try:
|
36 |
file_extension = os.path.splitext(PDF.name)[1].lower()
|
37 |
|
|
|
44 |
inputs = tokenizer([text], max_length=1024, truncation=True, return_tensors="pt")
|
45 |
min_length = int(min_length)
|
46 |
|
47 |
+
summary_ids = model.generate(inputs["input_ids"], num_beams=2, min_length=min_length, max_length=min_length + 4000)
|
48 |
+
output_text = tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0] # Set explicitly
|
49 |
|
50 |
pdf = FPDF()
|
51 |
pdf.add_page()
|
|
|
63 |
except Exception as e:
|
64 |
return None, f"An error occurred: {str(e)}", None
|
65 |
|
66 |
+
def process_sample_document(min_length=80):
|
|
|
67 |
sample_document_path = "Marbury v. Madison.pdf"
|
68 |
|
69 |
with open(sample_document_path, "rb") as f:
|
70 |
return pdf_to_text("", f, min_length)
|
71 |
|
|
|
72 |
with gr.Blocks() as iface:
|
73 |
with gr.Row():
|
74 |
+
process_sample_button = gr.Button("Summarize Marbury v. Madison Case Pre-Uploaded")
|
75 |
|
76 |
text_input = gr.Textbox(label="Input Text")
|
77 |
file_input = gr.File(label="Upload PDF or DOCX")
|
78 |
+
slider = gr.Slider(minimum=10, maximum=400, step=10, value=80, label="Summary Minimum Length") # Default value set to 80
|
79 |
|
80 |
audio_output = gr.Audio(label="Generated Audio")
|
81 |
summary_output = gr.Textbox(label="Generated Summary")
|