GirishKiran commited on
Commit
a1fd92e
1 Parent(s): 1ad1917

Upload ./app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +49 -18
app.py CHANGED
@@ -81,6 +81,13 @@ class Utility(object):
81
  repo_type="space")
82
  return
83
 
 
 
 
 
 
 
 
84
  # System Info : Fetch available CPU and RAM of the system
85
  def fetch_system_info(self):
86
  s=''
@@ -150,14 +157,14 @@ class Utility(object):
150
  y = f.encrypt(p)
151
  return y
152
 
153
- # Capitalize : Capitalizes the first letter of each word in a list.
154
  def capitalize_first_letter(self, list_of_words):
155
  capitalized_words = []
156
  for word in list_of_words:
157
  capitalized_word = word[0].upper() + word[1:]
158
  capitalized_words.append(capitalized_word)
159
  return capitalized_words
160
-
161
  # Add method to class
162
  def add_method(cls):
163
  def decorator(func):
@@ -171,16 +178,15 @@ def add_method(cls):
171
  """ This file contains multiple Python classes and responssible to provide Emotions based on the given user input
172
  Currently it supports emotions like Anger, Joy, Optimism and Sadness"""
173
 
174
- from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
175
  from matplotlib.colors import LinearSegmentedColormap
176
  import scipy
177
  import scipy.special
178
  import pandas
 
179
 
180
  class SentimentAnalyser(object):
181
 
182
- global utility
183
-
184
  # initialize the object
185
  def __init__(self, name="Sentiment",*args, **kwargs):
186
  super(SentimentAnalyser, self).__init__(*args, **kwargs)
@@ -195,9 +201,9 @@ class SentimentAnalyser(object):
195
  utility._ph()
196
  print(utility.fetch_system_info())
197
  utility._ph()
198
- print(utility.fetch_gpu_info())
199
  utility._ph()
200
- print(utility.fetch_host_ip())
201
  utility._ph()
202
  self._init_model()
203
  utility._login_hface()
@@ -205,9 +211,10 @@ class SentimentAnalyser(object):
205
 
206
  # initalise the model
207
  def _init_model(self):
208
- modelLink = "bhadresh-savani/distilbert-base-uncased-emotion"
209
  self.tokenizer = AutoTokenizer.from_pretrained(modelLink)
210
  self.model = AutoModelForSequenceClassification.from_pretrained(modelLink)
 
211
  return
212
 
213
  sentiment = SentimentAnalyser(name="EmotionAnalyser")
@@ -218,16 +225,15 @@ def _predict_sentiment(p):
218
  inputs = sentiment.tokenizer(p, return_tensors="pt")
219
  # Pass inputs through model
220
  outputs = sentiment.model(**inputs)
221
- print(outputs)
222
  out_data = outputs[0][0]
223
  scores = out_data.detach().numpy()
224
- print(out_data)
225
  scores = scipy.special.softmax(scores)
226
- sentiment_map = sentiment.utility.capitalize_first_letter(sentiment.model.config.label2id.keys())
227
  df_out = pandas.DataFrame([scores], columns=sentiment_map)
228
  df_out = df_out[['Love' , 'Joy', 'Surprise' , 'Fear', 'Sadness', 'Anger']]
229
  return df_out
230
-
231
  @add_method(SentimentAnalyser)
232
  def draw_bar_plot(df_data, title='Sentiment Analysis', xlabel='p string', ylabel='Emotion Score'):
233
  graphCmap=LinearSegmentedColormap.from_list('gr',["g", "w", "r"])
@@ -239,19 +245,41 @@ def draw_bar_plot(df_data, title='Sentiment Analysis', xlabel='p string', ylabel
239
  return pic
240
 
241
  @add_method(SentimentAnalyser)
242
- def predict_sentiment(p):
243
- df_out = _predict_sentiment(p)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
  max_column = df_out.loc[0].idxmax()
245
  max_value = df_out.loc[0].max()
246
  title = f'Sentiment Analysis: {max_column}: {round(max_value*100,1)}%'
247
- xlabel= f'Input: {p}'
248
  pic = draw_bar_plot(df_out, title=title, xlabel=xlabel)
249
  return pic.get_figure(), df_out.to_json()
250
 
251
  import gradio
252
- in_box = [gradio.Textbox(lines=1, label="Input", placeholder="type text here")]
 
 
 
 
 
 
253
  out_box = [gradio.Plot(label="Sentiment Score:"),
254
  gradio.Textbox(lines=4, label="Raw JSON Response:")]
 
255
  title = "Sentiment Analysis: Understanding the Emotional Tone of Text"
256
  desc = "Sentiment analysis is a powerful tool that can be used to gain insights into how people feel about the world around them."
257
  exp = [
@@ -260,10 +288,13 @@ exp = [
260
  ]
261
  arti= "<b>DistilBERT is 27 times faster than OpenAI, making it the clear winner for speed-sensitive applications.</b>\n\nWe did a comparision of OpenAI vs DestilBert model (which we are currently using in this space) by running 31 sentences in a loop and found DestilBert is 27 times faster than OpenAI."
262
 
263
- gradio.Interface(fn=predict_sentiment,
264
  inputs=in_box,
265
  outputs=out_box,
266
  title=title,
267
  description=desc,
268
  examples=exp,
269
- article=arti).launch(debug=True)
 
 
 
 
81
  repo_type="space")
82
  return
83
 
84
+ # Hugging face : Login to Hugging face
85
+ def _login_hface(self):
86
+ huggingface_hub.login(self._decrypt_it(self._huggingface_key),
87
+ add_to_git_credential=True) # non-blocking login
88
+ self._ph()
89
+ return
90
+
91
  # System Info : Fetch available CPU and RAM of the system
92
  def fetch_system_info(self):
93
  s=''
 
157
  y = f.encrypt(p)
158
  return y
159
 
160
+ # Capitalizes the first letter of each word in a list.
161
  def capitalize_first_letter(self, list_of_words):
162
  capitalized_words = []
163
  for word in list_of_words:
164
  capitalized_word = word[0].upper() + word[1:]
165
  capitalized_words.append(capitalized_word)
166
  return capitalized_words
167
+
168
  # Add method to class
169
  def add_method(cls):
170
  def decorator(func):
 
178
  """ This file contains multiple Python classes and responssible to provide Emotions based on the given user input
179
  Currently it supports emotions like Anger, Joy, Optimism and Sadness"""
180
 
181
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
182
  from matplotlib.colors import LinearSegmentedColormap
183
  import scipy
184
  import scipy.special
185
  import pandas
186
+ import whisper
187
 
188
  class SentimentAnalyser(object):
189
 
 
 
190
  # initialize the object
191
  def __init__(self, name="Sentiment",*args, **kwargs):
192
  super(SentimentAnalyser, self).__init__(*args, **kwargs)
 
201
  utility._ph()
202
  print(utility.fetch_system_info())
203
  utility._ph()
204
+ # print(utility.fetch_gpu_info())
205
  utility._ph()
206
+ # print(utility.fetch_host_ip())
207
  utility._ph()
208
  self._init_model()
209
  utility._login_hface()
 
211
 
212
  # initalise the model
213
  def _init_model(self):
214
+ modelLink = "bhadresh-savani/distilbert-base-uncased-emotion" #"SamLowe/roberta-base-go_emotions"
215
  self.tokenizer = AutoTokenizer.from_pretrained(modelLink)
216
  self.model = AutoModelForSequenceClassification.from_pretrained(modelLink)
217
+ self.whisper_model = whisper.load_model("small")
218
  return
219
 
220
  sentiment = SentimentAnalyser(name="EmotionAnalyser")
 
225
  inputs = sentiment.tokenizer(p, return_tensors="pt")
226
  # Pass inputs through model
227
  outputs = sentiment.model(**inputs)
228
+ sentiment_map = sentiment.utility.capitalize_first_letter(sentiment.model.config.label2id.keys())
229
  out_data = outputs[0][0]
230
  scores = out_data.detach().numpy()
 
231
  scores = scipy.special.softmax(scores)
232
+ # sentiment_map = ['Sadness', 'Joy', 'Love', 'Anger', 'Fear' , "Surprise"]
233
  df_out = pandas.DataFrame([scores], columns=sentiment_map)
234
  df_out = df_out[['Love' , 'Joy', 'Surprise' , 'Fear', 'Sadness', 'Anger']]
235
  return df_out
236
+
237
  @add_method(SentimentAnalyser)
238
  def draw_bar_plot(df_data, title='Sentiment Analysis', xlabel='p string', ylabel='Emotion Score'):
239
  graphCmap=LinearSegmentedColormap.from_list('gr',["g", "w", "r"])
 
245
  return pic
246
 
247
  @add_method(SentimentAnalyser)
248
+ def inference(audio):
249
+ audio = whisper.load_audio(audio)
250
+ audio = whisper.pad_or_trim(audio)
251
+
252
+ mel = whisper.log_mel_spectrogram(audio).to(sentiment.whisper_model.device)
253
+
254
+ _, probs = sentiment.whisper_model.detect_language(mel)
255
+
256
+ options = whisper.DecodingOptions(fp16 = False)
257
+ result = whisper.decode(sentiment.whisper_model, mel, options)
258
+
259
+ print(result.text)
260
+ return result.text
261
+
262
+ @add_method(SentimentAnalyser)
263
+ def predict_sentiment(input_text, input_audio = None, audio_text_btn = None):
264
+ df_out = _predict_sentiment(input_text)
265
  max_column = df_out.loc[0].idxmax()
266
  max_value = df_out.loc[0].max()
267
  title = f'Sentiment Analysis: {max_column}: {round(max_value*100,1)}%'
268
+ xlabel= f'Input: {input_text}'
269
  pic = draw_bar_plot(df_out, title=title, xlabel=xlabel)
270
  return pic.get_figure(), df_out.to_json()
271
 
272
  import gradio
273
+ whisper_audio = gradio.Audio(label="Audio Input",
274
+ source="microphone",
275
+ type="filepath")
276
+ whisper_button = gradio.Button("Convert Audio to Text")
277
+ input_text = gradio.Textbox(lines=1, label="Text Input", placeholder="type text here")
278
+
279
+ in_box = [input_text,whisper_audio,whisper_button]
280
  out_box = [gradio.Plot(label="Sentiment Score:"),
281
  gradio.Textbox(lines=4, label="Raw JSON Response:")]
282
+
283
  title = "Sentiment Analysis: Understanding the Emotional Tone of Text"
284
  desc = "Sentiment analysis is a powerful tool that can be used to gain insights into how people feel about the world around them."
285
  exp = [
 
288
  ]
289
  arti= "<b>DistilBERT is 27 times faster than OpenAI, making it the clear winner for speed-sensitive applications.</b>\n\nWe did a comparision of OpenAI vs DestilBert model (which we are currently using in this space) by running 31 sentences in a loop and found DestilBert is 27 times faster than OpenAI."
290
 
291
+ with gradio.Interface(fn=predict_sentiment,
292
  inputs=in_box,
293
  outputs=out_box,
294
  title=title,
295
  description=desc,
296
  examples=exp,
297
+ article=arti) as demo:
298
+ with gradio.Blocks() as block:
299
+ whisper_button.click(inference, inputs=[whisper_audio], outputs=[input_text])
300
+ demo.launch(debug=True)