Spaces:
Runtime error
Runtime error
yappeizhen
commited on
Commit
β’
667b50a
1
Parent(s):
ddd3909
scoreboard feature and bugfix on empty transcript table
Browse files
app.py
CHANGED
@@ -8,6 +8,24 @@ from dotenv import load_dotenv
|
|
8 |
import base64
|
9 |
import datetime
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
# Load google cloud credentials
|
12 |
load_dotenv()
|
13 |
base64_credentials = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')
|
@@ -18,42 +36,50 @@ db = firestore.Client.from_service_account_info(credentials_json)
|
|
18 |
# ===== Authentication =====
|
19 |
|
20 |
def authenticate(new_username, new_pw):
|
21 |
-
if new_username == '' or new_pw == '': return [None, None, gr.update(), gr.update()]
|
22 |
users_ref = db.collection('Users')
|
23 |
doc_ref = users_ref.document(new_username)
|
24 |
doc = doc_ref.get()
|
25 |
-
|
26 |
if doc.exists:
|
27 |
# User exists in Firestore
|
28 |
user_data = doc.to_dict()
|
29 |
-
|
30 |
# Handle incorrect password
|
31 |
if user_data['password'] != new_pw:
|
32 |
raise gr.Error("Incorrect password")
|
33 |
-
return None
|
34 |
else:
|
35 |
-
doc_ref.set({"username": new_username, "password": new_pw})
|
36 |
|
37 |
gr.Info(f"Welcome, {new_username}!")
|
38 |
-
|
39 |
show_welcome = gr.update(visible=True, value=f'<div style=\'height:190px; display:flex; justify-content:center; align-items:center;\'><h1 style=\'text-align:center\'>Hello {new_username}! π</h1></div>')
|
40 |
hide_signin = gr.update(visible=False)
|
41 |
|
42 |
-
return [new_username, new_pw, show_welcome, hide_signin]
|
43 |
|
44 |
def get_user_transcripts(username):
|
45 |
arr = []
|
46 |
if username is None: return [gr.update(value=arr)]
|
47 |
-
|
48 |
# Fetch user's records
|
49 |
user_transcripts = db.collection(f'Users/{username}/Transcripts').stream()
|
50 |
-
|
51 |
for trans in user_transcripts:
|
52 |
trans_dict = trans.to_dict()
|
53 |
arr.append([trans_dict['date'], trans_dict['transcription'], trans_dict['sentiment_output']])
|
54 |
-
|
|
|
55 |
return arr
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
# ===== Loading Whisper =====
|
59 |
|
@@ -66,6 +92,12 @@ def analyze_sentiment(text):
|
|
66 |
sentiment_results = {result['label']: result['score'] for result in results}
|
67 |
return sentiment_results
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
def get_sentiment_emoji(sentiment):
|
70 |
# Define the emojis corresponding to each sentiment
|
71 |
emoji_mapping = {
|
@@ -123,17 +155,23 @@ def inference(username, audio, sentiment_option):
|
|
123 |
result = whisper.decode(model, mel, options)
|
124 |
|
125 |
sentiment_results = analyze_sentiment(result.text)
|
126 |
-
sentiment_output = display_sentiment_results(sentiment_results, sentiment_option)
|
127 |
-
|
128 |
if username:
|
129 |
# save results in firestore
|
130 |
ts = datetime.datetime.now()
|
131 |
ts_formatted = ts.strftime("%d %b %Y, %H:%M")
|
132 |
-
|
133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
gr.Info("Transcription saved!")
|
135 |
|
136 |
-
return lang.upper(), result.text, sentiment_output
|
137 |
|
138 |
title = """<h1 align="center">β Lim Kopi Call Center Service π¬</h1>"""
|
139 |
image_path = "coffee_logo.jpg"
|
@@ -185,6 +223,7 @@ with app:
|
|
185 |
gr.HTML(title)
|
186 |
authed_username = gr.State(value=None)
|
187 |
authed_password = gr.State(value=None)
|
|
|
188 |
|
189 |
# ===== UI =====
|
190 |
|
@@ -206,7 +245,7 @@ with app:
|
|
206 |
login_btn.click(
|
207 |
authenticate,
|
208 |
inputs=[username_input, password_input],
|
209 |
-
outputs=[authed_username, authed_password, user_welcome, auth_block]
|
210 |
)
|
211 |
|
212 |
with gr.Column(scale=1):
|
@@ -224,17 +263,25 @@ with app:
|
|
224 |
with gr.Row():
|
225 |
with gr.Column():
|
226 |
gr.HTML("<br/>")
|
227 |
-
gr.HTML("""<
|
228 |
with gr.Group():
|
229 |
lang_str = gr.Textbox(label="Language")
|
230 |
text = gr.Textbox(label="Transcription")
|
231 |
sentiment_output = gr.Textbox(label="Sentiment Analysis Results")
|
232 |
-
btn.click(inference, inputs=[authed_username, audio, sentiment_option], outputs=[lang_str, text, sentiment_output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
|
234 |
with gr.Row(visible=True) as transcription_records:
|
235 |
with gr.Column():
|
236 |
gr.HTML("<br/>")
|
237 |
-
gr.HTML("""<
|
238 |
transcription_df = gr.Dataframe(
|
239 |
headers=["Date", "Transcription", "Sentiment"],
|
240 |
datatype=["str", "str", "str"],
|
@@ -245,4 +292,4 @@ with app:
|
|
245 |
sentiment_output.change(get_user_transcripts, inputs=[authed_username], outputs=[transcription_df[0]])
|
246 |
|
247 |
app.queue()
|
248 |
-
app.launch()
|
|
|
8 |
import base64
|
9 |
import datetime
|
10 |
|
11 |
+
# Positive sentiments
|
12 |
+
positive_sentiments = [
|
13 |
+
"approval",
|
14 |
+
"realization",
|
15 |
+
"joy",
|
16 |
+
"caring",
|
17 |
+
"relief",
|
18 |
+
"desire",
|
19 |
+
"admiration",
|
20 |
+
"optimism",
|
21 |
+
"love",
|
22 |
+
"excitement",
|
23 |
+
"curiosity",
|
24 |
+
"amusement",
|
25 |
+
"gratitude",
|
26 |
+
"pride"
|
27 |
+
]
|
28 |
+
|
29 |
# Load google cloud credentials
|
30 |
load_dotenv()
|
31 |
base64_credentials = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')
|
|
|
36 |
# ===== Authentication =====
|
37 |
|
38 |
def authenticate(new_username, new_pw):
|
39 |
+
if new_username == '' or new_pw == '': return [None, None, 0, gr.update(), gr.update()]
|
40 |
users_ref = db.collection('Users')
|
41 |
doc_ref = users_ref.document(new_username)
|
42 |
doc = doc_ref.get()
|
43 |
+
new_score = 0
|
44 |
if doc.exists:
|
45 |
# User exists in Firestore
|
46 |
user_data = doc.to_dict()
|
47 |
+
new_score = user_data['score']
|
48 |
# Handle incorrect password
|
49 |
if user_data['password'] != new_pw:
|
50 |
raise gr.Error("Incorrect password")
|
|
|
51 |
else:
|
52 |
+
doc_ref.set({"username": new_username, "password": new_pw, "score": new_score})
|
53 |
|
54 |
gr.Info(f"Welcome, {new_username}!")
|
|
|
55 |
show_welcome = gr.update(visible=True, value=f'<div style=\'height:190px; display:flex; justify-content:center; align-items:center;\'><h1 style=\'text-align:center\'>Hello {new_username}! π</h1></div>')
|
56 |
hide_signin = gr.update(visible=False)
|
57 |
|
58 |
+
return [new_username, new_pw, new_score, show_welcome, hide_signin]
|
59 |
|
60 |
def get_user_transcripts(username):
|
61 |
arr = []
|
62 |
if username is None: return [gr.update(value=arr)]
|
|
|
63 |
# Fetch user's records
|
64 |
user_transcripts = db.collection(f'Users/{username}/Transcripts').stream()
|
|
|
65 |
for trans in user_transcripts:
|
66 |
trans_dict = trans.to_dict()
|
67 |
arr.append([trans_dict['date'], trans_dict['transcription'], trans_dict['sentiment_output']])
|
68 |
+
if (len(arr) == 0):
|
69 |
+
arr = ['', '', '']
|
70 |
return arr
|
71 |
|
72 |
+
def get_user_score(username):
|
73 |
+
doc = db.document(f'Users/{username}').get()
|
74 |
+
if doc.exists:
|
75 |
+
# User exists in Firestore
|
76 |
+
user_data = doc.to_dict()
|
77 |
+
return [f"""
|
78 |
+
<p align="center">Earn points by making customers happy!</p>
|
79 |
+
<br/>
|
80 |
+
<h1 align="center" style=\'font-size:56px;\'>{user_data["score"]}</h1>
|
81 |
+
"""]
|
82 |
+
return [f'<h1 align="center"></h1>']
|
83 |
|
84 |
# ===== Loading Whisper =====
|
85 |
|
|
|
92 |
sentiment_results = {result['label']: result['score'] for result in results}
|
93 |
return sentiment_results
|
94 |
|
95 |
+
def is_positive(result):
|
96 |
+
result = result.split(' ')[0]
|
97 |
+
if (result in positive_sentiments):
|
98 |
+
return True
|
99 |
+
return False
|
100 |
+
|
101 |
def get_sentiment_emoji(sentiment):
|
102 |
# Define the emojis corresponding to each sentiment
|
103 |
emoji_mapping = {
|
|
|
155 |
result = whisper.decode(model, mel, options)
|
156 |
|
157 |
sentiment_results = analyze_sentiment(result.text)
|
158 |
+
sentiment_output = display_sentiment_results(sentiment_results, sentiment_option)
|
|
|
159 |
if username:
|
160 |
# save results in firestore
|
161 |
ts = datetime.datetime.now()
|
162 |
ts_formatted = ts.strftime("%d %b %Y, %H:%M")
|
163 |
+
ref = db.document(f'Users/{username}')
|
164 |
+
transcript_ref = db.document(f'Users/{username}/Transcripts/{ts_formatted}')
|
165 |
+
transcript_ref.set({"date": ts_formatted, "transcription": result.text, "sentiment_output": sentiment_output})
|
166 |
+
person_doc = ref.get()
|
167 |
+
user_data = person_doc.to_dict()
|
168 |
+
new_score = user_data['score']
|
169 |
+
if is_positive(sentiment_output):
|
170 |
+
new_score = new_score + 1
|
171 |
+
db.document(f'Users/{username}').update({"score": new_score})
|
172 |
gr.Info("Transcription saved!")
|
173 |
|
174 |
+
return lang.upper(), result.text, sentiment_output, new_score
|
175 |
|
176 |
title = """<h1 align="center">β Lim Kopi Call Center Service π¬</h1>"""
|
177 |
image_path = "coffee_logo.jpg"
|
|
|
223 |
gr.HTML(title)
|
224 |
authed_username = gr.State(value=None)
|
225 |
authed_password = gr.State(value=None)
|
226 |
+
user_score = gr.State(value=0)
|
227 |
|
228 |
# ===== UI =====
|
229 |
|
|
|
245 |
login_btn.click(
|
246 |
authenticate,
|
247 |
inputs=[username_input, password_input],
|
248 |
+
outputs=[authed_username, authed_password, user_score, user_welcome, auth_block]
|
249 |
)
|
250 |
|
251 |
with gr.Column(scale=1):
|
|
|
263 |
with gr.Row():
|
264 |
with gr.Column():
|
265 |
gr.HTML("<br/>")
|
266 |
+
gr.HTML("""<h1 align="center">π Results</h1>""")
|
267 |
with gr.Group():
|
268 |
lang_str = gr.Textbox(label="Language")
|
269 |
text = gr.Textbox(label="Transcription")
|
270 |
sentiment_output = gr.Textbox(label="Sentiment Analysis Results")
|
271 |
+
btn.click(inference, inputs=[authed_username, audio, sentiment_option], outputs=[lang_str, text, sentiment_output, user_score])
|
272 |
+
|
273 |
+
with gr.Row(visible=True) as scoreboard:
|
274 |
+
with gr.Column():
|
275 |
+
gr.HTML("<br/>")
|
276 |
+
gr.HTML("""<h1 align="center">π― Your Score</h1>""")
|
277 |
+
score_sheet = gr.HTML(visible=True, value=f'<p align="center">Log in to see your score and transcripts</p>')
|
278 |
+
user_welcome.change(get_user_score, inputs=[authed_username], outputs=[score_sheet])
|
279 |
+
sentiment_output.change(get_user_score, inputs=[authed_username], outputs=[score_sheet])
|
280 |
|
281 |
with gr.Row(visible=True) as transcription_records:
|
282 |
with gr.Column():
|
283 |
gr.HTML("<br/>")
|
284 |
+
gr.HTML("""<h1 align="center"> πͺ© Your Transcription Records</h1>""")
|
285 |
transcription_df = gr.Dataframe(
|
286 |
headers=["Date", "Transcription", "Sentiment"],
|
287 |
datatype=["str", "str", "str"],
|
|
|
292 |
sentiment_output.change(get_user_transcripts, inputs=[authed_username], outputs=[transcription_df[0]])
|
293 |
|
294 |
app.queue()
|
295 |
+
app.launch()
|