enoreyes commited on
Commit
0eb04fd
1 Parent(s): c39fcf1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -2
app.py CHANGED
@@ -13,7 +13,7 @@ from transformers import pipeline, Wav2Vec2ProcessorWithLM
13
  from pyannote.audio import Pipeline
14
  import whisperx
15
 
16
- from utils import split_into_sentences, sentiment, color_map
17
  from utils import speech_to_text as stt
18
 
19
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
@@ -47,6 +47,59 @@ speech_to_text = partial(
47
  whisper_device=whisper_device
48
  )
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  with gr.Blocks() as demo:
51
 
52
  with gr.Row():
@@ -79,5 +132,4 @@ with gr.Blocks() as demo:
79
  # when sentiment button clicked, display highlighted text and plot
80
  sentiment_btn.click(fn=partial(sentiment, emotion_pipeline=emotion_pipeline), inputs=diarized, outputs=[analyzed, plot])
81
 
82
-
83
  demo.launch(debug=1)
 
13
  from pyannote.audio import Pipeline
14
  import whisperx
15
 
16
+ from utils import split_into_sentences, create_fig, color_map
17
  from utils import speech_to_text as stt
18
 
19
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
 
47
  whisper_device=whisper_device
48
  )
49
 
50
+ def sentiment(diarized, emotion_pipeline):
51
+ """
52
+ diarized: a list of tuples. Each tuple has a string to be displayed and a label for highlighting.
53
+ The start/end times are not highlighted [(speaker text, speaker id), (start time/end time, None)]
54
+
55
+ This function gets the customer's sentiment and returns a list for highlighted text as well
56
+ as a plot of sentiment over time.
57
+ """
58
+
59
+ customer_sentiments = []
60
+ plot_sentences = []
61
+ to_plot = []
62
+
63
+ # used to set the x range of ticks on the plot
64
+ x_min = 100
65
+ x_max = 0
66
+
67
+ for i in range(0, len(diarized), 2):
68
+ speaker_speech, speaker_id = diarized[i]
69
+ times, _ = diarized[i + 1]
70
+
71
+ sentences = split_into_sentences(speaker_speech)
72
+ start_time, end_time = times[5:].split("-")
73
+ start_time, end_time = float(start_time), float(end_time)
74
+ interval_size = (end_time - start_time) / len(sentences)
75
+
76
+ if "Customer" in speaker_id:
77
+
78
+ outputs = emotion_pipeline(sentences)
79
+
80
+ for idx, (o, t) in enumerate(zip(outputs, sentences)):
81
+ sent = "neutral"
82
+ if o["score"] > thresholds[o["label"]]:
83
+ customer_sentiments.append(
84
+ (t + f"({round(idx*interval_size+start_time,1)} s)", o["label"])
85
+ )
86
+ if o["label"] in {"joy", "love", "surprise"}:
87
+ sent = "positive"
88
+ elif o["label"] in {"sadness", "anger", "fear"}:
89
+ sent = "negative"
90
+ if sent != "neutral":
91
+ to_plot.append((start_time + idx * interval_size, sent))
92
+ plot_sentences.append(t)
93
+
94
+ if start_time < x_min:
95
+ x_min = start_time
96
+ if end_time > x_max:
97
+ x_max = end_time
98
+
99
+ fig = create_fig(x_min, x_max, to_plot, plot_sentences)
100
+
101
+ return customer_sentiments, fig
102
+
103
  with gr.Blocks() as demo:
104
 
105
  with gr.Row():
 
132
  # when sentiment button clicked, display highlighted text and plot
133
  sentiment_btn.click(fn=partial(sentiment, emotion_pipeline=emotion_pipeline), inputs=diarized, outputs=[analyzed, plot])
134
 
 
135
  demo.launch(debug=1)