JudyChoi commited on
Commit
db95b72
β€’
1 Parent(s): 09017b0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
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)