Spaces:
Runtime error
Runtime error
Commit
ยท
85cf8f4
1
Parent(s):
001afc8
Update app.py
Browse files
app.py
CHANGED
@@ -206,112 +206,117 @@ class GradioInference:
|
|
206 |
- Sentiment Analysis: using Hugging Face's default sentiment classifier
|
207 |
- WordCloud: using the wordcloud python library.
|
208 |
"""
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
)
|
229 |
-
|
230 |
-
# Multilingual summary with mt5
|
231 |
-
WHITESPACE_HANDLER = lambda k: re.sub('\s+', ' ', re.sub('\n+', ' ', k.strip()))
|
232 |
-
|
233 |
-
input_ids_sum = self.mt5_tokenizer(
|
234 |
-
[WHITESPACE_HANDLER(results["text"])],
|
235 |
-
return_tensors="pt",
|
236 |
-
padding="max_length",
|
237 |
-
truncation=True,
|
238 |
-
max_length=512
|
239 |
-
)["input_ids"]
|
240 |
-
|
241 |
-
output_ids_sum = self.mt5_model.generate(
|
242 |
-
input_ids=input_ids_sum,
|
243 |
-
max_length=130,
|
244 |
-
no_repeat_ngram_size=2,
|
245 |
-
num_beams=4
|
246 |
-
)[0]
|
247 |
-
|
248 |
-
summary = self.mt5_tokenizer.decode(
|
249 |
-
output_ids_sum,
|
250 |
-
skip_special_tokens=True,
|
251 |
-
clean_up_tokenization_spaces=False
|
252 |
-
)
|
253 |
-
# End multilingual summary
|
254 |
-
|
255 |
-
progress(0.60, desc="Extracting Keywords")
|
256 |
-
|
257 |
-
# Extract keywords using VoiceLabT5
|
258 |
-
task_prefix = "Keywords: "
|
259 |
-
input_sequence = task_prefix + results["text"]
|
260 |
-
|
261 |
-
input_ids = self.keyword_tokenizer(
|
262 |
-
input_sequence,
|
263 |
-
return_tensors="pt",
|
264 |
-
truncation=False
|
265 |
-
).input_ids
|
266 |
-
|
267 |
-
output = self.keyword_model.generate(
|
268 |
-
input_ids,
|
269 |
-
no_repeat_ngram_size=3,
|
270 |
-
num_beams=4
|
271 |
-
)
|
272 |
-
predicted = self.keyword_tokenizer.decode(output[0], skip_special_tokens=True)
|
273 |
-
keywords = [x.strip() for x in predicted.split(",") if x.strip()]
|
274 |
-
formatted_keywords = "\n".join([f"โข {keyword}" for keyword in keywords])
|
275 |
-
|
276 |
-
progress(0.80, desc="Extracting Sentiment")
|
277 |
-
|
278 |
-
# Define a dictionary to map labels to emojis
|
279 |
-
sentiment_emojis = {
|
280 |
-
"positive": "Positive ๐๐ผ",
|
281 |
-
"negative": "Negative ๐๐ผ",
|
282 |
-
"neutral": "Neutral ๐ถ",
|
283 |
-
}
|
284 |
-
|
285 |
-
# Sentiment label
|
286 |
-
label = self.classifier(summary)[0]["label"]
|
287 |
-
|
288 |
-
# Format the label with emojis
|
289 |
-
formatted_sentiment = sentiment_emojis.get(label, label)
|
290 |
-
|
291 |
-
progress(0.90, desc="Generating Wordcloud")
|
292 |
-
# WordCloud object
|
293 |
-
wordcloud = WordCloud(colormap = "Oranges").generate(
|
294 |
-
results["text"]
|
295 |
-
)
|
296 |
-
wordcloud_image = wordcloud.to_image()
|
297 |
-
|
298 |
-
if lang == "english" or lang == "none":
|
299 |
-
return (
|
300 |
-
results["text"],
|
301 |
-
transcription_summary[0]["summary_text"],
|
302 |
-
formatted_keywords,
|
303 |
-
formatted_sentiment,
|
304 |
-
wordcloud_image,
|
305 |
)
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
313 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
314 |
|
|
|
|
|
315 |
|
316 |
def from_article(self, article, progress=gr.Progress()):
|
317 |
"""
|
@@ -322,91 +327,96 @@ class GradioInference:
|
|
322 |
- Sentiment Analysis: using Hugging Face's default sentiment classifier
|
323 |
- WordCloud: using the wordcloud python library.
|
324 |
"""
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
402 |
|
403 |
-
|
404 |
-
|
405 |
-
formatted_keywords,
|
406 |
-
formatted_sentiment,
|
407 |
-
wordcloud_image,
|
408 |
-
)
|
409 |
|
|
|
|
|
410 |
|
411 |
gio = GradioInference()
|
412 |
title = "Media Insights"
|
@@ -420,7 +430,7 @@ with block as demo:
|
|
420 |
"""
|
421 |
<div style="text-align: center; max-width: 500px; margin: 0 auto;">
|
422 |
<div>
|
423 |
-
<h1 style="font-family:
|
424 |
</div>
|
425 |
<h4>
|
426 |
Your AI-powered media analytics tool โจ
|
|
|
206 |
- Sentiment Analysis: using Hugging Face's default sentiment classifier
|
207 |
- WordCloud: using the wordcloud python library.
|
208 |
"""
|
209 |
+
try:
|
210 |
+
progress(0, desc="Starting analysis")
|
211 |
+
|
212 |
+
if lang == "none":
|
213 |
+
lang = None
|
214 |
+
|
215 |
+
if size != self.current_size:
|
216 |
+
self.loaded_model = whisper.load_model(size)
|
217 |
+
self.current_size = size
|
218 |
+
|
219 |
+
progress(0.20, desc="Transcribing")
|
220 |
+
|
221 |
+
results = self.loaded_model.transcribe(audio_file, language=lang)
|
222 |
+
|
223 |
+
progress(0.40, desc="Summarizing")
|
224 |
+
|
225 |
+
# Perform summarization on the transcription
|
226 |
+
transcription_summary = self.bart_summarizer(
|
227 |
+
results["text"], max_length=150, min_length=30, do_sample=False, truncation=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
)
|
229 |
+
|
230 |
+
# Multilingual summary with mt5
|
231 |
+
WHITESPACE_HANDLER = lambda k: re.sub('\s+', ' ', re.sub('\n+', ' ', k.strip()))
|
232 |
+
|
233 |
+
input_ids_sum = self.mt5_tokenizer(
|
234 |
+
[WHITESPACE_HANDLER(results["text"])],
|
235 |
+
return_tensors="pt",
|
236 |
+
padding="max_length",
|
237 |
+
truncation=True,
|
238 |
+
max_length=512
|
239 |
+
)["input_ids"]
|
240 |
+
|
241 |
+
output_ids_sum = self.mt5_model.generate(
|
242 |
+
input_ids=input_ids_sum,
|
243 |
+
max_length=130,
|
244 |
+
no_repeat_ngram_size=2,
|
245 |
+
num_beams=4
|
246 |
+
)[0]
|
247 |
+
|
248 |
+
summary = self.mt5_tokenizer.decode(
|
249 |
+
output_ids_sum,
|
250 |
+
skip_special_tokens=True,
|
251 |
+
clean_up_tokenization_spaces=False
|
252 |
)
|
253 |
+
# End multilingual summary
|
254 |
+
|
255 |
+
progress(0.60, desc="Extracting Keywords")
|
256 |
+
|
257 |
+
# Extract keywords using VoiceLabT5
|
258 |
+
task_prefix = "Keywords: "
|
259 |
+
input_sequence = task_prefix + results["text"]
|
260 |
+
|
261 |
+
input_ids = self.keyword_tokenizer(
|
262 |
+
input_sequence,
|
263 |
+
return_tensors="pt",
|
264 |
+
truncation=False
|
265 |
+
).input_ids
|
266 |
+
|
267 |
+
output = self.keyword_model.generate(
|
268 |
+
input_ids,
|
269 |
+
no_repeat_ngram_size=3,
|
270 |
+
num_beams=4
|
271 |
+
)
|
272 |
+
predicted = self.keyword_tokenizer.decode(output[0], skip_special_tokens=True)
|
273 |
+
keywords = [x.strip() for x in predicted.split(",") if x.strip()]
|
274 |
+
formatted_keywords = "\n".join([f"โข {keyword}" for keyword in keywords])
|
275 |
+
|
276 |
+
progress(0.80, desc="Extracting Sentiment")
|
277 |
+
|
278 |
+
# Define a dictionary to map labels to emojis
|
279 |
+
sentiment_emojis = {
|
280 |
+
"positive": "Positive ๐๐ผ",
|
281 |
+
"negative": "Negative ๐๐ผ",
|
282 |
+
"neutral": "Neutral ๐ถ",
|
283 |
+
}
|
284 |
+
|
285 |
+
# Sentiment label
|
286 |
+
label = self.classifier(summary)[0]["label"]
|
287 |
+
|
288 |
+
# Format the label with emojis
|
289 |
+
formatted_sentiment = sentiment_emojis.get(label, label)
|
290 |
+
|
291 |
+
progress(0.90, desc="Generating Wordcloud")
|
292 |
+
# WordCloud object
|
293 |
+
wordcloud = WordCloud(colormap = "Oranges").generate(
|
294 |
+
results["text"]
|
295 |
+
)
|
296 |
+
wordcloud_image = wordcloud.to_image()
|
297 |
+
|
298 |
+
if lang == "english" or lang == "none":
|
299 |
+
return (
|
300 |
+
results["text"],
|
301 |
+
transcription_summary[0]["summary_text"],
|
302 |
+
formatted_keywords,
|
303 |
+
formatted_sentiment,
|
304 |
+
wordcloud_image,
|
305 |
+
)
|
306 |
+
else:
|
307 |
+
return (
|
308 |
+
results["text"],
|
309 |
+
summary,
|
310 |
+
formatted_keywords,
|
311 |
+
formatted_sentiment,
|
312 |
+
wordcloud_image,
|
313 |
+
)
|
314 |
+
|
315 |
+
except:
|
316 |
+
gr.Error(message="Exceeded audio size. Choose a different audio")
|
317 |
|
318 |
+
finally:
|
319 |
+
gr.Info("Success!")
|
320 |
|
321 |
def from_article(self, article, progress=gr.Progress()):
|
322 |
"""
|
|
|
327 |
- Sentiment Analysis: using Hugging Face's default sentiment classifier
|
328 |
- WordCloud: using the wordcloud python library.
|
329 |
"""
|
330 |
+
try:
|
331 |
+
progress(0, desc="Starting analysis")
|
332 |
+
|
333 |
+
progress(0.30, desc="Summarizing")
|
334 |
+
|
335 |
+
# Perform summarization on the transcription
|
336 |
+
transcription_summary = self.bart_summarizer(
|
337 |
+
article, max_length=150, min_length=30, do_sample=False, truncation=True
|
338 |
+
)
|
339 |
+
|
340 |
+
# Multilingual summary with mt5
|
341 |
+
WHITESPACE_HANDLER = lambda k: re.sub('\s+', ' ', re.sub('\n+', ' ', k.strip()))
|
342 |
+
|
343 |
+
input_ids_sum = self.mt5_tokenizer(
|
344 |
+
[WHITESPACE_HANDLER(article)],
|
345 |
+
return_tensors="pt",
|
346 |
+
padding="max_length",
|
347 |
+
truncation=True,
|
348 |
+
max_length=512
|
349 |
+
)["input_ids"]
|
350 |
+
|
351 |
+
output_ids_sum = self.mt5_model.generate(
|
352 |
+
input_ids=input_ids_sum,
|
353 |
+
max_length=130,
|
354 |
+
no_repeat_ngram_size=2,
|
355 |
+
num_beams=4
|
356 |
+
)[0]
|
357 |
+
|
358 |
+
summary = self.mt5_tokenizer.decode(
|
359 |
+
output_ids_sum,
|
360 |
+
skip_special_tokens=True,
|
361 |
+
clean_up_tokenization_spaces=False
|
362 |
+
)
|
363 |
+
# End multilingual summary
|
364 |
+
|
365 |
+
progress(0.60, desc="Extracting Keywords")
|
366 |
+
|
367 |
+
# Extract keywords using VoiceLabT5
|
368 |
+
task_prefix = "Keywords: "
|
369 |
+
input_sequence = task_prefix + article
|
370 |
+
|
371 |
+
input_ids = self.keyword_tokenizer(
|
372 |
+
input_sequence,
|
373 |
+
return_tensors="pt",
|
374 |
+
truncation=False
|
375 |
+
).input_ids
|
376 |
+
|
377 |
+
output = self.keyword_model.generate(
|
378 |
+
input_ids,
|
379 |
+
no_repeat_ngram_size=3,
|
380 |
+
num_beams=4
|
381 |
+
)
|
382 |
+
predicted = self.keyword_tokenizer.decode(output[0], skip_special_tokens=True)
|
383 |
+
keywords = [x.strip() for x in predicted.split(",") if x.strip()]
|
384 |
+
formatted_keywords = "\n".join([f"โข {keyword}" for keyword in keywords])
|
385 |
+
|
386 |
+
progress(0.80, desc="Extracting Sentiment")
|
387 |
+
|
388 |
+
# Define a dictionary to map labels to emojis
|
389 |
+
sentiment_emojis = {
|
390 |
+
"positive": "Positive ๐๐ผ",
|
391 |
+
"negative": "Negative ๐๐ผ",
|
392 |
+
"neutral": "Neutral ๐ถ",
|
393 |
+
}
|
394 |
+
|
395 |
+
# Sentiment label
|
396 |
+
label = self.classifier(summary)[0]["label"]
|
397 |
+
|
398 |
+
# Format the label with emojis
|
399 |
+
formatted_sentiment = sentiment_emojis.get(label, label)
|
400 |
+
|
401 |
+
progress(0.90, desc="Generating Wordcloud")
|
402 |
+
# WordCloud object
|
403 |
+
wordcloud = WordCloud(colormap = "Oranges").generate(
|
404 |
+
article
|
405 |
+
)
|
406 |
+
wordcloud_image = wordcloud.to_image()
|
407 |
+
|
408 |
+
return (
|
409 |
+
transcription_summary[0]["summary_text"],
|
410 |
+
formatted_keywords,
|
411 |
+
formatted_sentiment,
|
412 |
+
wordcloud_image,
|
413 |
+
)
|
414 |
|
415 |
+
except:
|
416 |
+
gr.Error(message="Exceeded text size. Choose a different audio")
|
|
|
|
|
|
|
|
|
417 |
|
418 |
+
finally:
|
419 |
+
gr.Info("Success!")
|
420 |
|
421 |
gio = GradioInference()
|
422 |
title = "Media Insights"
|
|
|
430 |
"""
|
431 |
<div style="text-align: center; max-width: 500px; margin: 0 auto;">
|
432 |
<div>
|
433 |
+
<h1 style="font-family: Nunito, sans-serif;">MEDIA <span style="color: #433ccb;">INSIGHTS</span> ๐ก</h1>
|
434 |
</div>
|
435 |
<h4>
|
436 |
Your AI-powered media analytics tool โจ
|