Spaces:
Sleeping
Sleeping
Ari
commited on
Commit
•
204d8e4
1
Parent(s):
abdc1ac
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
|
|
|
|
3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
4 |
from fpdf import FPDF
|
5 |
from gtts import gTTS
|
@@ -8,20 +10,33 @@ from docx import Document
|
|
8 |
from reportlab.lib.pagesizes import letter
|
9 |
from reportlab.pdfgen import canvas
|
10 |
|
|
|
11 |
tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn")
|
12 |
model = AutoModelForSeq2SeqLM.from_pretrained("facebook/bart-large-cnn")
|
13 |
|
14 |
-
# Function to
|
15 |
-
def
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
chunks = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
chunk_tokens = tokens[:, i:i+max_length]
|
23 |
-
chunk_text = tokenizer.decode(chunk_tokens[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
|
24 |
-
chunks.append(chunk_text)
|
25 |
|
26 |
return chunks
|
27 |
|
@@ -42,18 +57,21 @@ def docx_to_pdf(docx_file, output_pdf="converted_doc.pdf"):
|
|
42 |
pdf.save()
|
43 |
return output_pdf
|
44 |
|
45 |
-
# Main processing function
|
46 |
def pdf_to_text(text, PDF, min_length=80):
|
47 |
try:
|
48 |
file_extension = os.path.splitext(PDF.name)[1].lower()
|
49 |
|
|
|
50 |
if file_extension == '.docx':
|
51 |
pdf_file_path = docx_to_pdf(PDF.name)
|
52 |
text = extract_text(pdf_file_path)
|
|
|
53 |
elif file_extension == '.pdf' and text == "":
|
54 |
text = extract_text(PDF.name)
|
55 |
|
56 |
-
chunks
|
|
|
57 |
summarized_text = ""
|
58 |
|
59 |
for chunk in chunks:
|
@@ -63,6 +81,7 @@ def pdf_to_text(text, PDF, min_length=80):
|
|
63 |
output_text = tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True)[0]
|
64 |
summarized_text += output_text + "\n\n"
|
65 |
|
|
|
66 |
pdf = FPDF()
|
67 |
pdf.add_page()
|
68 |
pdf.set_font("Times", size=12)
|
@@ -70,6 +89,7 @@ def pdf_to_text(text, PDF, min_length=80):
|
|
70 |
pdf_output_path = "legal.pdf"
|
71 |
pdf.output(pdf_output_path)
|
72 |
|
|
|
73 |
audio_output_path = "legal.wav"
|
74 |
tts = gTTS(text=summarized_text, lang='en', slow=False)
|
75 |
tts.save(audio_output_path)
|
@@ -79,12 +99,14 @@ def pdf_to_text(text, PDF, min_length=80):
|
|
79 |
except Exception as e:
|
80 |
return None, f"An error occurred: {str(e)}", None
|
81 |
|
|
|
82 |
def process_sample_document(min_length=80):
|
83 |
sample_document_path = "Marbury v. Madison.pdf"
|
84 |
|
85 |
with open(sample_document_path, "rb") as f:
|
86 |
return pdf_to_text("", f, min_length)
|
87 |
|
|
|
88 |
with gr.Blocks() as iface:
|
89 |
with gr.Row():
|
90 |
process_sample_button = gr.Button("Summarize Marbury v. Madison Case Pre-Uploaded")
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
+
import re
|
4 |
+
import numpy as np
|
5 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
6 |
from fpdf import FPDF
|
7 |
from gtts import gTTS
|
|
|
10 |
from reportlab.lib.pagesizes import letter
|
11 |
from reportlab.pdfgen import canvas
|
12 |
|
13 |
+
# Load the models and tokenizer
|
14 |
tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn")
|
15 |
model = AutoModelForSeq2SeqLM.from_pretrained("facebook/bart-large-cnn")
|
16 |
|
17 |
+
# Function to chunk text into sentence-based chunks
|
18 |
+
def chunk_text(text, max_token_len=1024):
|
19 |
+
# Split text into sentences
|
20 |
+
sentences = [sent.strip() + '.' for sent in re.split(r'(?<!\d)\.\s', text) if len(sent) > 1]
|
21 |
+
token_lengths = [len(tokenizer.tokenize(sent)) for sent in sentences]
|
22 |
+
|
23 |
+
# Initialize chunking
|
24 |
+
chunk_size = max_token_len
|
25 |
chunks = []
|
26 |
+
current_chunk = []
|
27 |
+
current_length = 0
|
28 |
+
|
29 |
+
for sent, length in zip(sentences, token_lengths):
|
30 |
+
if current_length + length <= chunk_size:
|
31 |
+
current_chunk.append(sent)
|
32 |
+
current_length += length
|
33 |
+
else:
|
34 |
+
chunks.append(" ".join(current_chunk))
|
35 |
+
current_chunk = [sent]
|
36 |
+
current_length = length
|
37 |
|
38 |
+
if current_chunk:
|
39 |
+
chunks.append(" ".join(current_chunk))
|
|
|
|
|
|
|
40 |
|
41 |
return chunks
|
42 |
|
|
|
57 |
pdf.save()
|
58 |
return output_pdf
|
59 |
|
60 |
+
# Main processing function using sentence-based chunking
|
61 |
def pdf_to_text(text, PDF, min_length=80):
|
62 |
try:
|
63 |
file_extension = os.path.splitext(PDF.name)[1].lower()
|
64 |
|
65 |
+
# If DOCX, convert to PDF
|
66 |
if file_extension == '.docx':
|
67 |
pdf_file_path = docx_to_pdf(PDF.name)
|
68 |
text = extract_text(pdf_file_path)
|
69 |
+
# If PDF, extract text
|
70 |
elif file_extension == '.pdf' and text == "":
|
71 |
text = extract_text(PDF.name)
|
72 |
|
73 |
+
# Split text into chunks based on sentence boundaries
|
74 |
+
chunks = chunk_text(text)
|
75 |
summarized_text = ""
|
76 |
|
77 |
for chunk in chunks:
|
|
|
81 |
output_text = tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True)[0]
|
82 |
summarized_text += output_text + "\n\n"
|
83 |
|
84 |
+
# Save summarized text to PDF
|
85 |
pdf = FPDF()
|
86 |
pdf.add_page()
|
87 |
pdf.set_font("Times", size=12)
|
|
|
89 |
pdf_output_path = "legal.pdf"
|
90 |
pdf.output(pdf_output_path)
|
91 |
|
92 |
+
# Convert summarized text to audio
|
93 |
audio_output_path = "legal.wav"
|
94 |
tts = gTTS(text=summarized_text, lang='en', slow=False)
|
95 |
tts.save(audio_output_path)
|
|
|
99 |
except Exception as e:
|
100 |
return None, f"An error occurred: {str(e)}", None
|
101 |
|
102 |
+
# Preloaded document processor
|
103 |
def process_sample_document(min_length=80):
|
104 |
sample_document_path = "Marbury v. Madison.pdf"
|
105 |
|
106 |
with open(sample_document_path, "rb") as f:
|
107 |
return pdf_to_text("", f, min_length)
|
108 |
|
109 |
+
# Gradio interface
|
110 |
with gr.Blocks() as iface:
|
111 |
with gr.Row():
|
112 |
process_sample_button = gr.Button("Summarize Marbury v. Madison Case Pre-Uploaded")
|