Spaces:
Runtime error
Runtime error
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() | |