Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -35,7 +35,7 @@ import torch
|
|
35 |
#import torchaudio
|
36 |
#import torchaudio.transforms as transforms
|
37 |
|
38 |
-
from transformers import pipeline,
|
39 |
|
40 |
import spacy
|
41 |
import networkx as nx
|
@@ -101,6 +101,11 @@ def transcribe_audio(audio_file, batch_size=16):
|
|
101 |
return text.strip(), system_info
|
102 |
|
103 |
# ------------summary section------------
|
|
|
|
|
|
|
|
|
|
|
104 |
@spaces.GPU()
|
105 |
def clean_text(text):
|
106 |
text = re.sub(r'https?:\/\/.*[\r\n]*', '', text)
|
@@ -112,6 +117,9 @@ nlp = spacy.blank("nb") # 'nb' ==> codename = Norwegian Bokmål
|
|
112 |
nlp.add_pipe('sentencizer')
|
113 |
spacy_stop_words = spacy.lang.nb.stop_words.STOP_WORDS
|
114 |
|
|
|
|
|
|
|
115 |
@spaces.GPU()
|
116 |
def preprocess_text(text):
|
117 |
# Process the text with SpaCy
|
@@ -122,7 +130,6 @@ def preprocess_text(text):
|
|
122 |
words = [token.text for token in doc if token.text.lower() not in stop_words]
|
123 |
return ' '.join(words)
|
124 |
|
125 |
-
# Summarize w/T5 model
|
126 |
@spaces.GPU()
|
127 |
def summarize_text(text):
|
128 |
preprocessed_text = preprocess_text(text)
|
@@ -130,7 +137,8 @@ def summarize_text(text):
|
|
130 |
inputs = inputs.to(device)
|
131 |
summary_ids = summarization_model.generate(inputs.input_ids, num_beams=5, max_length=150, early_stopping=True)
|
132 |
return summarization_tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
133 |
-
|
|
|
134 |
def build_similarity_matrix(sentences, stop_words):
|
135 |
similarity_matrix = nx.Graph()
|
136 |
for i, tokens_a in enumerate(sentences):
|
@@ -141,6 +149,7 @@ def build_similarity_matrix(sentences, stop_words):
|
|
141 |
return similarity_matrix
|
142 |
|
143 |
# PageRank
|
|
|
144 |
def graph_based_summary(text, num_paragraphs=3):
|
145 |
doc = nlp(text)
|
146 |
sentences = [sent.text for sent in doc.sents]
|
@@ -157,8 +166,8 @@ def graph_based_summary(text, num_paragraphs=3):
|
|
157 |
return ' '.join([sent for _, sent in ranked_sentences[:num_paragraphs]])
|
158 |
|
159 |
# LexRank
|
|
|
160 |
def lex_rank_summary(text, num_paragraphs=3, threshold=0.1):
|
161 |
-
|
162 |
doc = nlp(text)
|
163 |
sentences = [sent.text for sent in doc.sents]
|
164 |
if len(sentences) < num_paragraphs:
|
@@ -177,8 +186,8 @@ def lex_rank_summary(text, num_paragraphs=3, threshold=0.1):
|
|
177 |
return ' '.join([ranked_sentences[i][1] for i in range(num_paragraphs)])
|
178 |
|
179 |
# TextRank
|
|
|
180 |
def text_rank_summary(text, num_paragraphs=3):
|
181 |
-
|
182 |
doc = nlp(text)
|
183 |
sentences = [sent.text for sent in doc.sents]
|
184 |
if len(sentences) < num_paragraphs:
|
@@ -268,7 +277,7 @@ with iface:
|
|
268 |
|
269 |
""")
|
270 |
|
271 |
-
|
272 |
summarize_transcribed_button_text_rank.click(fn=lambda text: text_rank_summary(text), inputs=[text_output], outputs=[summary_output_text_rank])
|
273 |
summarize_uploaded_button_text_rank = gr.Button("Upload Text to Summarize, Click Here")
|
274 |
summarize_uploaded_button_text_rank.click(fn=text_rank_summary, inputs=[text_input_text_rank], outputs=[summary_output_text_rank])
|
|
|
35 |
#import torchaudio
|
36 |
#import torchaudio.transforms as transforms
|
37 |
|
38 |
+
from transformers import pipeline, AutoModel
|
39 |
|
40 |
import spacy
|
41 |
import networkx as nx
|
|
|
101 |
return text.strip(), system_info
|
102 |
|
103 |
# ------------summary section------------
|
104 |
+
|
105 |
+
|
106 |
+
|
107 |
+
# -----------------BLOCKS NEED EDIT....!--------------
|
108 |
+
|
109 |
@spaces.GPU()
|
110 |
def clean_text(text):
|
111 |
text = re.sub(r'https?:\/\/.*[\r\n]*', '', text)
|
|
|
117 |
nlp.add_pipe('sentencizer')
|
118 |
spacy_stop_words = spacy.lang.nb.stop_words.STOP_WORDS
|
119 |
|
120 |
+
summarization_model = AutoModel.from_pretrained("NbAiLab/nb-bert-large")
|
121 |
+
# pipe = pipeline("fill-mask", model="NbAiLab/nb-bert-large")
|
122 |
+
|
123 |
@spaces.GPU()
|
124 |
def preprocess_text(text):
|
125 |
# Process the text with SpaCy
|
|
|
130 |
words = [token.text for token in doc if token.text.lower() not in stop_words]
|
131 |
return ' '.join(words)
|
132 |
|
|
|
133 |
@spaces.GPU()
|
134 |
def summarize_text(text):
|
135 |
preprocessed_text = preprocess_text(text)
|
|
|
137 |
inputs = inputs.to(device)
|
138 |
summary_ids = summarization_model.generate(inputs.input_ids, num_beams=5, max_length=150, early_stopping=True)
|
139 |
return summarization_tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
140 |
+
|
141 |
+
@spaces.GPU()
|
142 |
def build_similarity_matrix(sentences, stop_words):
|
143 |
similarity_matrix = nx.Graph()
|
144 |
for i, tokens_a in enumerate(sentences):
|
|
|
149 |
return similarity_matrix
|
150 |
|
151 |
# PageRank
|
152 |
+
@spaces.GPU()
|
153 |
def graph_based_summary(text, num_paragraphs=3):
|
154 |
doc = nlp(text)
|
155 |
sentences = [sent.text for sent in doc.sents]
|
|
|
166 |
return ' '.join([sent for _, sent in ranked_sentences[:num_paragraphs]])
|
167 |
|
168 |
# LexRank
|
169 |
+
@spaces.GPU()
|
170 |
def lex_rank_summary(text, num_paragraphs=3, threshold=0.1):
|
|
|
171 |
doc = nlp(text)
|
172 |
sentences = [sent.text for sent in doc.sents]
|
173 |
if len(sentences) < num_paragraphs:
|
|
|
186 |
return ' '.join([ranked_sentences[i][1] for i in range(num_paragraphs)])
|
187 |
|
188 |
# TextRank
|
189 |
+
@spaces.GPU()
|
190 |
def text_rank_summary(text, num_paragraphs=3):
|
|
|
191 |
doc = nlp(text)
|
192 |
sentences = [sent.text for sent in doc.sents]
|
193 |
if len(sentences) < num_paragraphs:
|
|
|
277 |
|
278 |
""")
|
279 |
|
280 |
+
summarize_transcribed_button_text_rank = gr.Button("Summary of Transcribed Text, Click Here")
|
281 |
summarize_transcribed_button_text_rank.click(fn=lambda text: text_rank_summary(text), inputs=[text_output], outputs=[summary_output_text_rank])
|
282 |
summarize_uploaded_button_text_rank = gr.Button("Upload Text to Summarize, Click Here")
|
283 |
summarize_uploaded_button_text_rank.click(fn=text_rank_summary, inputs=[text_input_text_rank], outputs=[summary_output_text_rank])
|