Spaces:
Running
Running
Commit
·
9c73e01
1
Parent(s):
56e99ba
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,21 @@
|
|
1 |
-
#Initial installations
|
2 |
-
pip uninstall -y tensorflow
|
3 |
-
pip install tensorflow==2.14
|
4 |
|
5 |
-
|
6 |
-
pip install --upgrade transformers scipy
|
7 |
-
|
8 |
-
pip install transformers
|
9 |
-
pip install pymupdf
|
10 |
-
|
11 |
-
## Summarization
|
12 |
import gradio as gr
|
13 |
import fitz # PyMuPDF
|
14 |
from transformers import BartTokenizer, BartForConditionalGeneration, pipeline
|
15 |
import scipy.io.wavfile
|
16 |
import numpy as np
|
|
|
17 |
|
|
|
18 |
tokenizer = BartTokenizer.from_pretrained('facebook/bart-large-cnn')
|
19 |
model = BartForConditionalGeneration.from_pretrained('facebook/bart-large-cnn')
|
|
|
20 |
|
21 |
-
|
22 |
-
|
|
|
23 |
first_page = doc[0].get_text()
|
24 |
start_idx = first_page.lower().find("abstract")
|
25 |
end_idx = first_page.lower().find("introduction")
|
@@ -28,101 +24,34 @@ def extract_abstract(pdf_path):
|
|
28 |
else:
|
29 |
return "Abstract not found or '1 Introduction' not found in the first page."
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
print(abstract_text)
|
40 |
-
|
41 |
-
|
42 |
-
from IPython.core.display import display, HTML
|
43 |
-
|
44 |
-
# Function to display summary and reduction percentage aesthetically
|
45 |
-
def display_results(final_summary, original_text):
|
46 |
-
reduction_percentage = 100 * (1 - len(final_summary) / len(original_text))
|
47 |
-
html_content = f"""
|
48 |
-
<div style='padding: 20px; background-color: #f3f3f3; border-radius: 10px;'>
|
49 |
-
<h2 style='color: #2c3e50; text-align: center;'>Summary</h2>
|
50 |
-
<p style='color: #34495e; font-size: 16px; text-align: justify;'>{final_summary}</p>
|
51 |
-
<p style='color: #2c3e50;'><b>Reduction in Text:</b> {reduction_percentage:.2f}%</p>
|
52 |
-
</div>
|
53 |
-
"""
|
54 |
-
display(HTML(html_content))
|
55 |
-
|
56 |
-
# Summary generation and post-processing
|
57 |
-
inputs = tokenizer([abstract_text], max_length=1024, return_tensors='pt', truncation=True)
|
58 |
-
max_length_for_summary = 40
|
59 |
-
length_penalty_value = 2.0
|
60 |
-
|
61 |
-
summary_ids = model.generate(inputs['input_ids'],
|
62 |
-
num_beams=4,
|
63 |
-
max_length=max_length_for_summary,
|
64 |
-
min_length=10,
|
65 |
-
length_penalty=length_penalty_value,
|
66 |
-
early_stopping=True,
|
67 |
-
no_repeat_ngram_size=2)
|
68 |
-
|
69 |
-
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
70 |
-
summary = ' '.join(summary.split()) # Remove extra spaces
|
71 |
-
|
72 |
-
# Handle truncated words and adjust periods
|
73 |
-
words = summary.split()
|
74 |
-
cleaned_summary = []
|
75 |
-
for i, word in enumerate(words):
|
76 |
-
if '-' in word and i < len(words) - 1:
|
77 |
-
word = word.replace('-', '') + words[i + 1]
|
78 |
-
words[i + 1] = ""
|
79 |
-
|
80 |
-
if '.' in word and i != len(words) - 1:
|
81 |
-
word = word.replace('.', '')
|
82 |
-
cleaned_summary.append(word + ' and')
|
83 |
-
else:
|
84 |
-
cleaned_summary.append(word)
|
85 |
-
|
86 |
-
# Capitalize first word and adjust following words
|
87 |
-
final_summary = ' '.join(cleaned_summary)
|
88 |
-
final_summary = final_summary[0].upper() + final_summary[1:]
|
89 |
-
final_summary = ' '.join(w[0].lower() + w[1:] if w.lower() != 'and' else w for w in final_summary.split())
|
90 |
-
|
91 |
-
# Displaying the results
|
92 |
-
display_results(final_summary, abstract_text)
|
93 |
-
|
94 |
-
|
95 |
-
##Text-to-Speech
|
96 |
-
|
97 |
-
# Initialize the Bark TTS pipeline
|
98 |
-
synthesiser = pipeline("text-to-speech", "suno/bark")
|
99 |
-
|
100 |
-
# Initialize the Bark TTS pipeline
|
101 |
-
synthesiser = pipeline("text-to-speech", "suno/bark")
|
102 |
-
|
103 |
-
# Convert the summarized text to speech
|
104 |
-
speech = synthesiser(final_summary, forward_params={"do_sample": True})
|
105 |
|
106 |
-
#
|
107 |
-
|
108 |
-
|
|
|
109 |
|
110 |
-
# Save
|
111 |
-
output_file = "
|
112 |
-
scipy.io.wavfile.write(output_file, rate=speech["sampling_rate"], data=normalized_audio_data)
|
113 |
-
print(f"Audio file saved as {output_file}")
|
114 |
|
115 |
-
|
116 |
-
Audio(output_file)
|
117 |
|
118 |
# Gradio Interface
|
119 |
iface = gr.Interface(
|
120 |
fn=process_text,
|
121 |
-
inputs="
|
122 |
outputs=["text", "audio"],
|
123 |
title="Summarization and Text-to-Speech",
|
124 |
-
description="
|
125 |
)
|
126 |
|
127 |
if __name__ == "__main__":
|
128 |
-
iface.launch()
|
|
|
1 |
+
# Initial installations handled separately (not in app.py)
|
|
|
|
|
2 |
|
3 |
+
# Required imports
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import gradio as gr
|
5 |
import fitz # PyMuPDF
|
6 |
from transformers import BartTokenizer, BartForConditionalGeneration, pipeline
|
7 |
import scipy.io.wavfile
|
8 |
import numpy as np
|
9 |
+
from IPython.display import Audio
|
10 |
|
11 |
+
# Initialize tokenizers and models
|
12 |
tokenizer = BartTokenizer.from_pretrained('facebook/bart-large-cnn')
|
13 |
model = BartForConditionalGeneration.from_pretrained('facebook/bart-large-cnn')
|
14 |
+
synthesiser = pipeline("text-to-speech", "suno/bark")
|
15 |
|
16 |
+
# Function to extract abstract from PDF
|
17 |
+
def extract_abstract(pdf_content):
|
18 |
+
doc = fitz.open("pdf", pdf_content)
|
19 |
first_page = doc[0].get_text()
|
20 |
start_idx = first_page.lower().find("abstract")
|
21 |
end_idx = first_page.lower().find("introduction")
|
|
|
24 |
else:
|
25 |
return "Abstract not found or '1 Introduction' not found in the first page."
|
26 |
|
27 |
+
# Function to process text (summarize and convert to speech)
|
28 |
+
def process_text(pdf_content):
|
29 |
+
abstract_text = extract_abstract(pdf_content)
|
30 |
+
|
31 |
+
# Generate summary
|
32 |
+
inputs = tokenizer([abstract_text], max_length=1024, return_tensors='pt', truncation=True)
|
33 |
+
summary_ids = model.generate(inputs['input_ids'], num_beams=4, max_length=40, min_length=10, length_penalty=2.0, early_stopping=True, no_repeat_ngram_size=2)
|
34 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
# Convert summary to speech
|
37 |
+
speech = synthesiser(summary, forward_params={"do_sample": True})
|
38 |
+
audio_data = speech["audio"].squeeze()
|
39 |
+
normalized_audio_data = np.int16(audio_data / np.max(np.abs(audio_data)) * 32767)
|
40 |
|
41 |
+
# Save audio to temporary file
|
42 |
+
output_file = "temp_output.wav"
|
43 |
+
scipy.io.wavfile.write(output_file, rate=speech["sampling_rate"], data=normalized_audio_data)
|
|
|
44 |
|
45 |
+
return summary, output_file
|
|
|
46 |
|
47 |
# Gradio Interface
|
48 |
iface = gr.Interface(
|
49 |
fn=process_text,
|
50 |
+
inputs=gr.inputs.File(label="Upload PDF"),
|
51 |
outputs=["text", "audio"],
|
52 |
title="Summarization and Text-to-Speech",
|
53 |
+
description="Upload a PDF to extract, summarize its abstract, and convert to speech."
|
54 |
)
|
55 |
|
56 |
if __name__ == "__main__":
|
57 |
+
iface.launch()
|