Pontonkid commited on
Commit
baba88b
1 Parent(s): 633d171

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +147 -0
app.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import whisper
3
+ from transformers import pipeline
4
+
5
+ model = whisper.load_model("base")
6
+ sentiment_analysis = pipeline("sentiment-analysis", framework="pt", model="SamLowe/roberta-base-go_emotions")
7
+
8
+ def analyze_sentiment(text):
9
+ results = sentiment_analysis(text)
10
+ sentiment_results = {result['label']: result['score'] for result in results}
11
+ return sentiment_results
12
+
13
+ def get_sentiment_emoji(sentiment):
14
+ # Define the emojis corresponding to each sentiment
15
+ emoji_mapping = {
16
+ "disappointment": "😞",
17
+ "sadness": "😢",
18
+ "annoyance": "😠",
19
+ "neutral": "😐",
20
+ "disapproval": "👎",
21
+ "realization": "😮",
22
+ "nervousness": "😬",
23
+ "approval": "👍",
24
+ "joy": "😄",
25
+ "anger": "😡",
26
+ "embarrassment": "😳",
27
+ "caring": "🤗",
28
+ "remorse": "😔",
29
+ "disgust": "🤢",
30
+ "grief": "😥",
31
+ "confusion": "😕",
32
+ "relief": "😌",
33
+ "desire": "😍",
34
+ "admiration": "😌",
35
+ "optimism": "😊",
36
+ "fear": "😨",
37
+ "love": "❤️",
38
+ "excitement": "🎉",
39
+ "curiosity": "🤔",
40
+ "amusement": "😄",
41
+ "surprise": "😲",
42
+ "gratitude": "🙏",
43
+ "pride": "🦁"
44
+ }
45
+ return emoji_mapping.get(sentiment, "")
46
+
47
+ def display_sentiment_results(sentiment_results, option):
48
+ sentiment_text = ""
49
+ for sentiment, score in sentiment_results.items():
50
+ emoji = get_sentiment_emoji(sentiment)
51
+ if option == "Sentiment Only":
52
+ sentiment_text += f"{sentiment} {emoji}\n"
53
+ elif option == "Sentiment + Score":
54
+ sentiment_text += f"{sentiment} {emoji}: {score}\n"
55
+ return sentiment_text
56
+
57
+ def inference(audio, sentiment_option):
58
+ audio = whisper.load_audio(audio)
59
+ audio = whisper.pad_or_trim(audio)
60
+
61
+ mel = whisper.log_mel_spectrogram(audio).to(model.device)
62
+
63
+ _, probs = model.detect_language(mel)
64
+ lang = max(probs, key=probs.get)
65
+
66
+ options = whisper.DecodingOptions(fp16=False)
67
+ result = whisper.decode(model, mel, options)
68
+
69
+ sentiment_results = analyze_sentiment(result.text)
70
+ sentiment_output = display_sentiment_results(sentiment_results, sentiment_option)
71
+
72
+ return lang.upper(), result.text, sentiment_output
73
+
74
+ title = """<h1 align="center">🎤 Multilingual ASR 💬</h1>"""
75
+ image_path = "/content/thmbnail.jpg"
76
+ description = """
77
+ 💻 This demo showcases a general-purpose speech recognition model called Whisper. It is trained on a large dataset of diverse audio and supports multilingual speech recognition, speech translation, and language identification tasks.<br><br>
78
+ 📝 For more details, check out the [GitHub repository](https://github.com/openai/whisper).<br><br>
79
+ ⚙️ Components of the tool:<br>
80
+ <br>
81
+ &nbsp;&nbsp;&nbsp;&nbsp; - Real-time multilingual speech recognition<br>
82
+ &nbsp;&nbsp;&nbsp;&nbsp; - Language identification<br>
83
+ &nbsp;&nbsp;&nbsp;&nbsp; - Sentiment analysis of the transcriptions<br>
84
+ <br>
85
+ 🎯 The sentiment analysis results are provided as a dictionary with different emotions and their corresponding scores.<br>
86
+ ✅ The higher the score for a specific emotion, the stronger the presence of that emotion in the transcribed text.<br>
87
+ ❓ Use the "Input Audio" option to provide an audio file or use the microphone for real-time speech recognition.<br>
88
+ ⚡️ The model will transcribe the audio and perform sentiment analysis on the transcribed text.<br>
89
+ 😃 The sentiment analysis results are displayed with emojis representing the corresponding sentiment.<br>
90
+ """
91
+
92
+ custom_css = """
93
+ #banner-image {
94
+ display: block;
95
+ margin-left: auto;
96
+ margin-right: auto;
97
+ }
98
+ #chat-message {
99
+ font-size: 14px;
100
+ min-height: 300px;
101
+ }
102
+ """
103
+
104
+ block = gr.Blocks(css=custom_css)
105
+
106
+ with block:
107
+ gr.HTML(title)
108
+
109
+ with gr.Row():
110
+ with gr.Column():
111
+ gr.Image(image_path, elem_id="banner-image", show_label=False)
112
+ with gr.Column():
113
+ gr.HTML(description)
114
+
115
+ with gr.Group():
116
+ with gr.Box():
117
+ audio = gr.Audio(
118
+ label="Input Audio",
119
+ show_label=False,
120
+ source="microphone",
121
+ type="filepath"
122
+ )
123
+
124
+ sentiment_option = gr.Radio(
125
+ choices=["Sentiment Only", "Sentiment + Score"],
126
+ label="Select an option",
127
+ default="Sentiment Only"
128
+ )
129
+
130
+ btn = gr.Button("Transcribe")
131
+
132
+ lang_str = gr.Textbox(label="Language")
133
+
134
+ text = gr.Textbox(label="Transcription")
135
+
136
+ sentiment_output = gr.Textbox(label="Sentiment Analysis Results", output=True)
137
+
138
+ btn.click(inference, inputs=[audio, sentiment_option], outputs=[lang_str, text, sentiment_output])
139
+
140
+ gr.HTML('''
141
+ <div class="footer">
142
+ <p>Model by <a href="https://github.com/openai/whisper" style="text-decoration: underline;" target="_blank">OpenAI</a>
143
+ </p>
144
+ </div>
145
+ ''')
146
+
147
+ block.launch()