Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -34,6 +34,11 @@ emotion_pipeline = pipeline(
|
|
34 |
model="bhadresh-savani/distilbert-base-uncased-emotion",
|
35 |
device=device,
|
36 |
)
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
EXAMPLES = [["Customer_Support_Call.wav"]]
|
39 |
|
@@ -47,6 +52,30 @@ speech_to_text = partial(
|
|
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.
|
@@ -57,22 +86,12 @@ def sentiment(diarized, emotion_pipeline):
|
|
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)
|
@@ -87,18 +106,8 @@ def sentiment(diarized, emotion_pipeline):
|
|
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 |
-
|
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 |
|
@@ -109,6 +118,11 @@ with gr.Blocks() as demo:
|
|
109 |
|
110 |
gr.Markdown("**Call Transcript:**")
|
111 |
diarized = gr.HighlightedText(label="Call Transcript")
|
|
|
|
|
|
|
|
|
|
|
112 |
sentiment_btn = gr.Button("Get Customer Sentiment")
|
113 |
analyzed = gr.HighlightedText(color_map=color_map)
|
114 |
plot = gr.Plot(label="Sentiment over time", type="plotly")
|
@@ -128,6 +142,8 @@ with gr.Blocks() as demo:
|
|
128 |
inputs=audio,
|
129 |
outputs=diarized,
|
130 |
)
|
|
|
|
|
131 |
|
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])
|
|
|
34 |
model="bhadresh-savani/distilbert-base-uncased-emotion",
|
35 |
device=device,
|
36 |
)
|
37 |
+
summarization_pipeline = pipeline(
|
38 |
+
"summarization",
|
39 |
+
model="knkarthick/MEETING_SUMMARY",
|
40 |
+
device=device
|
41 |
+
)
|
42 |
|
43 |
EXAMPLES = [["Customer_Support_Call.wav"]]
|
44 |
|
|
|
52 |
whisper_device=whisper_device
|
53 |
)
|
54 |
|
55 |
+
def summarize(diarized, check, summarization_pipeline):
|
56 |
+
"""
|
57 |
+
diarized: a list of tuples. Each tuple has a string to be displayed and a label for highlighting.
|
58 |
+
The start/end times are not highlighted [(speaker text, speaker id), (start time/end time, None)]
|
59 |
+
check is a list of speaker ids whose speech will get summarized
|
60 |
+
"""
|
61 |
+
|
62 |
+
if len(check) == 0:
|
63 |
+
return ""
|
64 |
+
|
65 |
+
text = ""
|
66 |
+
for d in diarized:
|
67 |
+
if len(check) == 2 and d[1] is not None:
|
68 |
+
text += f"\n{d[1]}: {d[0]}"
|
69 |
+
elif d[1] in check:
|
70 |
+
text += f"\n{d[0]}"
|
71 |
+
|
72 |
+
# inner function cached because outer function cannot be cached
|
73 |
+
@functools.lru_cache(maxsize=128)
|
74 |
+
def call_summarize_api(text):
|
75 |
+
return summarization_pipeline(text)[0]["summary_text"]
|
76 |
+
|
77 |
+
return call_summarize_api(text)
|
78 |
+
|
79 |
def sentiment(diarized, emotion_pipeline):
|
80 |
"""
|
81 |
diarized: a list of tuples. Each tuple has a string to be displayed and a label for highlighting.
|
|
|
86 |
"""
|
87 |
|
88 |
customer_sentiments = []
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
for i in range(0, len(diarized), 2):
|
91 |
speaker_speech, speaker_id = diarized[i]
|
92 |
times, _ = diarized[i + 1]
|
93 |
|
94 |
sentences = split_into_sentences(speaker_speech)
|
|
|
|
|
|
|
|
|
95 |
if "Customer" in speaker_id:
|
96 |
|
97 |
outputs = emotion_pipeline(sentences)
|
|
|
106 |
sent = "positive"
|
107 |
elif o["label"] in {"sadness", "anger", "fear"}:
|
108 |
sent = "negative"
|
|
|
|
|
|
|
109 |
|
110 |
+
return customer_sentiments
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
with gr.Blocks() as demo:
|
113 |
|
|
|
118 |
|
119 |
gr.Markdown("**Call Transcript:**")
|
120 |
diarized = gr.HighlightedText(label="Call Transcript")
|
121 |
+
gr.Markdown("Choose speaker to summarize:")
|
122 |
+
check = gr.CheckboxGroup(
|
123 |
+
choices=["Customer", "Support"], show_label=False, type="value"
|
124 |
+
)
|
125 |
+
summary = gr.Textbox(lines=4)
|
126 |
sentiment_btn = gr.Button("Get Customer Sentiment")
|
127 |
analyzed = gr.HighlightedText(color_map=color_map)
|
128 |
plot = gr.Plot(label="Sentiment over time", type="plotly")
|
|
|
142 |
inputs=audio,
|
143 |
outputs=diarized,
|
144 |
)
|
145 |
+
# when summarize checkboxes are changed, create summary
|
146 |
+
check.change(fn=partial(summarize, summarization_pipeline=summarization_pipeline), inputs=[diarized, check], outputs=summary)
|
147 |
|
148 |
# when sentiment button clicked, display highlighted text and plot
|
149 |
sentiment_btn.click(fn=partial(sentiment, emotion_pipeline=emotion_pipeline), inputs=diarized, outputs=[analyzed, plot])
|