Subzero7473/transcript_sentiment
0
1import gradio as gr2import pandas as pd3import requests4import whisper5 6from transformers import pipeline7 8import nltk9nltk.download('punkt')10from nltk.tokenize import sent_tokenize11 12 13model = whisper.load_model('base')14 15 16def get_transcript_sentiment(url):17 # Download the file - binary content18 response = requests.get(url)19 with open('audio.mp3', 'wb') as f:20 f.write(response.content)21 22 # Download the model and generate the transcription23 result = model.transcribe('audio.mp3')24 25 # Split the transcript into sentences26 transcript_sentences = sent_tokenize(result['text'])27 28 # Run the Hugging Face sentiment analysis pipeline29 sentiment_pipeline = pipeline('sentiment-analysis')30 labels = sentiment_pipeline(transcript_sentences)31 32 # Reorganize into dataframe33 df = pd.DataFrame()34 col_labels = pd.json_normalize(labels)35 df['transcript_sentence'] = pd.Series(transcript_sentences)36 df['label'] = col_labels['label']37 df['score'] = col_labels['score']38 39 return df40 41with gr.Blocks() as demo:42 gr.Markdown(43 """44 # Provide MP3 -> 45 # Whisper generates captions -> 46 # Hugging Face generates sentiment 47 Paste the URL of an audio file (mp3) into the box and get the sentiment.48 """49 )50 with gr.Column(scale=1, min_width=800):51 text_input = gr.Text(placeholder='MP3 URL here', label='URL')52 df_output = gr.DataFrame(wrap=True, max_rows=10, headers=['transcript_sentence', 'label', 'score'])53 run_btn = gr.Button('Submit')54 run_btn.click(fn=get_transcript_sentiment, inputs=text_input, outputs=df_output)55 56 gr.Markdown('''57 ### Example 1: 58 [San Francisco Government Public Defender Candidate Statements](https://sanfrancisco.granicus.com/player/clip/42153?view_id=74&redirect=true&h=31a924a5a2f50ac4f57f07b44f58331c)59 '''60 )61 gr.Examples(62 [['https://archive-media2.granicus.com/OnDemand/sanfrancisco/sanfrancisco_8c0f76b6-527a-4702-b117-ed40c1eb26fd.mp3']],63 [text_input],64 fn=get_transcript_sentiment,65 label='Click to insert example URL'66 )67 68 gr.Markdown('''69 ### Example 2: 70 [San Francisco Government Mission Cultural Center](https://sanfrancisco.granicus.com/MediaPlayer.php?view_id=74&clip_id=42053)71 '''72 )73 gr.Examples(74 [['https://archive-media2.granicus.com/OnDemand/sanfrancisco/sanfrancisco_33cae840-42ed-458b-8eaf-bd4760747ab0.mp3']],75 [text_input],76 fn=get_transcript_sentiment,77 label='Click to insert example URL'78 )79 80 81 82 83demo.launch()84 