command-bot / app.py
JudyChoi's picture
Update app.py
f18fc4e verified
import gradio as gr
import re
import json
# JSON 파일 읽기
with open('perio_demo.json', 'r') as file:
node_info = json.load(file)
##############################################################################
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return len(self.items) == 0
def push(self, item):
self.items.append(item)
def pop(self):
if not self.is_empty():
return self.items.pop()
else:
return None # μŠ€νƒμ΄ λΉ„μ–΄μžˆμ„ λ•ŒλŠ” None λ°˜ν™˜ λ˜λŠ” μ˜ˆμ™Έ μ²˜λ¦¬λ„ κ°€λŠ₯
def peek(self):
if not self.is_empty():
return self.items[-1]
else:
return None # μŠ€νƒμ΄ λΉ„μ–΄μžˆμ„ λ•ŒλŠ” None λ°˜ν™˜ λ˜λŠ” μ˜ˆμ™Έ μ²˜λ¦¬λ„ κ°€λŠ₯
def size(self):
return len(self.items)
def clear(self):
self.items = [] # μŠ€νƒμ„ λΉ„μš°κΈ°
stack = Stack()
# λ°œν™”μ— λͺ…λ Ήμ–΄κ°€ ν¬ν•¨λ˜μ–΄ μžˆλŠ”μ§€ 확인
# λͺ…λ Ήμ–΄κ°€ ν¬ν•¨λ˜μ–΄ μžˆλ‹€λ©΄ ν•΄λ‹Ή λ…Έλ“œ μΆ”μΆœ
def classifier(utterance):
print(utterance)
if utterance == "reset":
stack.clear()
stack.push(node_info["items"][0])
stack.push(node_info["items"][1])
return str("Reset!")
top = stack.peek()
global_check = re.compile(r'^#global')
# top node 의 dest node λ₯Ό λͺ¨λ‘ κ°€μ Έμ˜¨λ‹€.
for dest_node in top['dest_node']:
# node_info μ—μ„œ start_node == dest_node 인 λ…Έλ“œλ₯Ό μ°ΎλŠ”λ‹€.
for node in node_info["items"]:
if node["utterance"] == utterance:
if node["fallback"] is not None:
if global_check.match(node["fallback"]):
node["fallback"] = int(re.sub(r'\D', '', node["fallback"]))
while(True):
if stack.size() == 0:
return
peek = stack.peek()
if peek['start_node'] == node["fallback"]:
return str(str(node['depth']) + ':' + node['intent'])
else:
stack.pop()
if node['start_node'] == dest_node:
# candidate_node.append(node)
# μ΅œμ‹  λ…Έλ“œλ³΄λ‹€ λ°œν™” λ…Έλ“œκ°€ λ‹€μŒ state 인지 확인
if top['depth']+1 == node['depth']:
# dest_node 의 uttr μΆ”μΆœ -> λ°œν™”μ— / λ°œν™”κ°€ ν¬ν•¨λ˜λŠ”μ§€ 확인
# node['uttrance'] 에 값이 λ‹€μˆ˜ λ“€μ–΄μžˆλŠ” 경우 λ°°μ—΄λ‘œ λ°˜ν™˜
# if '\n' in node['utterance']:
node_utter = node['utterance'].split('\n')
for n in node_utter:
if utterance in n or n in utterance:
stack.push(node)
# print(node['intent'])
if node['fallback']:
while(True):
if stack.size() == 0:
return
peek = stack.peek()
if peek['start_node'] == node['fallback']:
return str(str(node['depth']) + ':' + node['intent'])
else:
stack.pop()
else:
return str(str(node['depth']) + ':' + node['intent'])
return str("Can't run this command!")
# 1. 차트 μ›Ή ꡬ동
## state = 1
stack.push(node_info["items"][0])
# 2. 페리였 / μž„ν”Œλž€νŠΈ 각 차트 λΆ„κΈ°
## 페리였 차트둜 λΆ„κΈ°
stack.push(node_info["items"][1])
demo = gr.Interface(fn=classifier, inputs="textbox", outputs="textbox",
examples=[
["start listening"],
["select tooth 1"],
["pocket depth 1 2 3"],
["select tooth 2"],
["treatment note"],
["missing"],
["select tooth 5 buccal mesial pocket depth"],
["save"],
["stop listening"],
["reset"]
])
if __name__ == "__main__":
demo.launch()