Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
import json
|
4 |
+
|
5 |
+
# JSON νμΌ μ½κΈ°
|
6 |
+
with open('/home/judy/command_bot/node.json', 'r') as file:
|
7 |
+
node_info = json.load(file)
|
8 |
+
|
9 |
+
##############################################################################
|
10 |
+
|
11 |
+
class Stack:
|
12 |
+
def __init__(self):
|
13 |
+
self.items = []
|
14 |
+
|
15 |
+
def is_empty(self):
|
16 |
+
return len(self.items) == 0
|
17 |
+
|
18 |
+
def push(self, item):
|
19 |
+
self.items.append(item)
|
20 |
+
|
21 |
+
def pop(self):
|
22 |
+
if not self.is_empty():
|
23 |
+
return self.items.pop()
|
24 |
+
else:
|
25 |
+
return None # μ€νμ΄ λΉμ΄μμ λλ None λ°ν λλ μμΈ μ²λ¦¬λ κ°λ₯
|
26 |
+
|
27 |
+
def peek(self):
|
28 |
+
if not self.is_empty():
|
29 |
+
return self.items[-1]
|
30 |
+
else:
|
31 |
+
return None # μ€νμ΄ λΉμ΄μμ λλ None λ°ν λλ μμΈ μ²λ¦¬λ κ°λ₯
|
32 |
+
|
33 |
+
def size(self):
|
34 |
+
return len(self.items)
|
35 |
+
|
36 |
+
stack = Stack()
|
37 |
+
|
38 |
+
# λ°νμ λͺ
λ Ήμ΄κ° ν¬ν¨λμ΄ μλμ§ νμΈ
|
39 |
+
# λͺ
λ Ήμ΄κ° ν¬ν¨λμ΄ μλ€λ©΄ ν΄λΉ λ
Έλ μΆμΆ
|
40 |
+
def classifier(utterance):
|
41 |
+
|
42 |
+
print(utterance)
|
43 |
+
|
44 |
+
top = stack.peek()
|
45 |
+
|
46 |
+
if utterance == "save":
|
47 |
+
while(True):
|
48 |
+
if stack.size() == 0:
|
49 |
+
return
|
50 |
+
peek = stack.peek()
|
51 |
+
if peek['start_node'] == 3:
|
52 |
+
return
|
53 |
+
else:
|
54 |
+
stack.pop()
|
55 |
+
|
56 |
+
# top node μ dest node λ₯Ό λͺ¨λ κ°μ Έμ¨λ€.
|
57 |
+
for dest_node in top['dest_node']:
|
58 |
+
|
59 |
+
# node_info μμ start_node == dest_node μΈ λ
Έλλ₯Ό μ°Ύλλ€.
|
60 |
+
for node in node_info["items"]:
|
61 |
+
if node['start_node'] == dest_node:
|
62 |
+
# candidate_node.append(node)
|
63 |
+
# μ΅μ λ
Έλλ³΄λ€ λ°ν λ
Έλκ° λ€μ state μΈμ§ νμΈ
|
64 |
+
if top['depth']+1 == node['depth']:
|
65 |
+
# dest_node μ uttr μΆμΆ -> λ°νμ / λ°νκ° ν¬ν¨λλμ§ νμΈ
|
66 |
+
# node['uttrance'] μ κ°μ΄ λ€μ λ€μ΄μλ κ²½μ° λ°°μ΄λ‘ λ°ν
|
67 |
+
# if '\n' in node['utterance']:
|
68 |
+
node_utter = node['utterance'].split('\n')
|
69 |
+
for n in node_utter:
|
70 |
+
if utterance in n or n in utterance:
|
71 |
+
stack.push(node)
|
72 |
+
# print(node['intent'])
|
73 |
+
|
74 |
+
if node['fallback']:
|
75 |
+
while(True):
|
76 |
+
if stack.size() == 0:
|
77 |
+
return
|
78 |
+
peek = stack.peek()
|
79 |
+
if peek['start_node'] == node['fallback']:
|
80 |
+
return str(str(node['depth']) + ':' + node['intent'])
|
81 |
+
else:
|
82 |
+
stack.pop()
|
83 |
+
else:
|
84 |
+
return str(str(node['depth']) + ':' + node['intent'])
|
85 |
+
return str("Can't run this command!")
|
86 |
+
|
87 |
+
# 1. μ°¨νΈ μΉ κ΅¬λ
|
88 |
+
## state = 1
|
89 |
+
stack.push(node_info["items"][0])
|
90 |
+
|
91 |
+
# 2. νλ¦¬μ€ / μνλνΈ κ° μ°¨νΈ λΆκΈ°
|
92 |
+
## νλ¦¬μ€ μ°¨νΈλ‘ λΆκΈ°
|
93 |
+
stack.push(node_info["items"][1])
|
94 |
+
|
95 |
+
|
96 |
+
|
97 |
+
demo = gr.Interface(fn=classifier, inputs="textbox", outputs="textbox",
|
98 |
+
examples=[
|
99 |
+
["start listening"],
|
100 |
+
["select tooth 1"],
|
101 |
+
["pocket depth 1 2 3"],
|
102 |
+
["select tooth 2"],
|
103 |
+
["treatment plan"],
|
104 |
+
["missing"],
|
105 |
+
["select tooth 5 buccal mesial pocket depth"],
|
106 |
+
["save"]
|
107 |
+
]
|
108 |
+
)
|
109 |
+
|
110 |
+
if __name__ == "__main__":
|
111 |
+
demo.launch(share=True)
|