Spaces:
Build error
Build error
Add state transitions
Browse files
run.py
CHANGED
|
@@ -1,10 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
-
changeableElemNames=["Input Text","Submit","Button to State 2","Button to State 3","Button to State 4","Button to State 5","Button to State 6"] # Input Text, Submit Button and further buttons
|
|
|
|
|
|
|
| 5 |
elemIDs=list(range(len(changeableElemNames))) # IDs of elements
|
| 6 |
-
defaultElemIDs=list(range(0,len(changeableElemNames)))
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
def toggler(visibility=defaultElemIDs):
|
| 9 |
l=[]
|
| 10 |
l.append(gr.Textbox(changeableElemNames[0], visible=(0 in visibility)))
|
|
@@ -25,17 +43,29 @@ def respond(state, chat_history, visible_elemIDs,internal_state):
|
|
| 25 |
previous_state = internal_state
|
| 26 |
internal_state = state # in this example the internal state is set to the button clicked
|
| 27 |
if(str(state) in [str(e) for e in visible_elemIDs]): # react to Buttons
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
#print("respond to button!")
|
| 30 |
-
chat_history.append((state, bot_message))
|
| 31 |
else: # react to Text-Input
|
| 32 |
internal_state="1"
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
#print("respond to text!")
|
| 35 |
chat_history.append((str(state), bot_message))
|
| 36 |
state="1"
|
| 37 |
-
visible_elemIDs= [e for e in
|
| 38 |
-
if ((str(e) != str(internal_state))|(str(e)=="1")|(str(e)=="0"))] # delete some buttons
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
if(len(visible_elemIDs)<=2): # if visible_elemIDs has length below 3, reset
|
| 40 |
print("restore visibility to "+str(defaultElemIDs))
|
| 41 |
visible_elemIDs=defaultElemIDs
|
|
@@ -43,14 +73,14 @@ def respond(state, chat_history, visible_elemIDs,internal_state):
|
|
| 43 |
return state, chat_history, visible_elemIDs, internal_state
|
| 44 |
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
-
chatbot = gr.Chatbot(value=[[None, f"I'm in state \"
|
| 47 |
changeableElems=toggler()
|
| 48 |
for name, value in zip(changeableElemNames, changeableElems):
|
| 49 |
globals()[name] = value
|
| 50 |
hideInternals=True
|
| 51 |
internalState=gr.Radio(
|
| 52 |
[str(e) for e in elemIDs],
|
| 53 |
-
value=
|
| 54 |
visible=hideInternals==False,
|
| 55 |
label="Internal state") # determine internal state of the chatbot
|
| 56 |
visibility=gr.Dropdown(
|
|
|
|
| 1 |
+
initialState="1"
|
| 2 |
+
transitions=[
|
| 3 |
+
[1,"When are your openin hours?","""We are at your disposal 24/7""",-1],
|
| 4 |
+
[1,"I forgot my password",""""There is no password - just type "Login" and click on "Submit".""",-1],
|
| 5 |
+
[2,"Cool, thank you so much.","""With pleasure. Please let me know if you have further questions?""",-1],
|
| 6 |
+
[2,"I don't find the logout-button.","""Just type "Logout" in the text-box and click on "Submit".""",-1],
|
| 7 |
+
[0,"Are you an AI?","""No, not really.""",-1]
|
| 8 |
+
]
|
| 9 |
+
|
| 10 |
+
conditions=[line[0] for line in transitions]
|
| 11 |
+
intents=[line[1] for line in transitions]
|
| 12 |
+
replies=[line[2] for line in transitions]
|
| 13 |
+
consequences=[line[3] for line in transitions]
|
| 14 |
|
| 15 |
import gradio as gr
|
| 16 |
|
| 17 |
+
#changeableElemNames=["Input Text","Submit","Button to State 2","Button to State 3","Button to State 4","Button to State 5","Button to State 6"] # Input Text, Submit Button and further buttons
|
| 18 |
+
changeableElemNames=["Input Text","Submit"]
|
| 19 |
+
changeableElemNames.extend(intents) #[intents[i] for i in subsetIDs])
|
| 20 |
elemIDs=list(range(len(changeableElemNames))) # IDs of elements
|
| 21 |
+
#defaultElemIDs=list(range(0,len(changeableElemNames)))
|
| 22 |
+
defaultElemIDs=[0,1]
|
| 23 |
+
defaultElemIDs.extend([i+2 for i in range(len(conditions)) if conditions[i]<2]) # IDs of elements visible at the beginnning
|
| 24 |
+
defaultElemIDs
|
| 25 |
+
|
| 26 |
def toggler(visibility=defaultElemIDs):
|
| 27 |
l=[]
|
| 28 |
l.append(gr.Textbox(changeableElemNames[0], visible=(0 in visibility)))
|
|
|
|
| 43 |
previous_state = internal_state
|
| 44 |
internal_state = state # in this example the internal state is set to the button clicked
|
| 45 |
if(str(state) in [str(e) for e in visible_elemIDs]): # react to Buttons
|
| 46 |
+
internal_state=previous_state # default is to not change the state
|
| 47 |
+
intent=intents[int(state)-2]
|
| 48 |
+
reply=replies[int(state)-2]
|
| 49 |
+
condition=conditions[int(state)-2]
|
| 50 |
+
consequence=consequences[int(state)-2]
|
| 51 |
+
if(int(consequence)>=0): internal_state=consequence # react to consequence if specified
|
| 52 |
+
bot_message = f"{reply}\n\n<i><small>[Debugging info: I was in state \"{previous_state}\" and am now in state \"{internal_state}\". please click another button.]</small></i>"
|
| 53 |
#print("respond to button!")
|
| 54 |
+
chat_history.append((intent, bot_message)) #((str(state), bot_message))
|
| 55 |
else: # react to Text-Input
|
| 56 |
internal_state="1"
|
| 57 |
+
if("Login" in state):internal_state="2"
|
| 58 |
+
if("Logout" in state):internal_state="1"
|
| 59 |
+
bot_message = f"I was in state \"{previous_state}\" and am now in state \"{internal_state}\" after processing \"{state}\". please click another button."
|
| 60 |
#print("respond to text!")
|
| 61 |
chat_history.append((str(state), bot_message))
|
| 62 |
state="1"
|
| 63 |
+
visible_elemIDs= [e for e in elemIDs
|
| 64 |
+
#if ((str(e) != str(internal_state))|(str(e)=="1")|(str(e)=="0"))] # delete some buttons
|
| 65 |
+
if(
|
| 66 |
+
(str(e)=="1")|(str(e)=="0")|
|
| 67 |
+
(str(conditions[int(e)-2])=="0")|
|
| 68 |
+
(str(conditions[int(e)-2])==str(internal_state)))]
|
| 69 |
if(len(visible_elemIDs)<=2): # if visible_elemIDs has length below 3, reset
|
| 70 |
print("restore visibility to "+str(defaultElemIDs))
|
| 71 |
visible_elemIDs=defaultElemIDs
|
|
|
|
| 73 |
return state, chat_history, visible_elemIDs, internal_state
|
| 74 |
|
| 75 |
with gr.Blocks() as demo:
|
| 76 |
+
chatbot = gr.Chatbot(value=[[None, f"I'm in state \"{initialState}\". Please click a button!"]])
|
| 77 |
changeableElems=toggler()
|
| 78 |
for name, value in zip(changeableElemNames, changeableElems):
|
| 79 |
globals()[name] = value
|
| 80 |
hideInternals=True
|
| 81 |
internalState=gr.Radio(
|
| 82 |
[str(e) for e in elemIDs],
|
| 83 |
+
value=initialState,
|
| 84 |
visible=hideInternals==False,
|
| 85 |
label="Internal state") # determine internal state of the chatbot
|
| 86 |
visibility=gr.Dropdown(
|