Spaces:
Sleeping
Sleeping
Ari
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
-
import
|
4 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
5 |
from fpdf import FPDF
|
6 |
from gtts import gTTS
|
@@ -9,40 +9,23 @@ from docx import Document
|
|
9 |
from reportlab.lib.pagesizes import letter
|
10 |
from reportlab.pdfgen import canvas
|
11 |
|
12 |
-
|
13 |
-
tokenizer = AutoTokenizer.from_pretrained("nlpaueb/legal-bert-base-uncased")
|
14 |
-
model = AutoModelForSeq2SeqLM.from_pretrained("nlpaueb/legal-bert-base-uncased")
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
chunks = []
|
20 |
-
current_chunk = []
|
21 |
-
current_length = 0
|
22 |
-
|
23 |
-
for sentence in sentences:
|
24 |
-
tokens = tokenizer.tokenize(sentence)
|
25 |
-
if current_length + len(tokens) <= max_token_len:
|
26 |
-
current_chunk.append(sentence)
|
27 |
-
current_length += len(tokens)
|
28 |
-
else:
|
29 |
-
chunks.append(" ".join(current_chunk))
|
30 |
-
current_chunk = [sentence]
|
31 |
-
current_length = len(tokens)
|
32 |
-
|
33 |
-
if current_chunk:
|
34 |
-
chunks.append(" ".join(current_chunk))
|
35 |
-
|
36 |
-
return chunks
|
37 |
|
|
|
38 |
def docx_to_pdf(docx_file, output_pdf="converted_doc.pdf"):
|
39 |
doc = Document(docx_file)
|
40 |
-
full_text = [
|
|
|
|
|
41 |
|
42 |
pdf = canvas.Canvas(output_pdf, pagesize=letter)
|
43 |
pdf.setFont("Helvetica", 12)
|
44 |
-
text = pdf.beginText(40, 750)
|
45 |
|
|
|
46 |
for line in full_text:
|
47 |
text.textLine(line)
|
48 |
|
@@ -50,14 +33,8 @@ def docx_to_pdf(docx_file, output_pdf="converted_doc.pdf"):
|
|
50 |
pdf.save()
|
51 |
return output_pdf
|
52 |
|
53 |
-
#
|
54 |
-
def
|
55 |
-
inputs = tokenizer([chunk], max_length=512, truncation=True, return_tensors="pt")
|
56 |
-
summary_ids = model.generate(inputs["input_ids"], num_beams=2, min_length=min_length, max_length=max_length)
|
57 |
-
return tokenizer.decode(summary_ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
|
58 |
-
|
59 |
-
# Main processing function using recursive summarization
|
60 |
-
def pdf_to_text(text, PDF, min_length=50):
|
61 |
try:
|
62 |
file_extension = os.path.splitext(PDF.name)[1].lower()
|
63 |
|
@@ -67,32 +44,30 @@ def pdf_to_text(text, PDF, min_length=50):
|
|
67 |
elif file_extension == '.pdf' and text == "":
|
68 |
text = extract_text(PDF.name)
|
69 |
|
70 |
-
|
71 |
-
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
final_summary = summarize_chunk(summarized_text, min_length=min_length, max_length=min_length+150)
|
76 |
|
77 |
-
# Save summarized text to PDF
|
78 |
pdf = FPDF()
|
79 |
pdf.add_page()
|
80 |
pdf.set_font("Times", size=12)
|
81 |
-
pdf.multi_cell(190, 10, txt=
|
82 |
pdf_output_path = "legal.pdf"
|
83 |
pdf.output(pdf_output_path)
|
84 |
|
85 |
-
# Convert summarized text to audio
|
86 |
audio_output_path = "legal.wav"
|
87 |
-
tts = gTTS(text=
|
88 |
tts.save(audio_output_path)
|
89 |
|
90 |
-
return audio_output_path,
|
91 |
|
92 |
except Exception as e:
|
93 |
return None, f"An error occurred: {str(e)}", None
|
94 |
|
95 |
-
|
|
|
96 |
sample_document_path = "Marbury v. Madison.pdf"
|
97 |
|
98 |
with open(sample_document_path, "rb") as f:
|
@@ -105,7 +80,7 @@ with gr.Blocks() as iface:
|
|
105 |
|
106 |
text_input = gr.Textbox(label="Input Text")
|
107 |
file_input = gr.File(label="Upload PDF or DOCX")
|
108 |
-
slider = gr.Slider(minimum=10, maximum=
|
109 |
|
110 |
audio_output = gr.Audio(label="Generated Audio")
|
111 |
summary_output = gr.Textbox(label="Generated Summary")
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
+
import nltk
|
4 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
5 |
from fpdf import FPDF
|
6 |
from gtts import gTTS
|
|
|
9 |
from reportlab.lib.pagesizes import letter
|
10 |
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 = []
|
22 |
+
for para in doc.paragraphs:
|
23 |
+
full_text.append(para.text)
|
24 |
|
25 |
pdf = canvas.Canvas(output_pdf, pagesize=letter)
|
26 |
pdf.setFont("Helvetica", 12)
|
|
|
27 |
|
28 |
+
text = pdf.beginText(40, 750)
|
29 |
for line in full_text:
|
30 |
text.textLine(line)
|
31 |
|
|
|
33 |
pdf.save()
|
34 |
return output_pdf
|
35 |
|
36 |
+
# Process input file (PDF or DOCX)
|
37 |
+
def pdf_to_text(text, PDF, min_length=20):
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
try:
|
39 |
file_extension = os.path.splitext(PDF.name)[1].lower()
|
40 |
|
|
|
44 |
elif file_extension == '.pdf' and text == "":
|
45 |
text = extract_text(PDF.name)
|
46 |
|
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+1000)
|
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()
|
55 |
pdf.set_font("Times", size=12)
|
56 |
+
pdf.multi_cell(190, 10, txt=output_text, align='C')
|
57 |
pdf_output_path = "legal.pdf"
|
58 |
pdf.output(pdf_output_path)
|
59 |
|
|
|
60 |
audio_output_path = "legal.wav"
|
61 |
+
tts = gTTS(text=output_text, lang='en', slow=False)
|
62 |
tts.save(audio_output_path)
|
63 |
|
64 |
+
return audio_output_path, output_text, pdf_output_path
|
65 |
|
66 |
except Exception as e:
|
67 |
return None, f"An error occurred: {str(e)}", None
|
68 |
|
69 |
+
# Preloaded document handler
|
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:
|
|
|
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=100, step=10, value=20, label="Summary Minimum Length")
|
84 |
|
85 |
audio_output = gr.Audio(label="Generated Audio")
|
86 |
summary_output = gr.Textbox(label="Generated Summary")
|