Spaces:
Runtime error
Runtime error
Add Application file
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from statistics import mean
|
2 |
+
import random
|
3 |
+
|
4 |
+
import torch
|
5 |
+
from transformers import BertModel, BertTokenizerFast
|
6 |
+
import numpy as np
|
7 |
+
import torch.nn.functional as F
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
threshold = 0.4
|
11 |
+
|
12 |
+
tokenizer = BertTokenizerFast.from_pretrained("setu4993/LaBSE")
|
13 |
+
model = BertModel.from_pretrained("setu4993/LaBSE")
|
14 |
+
model = model.eval()
|
15 |
+
|
16 |
+
order_food_ex = [
|
17 |
+
"food",
|
18 |
+
"I am hungry, I want to order food",
|
19 |
+
"How do I order food",
|
20 |
+
"What are the food options",
|
21 |
+
"I need dinner",
|
22 |
+
"I want lunch",
|
23 |
+
"What are the menu options",
|
24 |
+
"I want a hamburger"
|
25 |
+
]
|
26 |
+
|
27 |
+
talk_to_human_ex = [
|
28 |
+
"I need to talk to someone",
|
29 |
+
"Connect me with a human",
|
30 |
+
"I need to speak with a person",
|
31 |
+
"Put me on with a human",
|
32 |
+
"Connect me with customer service",
|
33 |
+
"human"
|
34 |
+
]
|
35 |
+
|
36 |
+
def embed(text, tokenizer, model):
|
37 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True)
|
38 |
+
with torch.no_grad():
|
39 |
+
outputs = model(**inputs)
|
40 |
+
return outputs.pooler_output
|
41 |
+
|
42 |
+
def similarity(embeddings_1, embeddings_2):
|
43 |
+
normalized_embeddings_1 = F.normalize(embeddings_1, p=2)
|
44 |
+
normalized_embeddings_2 = F.normalize(embeddings_2, p=2)
|
45 |
+
return torch.matmul(
|
46 |
+
normalized_embeddings_1, normalized_embeddings_2.transpose(0, 1)
|
47 |
+
)
|
48 |
+
|
49 |
+
order_food_embed = [embed(x, tokenizer, model) for x in order_food_ex]
|
50 |
+
talk_to_human_embed = [embed(x, tokenizer, model) for x in talk_to_human_ex]
|
51 |
+
|
52 |
+
def chat(message, history):
|
53 |
+
history = history or []
|
54 |
+
message_embed = embed(message, tokenizer, model)
|
55 |
+
order_sim = []
|
56 |
+
for em in order_food_embed:
|
57 |
+
order_sim.append(float(similarity(em, message_embed)))
|
58 |
+
human_sim = []
|
59 |
+
for em in talk_to_human_embed:
|
60 |
+
human_sim.append(float(similarity(em, message_embed)))
|
61 |
+
if mean(order_sim) > threshold:
|
62 |
+
response = random.choice([
|
63 |
+
"We have hamburgers or pizza! Which one do you want?",
|
64 |
+
"Do you want a hamburger or a pizza?"])
|
65 |
+
elif mean(human_sim) > threshold:
|
66 |
+
response = random.choice([
|
67 |
+
"Sure, a customer service agent will jump into this convo shortly!",
|
68 |
+
"No problem. Let me forward on this conversation to a person that can respond."])
|
69 |
+
else:
|
70 |
+
response = "Sorry, I didn't catch that. Could your rephrase?"
|
71 |
+
history.append((message, response))
|
72 |
+
return history, history
|
73 |
+
|
74 |
+
iface = gr.Interface(
|
75 |
+
chat,
|
76 |
+
["text", "state"],
|
77 |
+
["chatbot", "state"],
|
78 |
+
allow_screenshot=False,
|
79 |
+
allow_flagging="never",
|
80 |
+
)
|
81 |
+
iface.launch()
|