Spaces:
Runtime error
Runtime error
File size: 4,439 Bytes
db95b72 992ab47 db95b72 992ab47 db95b72 f18fc4e db95b72 f18fc4e db95b72 992ab47 db95b72 992ab47 db95b72 992ab47 db95b72 992ab47 f18fc4e 992ab47 db95b72 992ab47 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
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()
|