storresbusquets commited on
Commit
85cf8f4
ยท
1 Parent(s): 001afc8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +197 -187
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
- gr.Info("Starting process")
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
 
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
- gr.Info("Starting process")
326
- progress(0, desc="Starting analysis")
327
-
328
- progress(0.30, desc="Summarizing")
329
-
330
- # Perform summarization on the transcription
331
- transcription_summary = self.bart_summarizer(
332
- article, max_length=150, min_length=30, do_sample=False, truncation=True
333
- )
334
-
335
- # Multilingual summary with mt5
336
- WHITESPACE_HANDLER = lambda k: re.sub('\s+', ' ', re.sub('\n+', ' ', k.strip()))
337
-
338
- input_ids_sum = self.mt5_tokenizer(
339
- [WHITESPACE_HANDLER(article)],
340
- return_tensors="pt",
341
- padding="max_length",
342
- truncation=True,
343
- max_length=512
344
- )["input_ids"]
345
-
346
- output_ids_sum = self.mt5_model.generate(
347
- input_ids=input_ids_sum,
348
- max_length=130,
349
- no_repeat_ngram_size=2,
350
- num_beams=4
351
- )[0]
352
-
353
- summary = self.mt5_tokenizer.decode(
354
- output_ids_sum,
355
- skip_special_tokens=True,
356
- clean_up_tokenization_spaces=False
357
- )
358
- # End multilingual summary
359
-
360
- progress(0.60, desc="Extracting Keywords")
361
-
362
- # Extract keywords using VoiceLabT5
363
- task_prefix = "Keywords: "
364
- input_sequence = task_prefix + article
365
-
366
- input_ids = self.keyword_tokenizer(
367
- input_sequence,
368
- return_tensors="pt",
369
- truncation=False
370
- ).input_ids
371
-
372
- output = self.keyword_model.generate(
373
- input_ids,
374
- no_repeat_ngram_size=3,
375
- num_beams=4
376
- )
377
- predicted = self.keyword_tokenizer.decode(output[0], skip_special_tokens=True)
378
- keywords = [x.strip() for x in predicted.split(",") if x.strip()]
379
- formatted_keywords = "\n".join([f"โ€ข {keyword}" for keyword in keywords])
380
-
381
- progress(0.80, desc="Extracting Sentiment")
382
-
383
- # Define a dictionary to map labels to emojis
384
- sentiment_emojis = {
385
- "positive": "Positive ๐Ÿ‘๐Ÿผ",
386
- "negative": "Negative ๐Ÿ‘Ž๐Ÿผ",
387
- "neutral": "Neutral ๐Ÿ˜ถ",
388
- }
389
-
390
- # Sentiment label
391
- label = self.classifier(summary)[0]["label"]
392
-
393
- # Format the label with emojis
394
- formatted_sentiment = sentiment_emojis.get(label, label)
395
-
396
- progress(0.90, desc="Generating Wordcloud")
397
- # WordCloud object
398
- wordcloud = WordCloud(colormap = "Oranges").generate(
399
- article
400
- )
401
- wordcloud_image = wordcloud.to_image()
 
 
 
 
 
 
 
402
 
403
- return (
404
- transcription_summary[0]["summary_text"],
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: Montserrat, sans-serif;">MEDIA <span style="color: #433ccb;">INSIGHTS</span> ๐Ÿ’ก</h1>
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 โœจ