Spaces:
Sleeping
Sleeping
Ari
commited on
Commit
•
ec7dfc2
1
Parent(s):
f6ccaae
Update app.py
Browse files
app.py
CHANGED
@@ -11,23 +11,21 @@ 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 |
-
#
|
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 |
-
# Create a PDF and write the extracted text using reportlab
|
26 |
pdf = canvas.Canvas(output_pdf, pagesize=letter)
|
27 |
pdf.setFont("Helvetica", 12)
|
28 |
|
29 |
-
|
30 |
-
text = pdf.beginText(40, 750) # Start position on the page
|
31 |
for line in full_text:
|
32 |
text.textLine(line)
|
33 |
|
@@ -35,29 +33,23 @@ def docx_to_pdf(docx_file, output_pdf="converted_doc.pdf"):
|
|
35 |
pdf.save()
|
36 |
return output_pdf
|
37 |
|
38 |
-
#
|
39 |
def pdf_to_text(text, PDF, min_length=20):
|
40 |
try:
|
41 |
-
# Determine whether the input is a PDF or DOCX
|
42 |
file_extension = os.path.splitext(PDF.name)[1].lower()
|
43 |
|
44 |
-
# If DOCX, first convert it to PDF
|
45 |
if file_extension == '.docx':
|
46 |
-
pdf_file_path = docx_to_pdf(PDF.name)
|
47 |
-
text = extract_text(pdf_file_path)
|
48 |
-
# If PDF, extract text from it directly
|
49 |
elif file_extension == '.pdf' and text == "":
|
50 |
text = extract_text(PDF.name)
|
51 |
|
52 |
-
|
53 |
-
inputs = tokenizer([text], max_length=1024, return_tensors="pt")
|
54 |
min_length = int(min_length)
|
55 |
|
56 |
-
# Generate summary
|
57 |
summary_ids = model.generate(inputs["input_ids"], num_beams=2, min_length=min_length, max_length=min_length+1000)
|
58 |
-
output_text = tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=
|
59 |
|
60 |
-
# Save summarized text to PDF
|
61 |
pdf = FPDF()
|
62 |
pdf.add_page()
|
63 |
pdf.set_font("Times", size=12)
|
@@ -65,7 +57,6 @@ def pdf_to_text(text, PDF, min_length=20):
|
|
65 |
pdf_output_path = "legal.pdf"
|
66 |
pdf.output(pdf_output_path)
|
67 |
|
68 |
-
# Convert summarized text to audio
|
69 |
audio_output_path = "legal.wav"
|
70 |
tts = gTTS(text=output_text, lang='en', slow=False)
|
71 |
tts.save(audio_output_path)
|
@@ -75,35 +66,27 @@ def pdf_to_text(text, PDF, min_length=20):
|
|
75 |
except Exception as e:
|
76 |
return None, f"An error occurred: {str(e)}", None
|
77 |
|
78 |
-
#
|
79 |
def process_sample_document(min_length=20):
|
80 |
-
# Replace this with the path to your preloaded legal document (PDF)
|
81 |
sample_document_path = "Marbury v. Madison.pdf"
|
82 |
|
83 |
-
# Simulate file-like object for Gradio
|
84 |
with open(sample_document_path, "rb") as f:
|
85 |
return pdf_to_text("", f, min_length)
|
86 |
|
87 |
# Gradio interface
|
88 |
with gr.Blocks() as iface:
|
89 |
-
# Create a button to process the sample document
|
90 |
with gr.Row():
|
91 |
-
process_sample_button = gr.Button("
|
92 |
|
93 |
-
# Create the regular input interface
|
94 |
text_input = gr.Textbox(label="Input Text")
|
95 |
file_input = gr.File(label="Upload PDF or DOCX")
|
96 |
slider = gr.Slider(minimum=10, maximum=100, step=10, value=20, label="Summary Minimum Length")
|
97 |
|
98 |
-
# Outputs
|
99 |
audio_output = gr.Audio(label="Generated Audio")
|
100 |
summary_output = gr.Textbox(label="Generated Summary")
|
101 |
pdf_output = gr.File(label="Summary PDF")
|
102 |
|
103 |
-
# Define the action for the button click to process the preloaded document
|
104 |
process_sample_button.click(fn=process_sample_document, inputs=slider, outputs=[audio_output, summary_output, pdf_output])
|
105 |
-
|
106 |
-
# Define the action for the regular file processing
|
107 |
file_input.change(fn=pdf_to_text, inputs=[text_input, file_input, slider], outputs=[audio_output, summary_output, pdf_output])
|
108 |
|
109 |
if __name__ == "__main__":
|
|
|
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 |
|
|
|
41 |
if file_extension == '.docx':
|
42 |
+
pdf_file_path = docx_to_pdf(PDF.name)
|
43 |
+
text = extract_text(pdf_file_path)
|
|
|
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)
|
|
|
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)
|
|
|
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:
|
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 Pre-Uploaded Marbury v. Madison Case Documented")
|
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")
|
87 |
pdf_output = gr.File(label="Summary PDF")
|
88 |
|
|
|
89 |
process_sample_button.click(fn=process_sample_document, inputs=slider, outputs=[audio_output, summary_output, pdf_output])
|
|
|
|
|
90 |
file_input.change(fn=pdf_to_text, inputs=[text_input, file_input, slider], outputs=[audio_output, summary_output, pdf_output])
|
91 |
|
92 |
if __name__ == "__main__":
|