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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""app.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1i-Mo3pDk4cm6BpSRL37pXCvCqWNBlO7X
|
8 |
+
"""
|
9 |
+
|
10 |
+
from __future__ import annotations
|
11 |
+
|
12 |
+
import gradio as gr
|
13 |
+
import whisper
|
14 |
+
from transformers import pipeline
|
15 |
+
from gradio.themes.base import Base
|
16 |
+
from gradio.themes.utils import colors, fonts, sizes
|
17 |
+
from typing import Iterable
|
18 |
+
import os
|
19 |
+
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
20 |
+
import matplotlib
|
21 |
+
matplotlib.use('TkAgg')
|
22 |
+
import matplotlib.pyplot as plt
|
23 |
+
|
24 |
+
|
25 |
+
model = whisper.load_model("base")
|
26 |
+
sentiment_analysis = pipeline("sentiment-analysis", framework="pt", model="SamLowe/roberta-base-go_emotions")
|
27 |
+
|
28 |
+
def analyze_sentiment(text):
|
29 |
+
results = sentiment_analysis(text)
|
30 |
+
sentiment_results = {result['label']: result['score'] for result in results}
|
31 |
+
return sentiment_results
|
32 |
+
|
33 |
+
def get_sentiment_emoji(sentiment):
|
34 |
+
# Define the emojis corresponding to each sentiment
|
35 |
+
emoji_mapping = {
|
36 |
+
"disappointment": "😞",
|
37 |
+
"sadness": "😢",
|
38 |
+
"annoyance": "😠",
|
39 |
+
"neutral": "😐",
|
40 |
+
"disapproval": "👎",
|
41 |
+
"realization": "😮",
|
42 |
+
"nervousness": "😬",
|
43 |
+
"approval": "👍",
|
44 |
+
"joy": "😄",
|
45 |
+
"anger": "😡",
|
46 |
+
"embarrassment": "😳",
|
47 |
+
"caring": "🤗",
|
48 |
+
"remorse": "😔",
|
49 |
+
"disgust": "🤢",
|
50 |
+
"grief": "😥",
|
51 |
+
"confusion": "😕",
|
52 |
+
"relief": "😌",
|
53 |
+
"desire": "😍",
|
54 |
+
"admiration": "😌",
|
55 |
+
"optimism": "😊",
|
56 |
+
"fear": "😨",
|
57 |
+
"love": "❤️",
|
58 |
+
"excitement": "🎉",
|
59 |
+
"curiosity": "🤔",
|
60 |
+
"amusement": "😄",
|
61 |
+
"surprise": "😲",
|
62 |
+
"gratitude": "🙏",
|
63 |
+
"pride": "🦁"
|
64 |
+
}
|
65 |
+
return emoji_mapping.get(sentiment, "")
|
66 |
+
|
67 |
+
def display_sentiment_results(sentiment_results, option):
|
68 |
+
sentiment_text = ""
|
69 |
+
for sentiment, score in sentiment_results.items():
|
70 |
+
emoji = get_sentiment_emoji(sentiment)
|
71 |
+
if option == "Sentiment Only":
|
72 |
+
sentiment_text += f"{sentiment} {emoji}\n"
|
73 |
+
elif option == "Sentiment + Score":
|
74 |
+
sentiment_text += f"{sentiment} {emoji}: {score}\n"
|
75 |
+
return sentiment_text
|
76 |
+
|
77 |
+
def inference(audio, sentiment_option):
|
78 |
+
audio = whisper.load_audio(audio)
|
79 |
+
audio = whisper.pad_or_trim(audio)
|
80 |
+
|
81 |
+
mel = whisper.log_mel_spectrogram(audio).to(model.device)
|
82 |
+
|
83 |
+
_, probs = model.detect_language(mel)
|
84 |
+
lang = max(probs, key=probs.get)
|
85 |
+
|
86 |
+
options = whisper.DecodingOptions(fp16=False)
|
87 |
+
result = whisper.decode(model, mel, options)
|
88 |
+
|
89 |
+
sentiment_results = analyze_sentiment(result.text)
|
90 |
+
sentiment_output = display_sentiment_results(sentiment_results, sentiment_option)
|
91 |
+
|
92 |
+
return lang.upper(), result.text, sentiment_output
|
93 |
+
|
94 |
+
title = """<h1 align="center">Audio Sentiment Analysis</h1>"""
|
95 |
+
subtitle = """<h6 align="center">Automatic Speech Recognition</h6>"""
|
96 |
+
image_path = "/Users/rayespinoza/PycharmProjects/AnalyticsProjects/Styles/Arquitecture.jpg"
|
97 |
+
description = """
|
98 |
+
<p align="justify">With cross-modal interaction and AI (tools and pre-trained models in NLP), we can analyze large audio data
|
99 |
+
in real-time, such as recorded conversations, customer service calls, or voice recordings, in order to identify and categorize
|
100 |
+
emotions (from positive and neutral to sad and angry.</p><br>
|
101 |
+
|
102 |
+
Components of the tool:<br>
|
103 |
+
- Input: Real-time multilingual<br>
|
104 |
+
- Video Call speech recognition<br>
|
105 |
+
- Pre-trained model: Whisper<br>
|
106 |
+
- Model size: Large with 769M Parameters<br>
|
107 |
+
- Encoder/Decoder Arquitecture <br>
|
108 |
+
- Transcribe, Translate, and Identify Audio<br>
|
109 |
+
- Output: Sentiment analysis<br>
|
110 |
+
<br>
|
111 |
+
"""
|
112 |
+
|
113 |
+
custom_css = """
|
114 |
+
banner-image {
|
115 |
+
margin-left: auto;
|
116 |
+
margin-right: auto;
|
117 |
+
}
|
118 |
+
chat-message {
|
119 |
+
font-size: 300px;
|
120 |
+
min-height: 600px;
|
121 |
+
}
|
122 |
+
|
123 |
+
img {
|
124 |
+
border-radius: 8px;
|
125 |
+
max-width: 100%;
|
126 |
+
height: auto;
|
127 |
+
}
|
128 |
+
|
129 |
+
"""
|
130 |
+
|
131 |
+
#-----Themes config:
|
132 |
+
|
133 |
+
class Seafoam(Base):
|
134 |
+
def __init__(
|
135 |
+
self,
|
136 |
+
*,
|
137 |
+
primary_hue: colors.Color | str = colors.emerald,
|
138 |
+
secondary_hue: colors.Color | str = colors.blue,
|
139 |
+
neutral_hue: colors.Color | str = colors.blue,
|
140 |
+
spacing_size: sizes.Size | str = sizes.spacing_md,
|
141 |
+
radius_size: sizes.Size | str = sizes.radius_md,
|
142 |
+
text_size: sizes.Size | str = sizes.text_lg,
|
143 |
+
font: fonts.Font
|
144 |
+
| str
|
145 |
+
| Iterable[fonts.Font | str] = (
|
146 |
+
fonts.GoogleFont("Quicksand"),
|
147 |
+
"ui-sans-serif",
|
148 |
+
"sans-serif",
|
149 |
+
),
|
150 |
+
font_mono: fonts.Font
|
151 |
+
| str
|
152 |
+
| Iterable[fonts.Font | str] = (
|
153 |
+
fonts.GoogleFont("IBM Plex Mono"),
|
154 |
+
"ui-monospace",
|
155 |
+
"monospace",
|
156 |
+
),
|
157 |
+
):
|
158 |
+
super().__init__(
|
159 |
+
primary_hue=primary_hue,
|
160 |
+
secondary_hue=secondary_hue,
|
161 |
+
neutral_hue=neutral_hue,
|
162 |
+
spacing_size=spacing_size,
|
163 |
+
radius_size=radius_size,
|
164 |
+
text_size=text_size,
|
165 |
+
font=font,
|
166 |
+
font_mono=font_mono,
|
167 |
+
)
|
168 |
+
super().set(
|
169 |
+
body_background_fill="repeating-linear-gradient(45deg, *primary_200, *primary_200 10px, *primary_50 10px, *primary_50 20px)",
|
170 |
+
body_background_fill_dark="repeating-linear-gradient(45deg, *primary_800, *primary_800 10px, *primary_900 10px, *primary_900 20px)",
|
171 |
+
button_primary_background_fill="linear-gradient(90deg, *primary_300, *secondary_400)",
|
172 |
+
button_primary_background_fill_hover="linear-gradient(90deg, *primary_200, *secondary_300)",
|
173 |
+
button_primary_text_color="white",
|
174 |
+
button_primary_background_fill_dark="linear-gradient(90deg, *primary_600, *secondary_800)",
|
175 |
+
slider_color="*secondary_300",
|
176 |
+
slider_color_dark="*secondary_600",
|
177 |
+
block_title_text_weight="600",
|
178 |
+
block_border_width="3px",
|
179 |
+
block_shadow="*shadow_drop_lg",
|
180 |
+
button_shadow="*shadow_drop_lg",
|
181 |
+
button_large_padding="32px",
|
182 |
+
)
|
183 |
+
|
184 |
+
|
185 |
+
seafoam = Seafoam()
|
186 |
+
#
|
187 |
+
|
188 |
+
lock_symbol = '\U0001F512' # 🔒
|
189 |
+
unlock_symbol = '\U0001F513' # 🔓
|
190 |
+
switch_values_symbol = '\U000021C5' # ⇅
|
191 |
+
|
192 |
+
class FormRow(gr.Row, gr.components.FormComponent):
|
193 |
+
"""Same as gr.Row but fits inside gradio forms"""
|
194 |
+
|
195 |
+
def get_block_name(self):
|
196 |
+
return "row"
|
197 |
+
|
198 |
+
class ToolButton(gr.Button, gr.components.FormComponent):
|
199 |
+
"""Small button with single emoji as text, fits inside gradio forms"""
|
200 |
+
|
201 |
+
def __init__(self, **kwargs):
|
202 |
+
super().__init__(variant="tool", **kwargs)
|
203 |
+
|
204 |
+
def get_block_name(self):
|
205 |
+
return "button"
|
206 |
+
|
207 |
+
def toggle_aspect_ratio(btn):
|
208 |
+
if btn == unlock_symbol:
|
209 |
+
return gr.update(value = lock_symbol, variant="primary")
|
210 |
+
else:
|
211 |
+
return gr.update(value = unlock_symbol, variant="secondary")
|
212 |
+
|
213 |
+
|
214 |
+
#
|
215 |
+
|
216 |
+
with open('styles.css', 'r') as f:
|
217 |
+
css_app = f.read()
|
218 |
+
|
219 |
+
|
220 |
+
block = gr.Blocks(css=custom_css, theme='gradio/default',title="Analytics Projects by Ray Espinoza")
|
221 |
+
#block = gr.Blocks(css=custom_css, title="Analytics Projects by Ray Espinoza")
|
222 |
+
#block = gr.Blocks(css=".gradio-container {background-color: black}", title="Analytics Projects by Ray Espinoza")
|
223 |
+
#block = gr.Blocks(css=".gradio-container {background: url('file=pic4.jpg')}", title="Analytics Projects by Ray Espinoza")
|
224 |
+
|
225 |
+
with block:
|
226 |
+
gr.HTML(title)
|
227 |
+
gr.HTML(subtitle)
|
228 |
+
|
229 |
+
with gr.Row():
|
230 |
+
with gr.Column(scale=2):
|
231 |
+
gr.Image(image_path, elem_id="banner-image", show_label=False, show_download_button=False)
|
232 |
+
#banner-image
|
233 |
+
#gr.Markdown(value=image_path, elem_id="img")
|
234 |
+
#gr.Image(image_path, elem_id="chat-message", show_label=False)
|
235 |
+
with gr.Column():
|
236 |
+
gr.HTML(description)
|
237 |
+
|
238 |
+
with gr.Group():
|
239 |
+
with gr.Box():
|
240 |
+
audio = gr.Audio(
|
241 |
+
label="Input Audio",
|
242 |
+
show_label=False,#Here#False
|
243 |
+
source="microphone",
|
244 |
+
type="filepath"
|
245 |
+
)
|
246 |
+
|
247 |
+
sentiment_option = gr.Radio(
|
248 |
+
choices=["Sentiment Only", "Sentiment + Score"],
|
249 |
+
label="Select an option",
|
250 |
+
default="Sentiment Only"
|
251 |
+
)
|
252 |
+
|
253 |
+
btn = gr.Button("Execute: Transcribe",variant="primary")
|
254 |
+
|
255 |
+
lang_str = gr.Textbox(label="Language:")
|
256 |
+
|
257 |
+
text = gr.Textbox(label="Transcription:")
|
258 |
+
|
259 |
+
sentiment_output = gr.Textbox(label="Sentiment Analysis Results:", output=True)
|
260 |
+
|
261 |
+
btn.click(inference, inputs=[audio, sentiment_option], outputs=[lang_str, text, sentiment_output])
|
262 |
+
|
263 |
+
gr.HTML('''
|
264 |
+
<div class="footer">
|
265 |
+
<p>By <a href="https://github.com" style="text-decoration: underline;" target="_blank"> Ray EH Github</a>
|
266 |
+
</p>
|
267 |
+
</div>
|
268 |
+
''')
|
269 |
+
|
270 |
+
block.launch()
|