ButtonBot / run.py
AFischer1985's picture
Update run.py
0cfb089 verified
initialState="1"
transitions=[
[1,"When are your opening hours?","""We are at your disposal 24/7""",-1],
[1,"I forgot my password",""""There is no password - just type "Login" and click on "Submit".""",-1],
[2,"Cool, thank you so much.","""With pleasure. Please let me know if you have further questions?""",-1],
[2,"I don't find the logout-button.","""Just type "Logout" in the text-box and click on "Submit".""",-1],
[0,"Are you an AI?","""No, not really.""",-1]
]
conditions=[line[0] for line in transitions]
intents=[line[1] for line in transitions]
replies=[line[2] for line in transitions]
consequences=[line[3] for line in transitions]
import gradio as gr
#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
changeableElemNames=["Input Text","Submit"]
changeableElemNames.extend(intents) #[intents[i] for i in subsetIDs])
elemIDs=list(range(len(changeableElemNames))) # IDs of elements
#defaultElemIDs=list(range(0,len(changeableElemNames)))
defaultElemIDs=[0,1]
defaultElemIDs.extend([i+2 for i in range(len(conditions)) if conditions[i]<2]) # IDs of elements visible at the beginnning
defaultElemIDs
def toggler(visibility=defaultElemIDs):
l=[]
l.append(gr.Textbox(changeableElemNames[0], visible=(0 in visibility)))
l.append(gr.Button(changeableElemNames[1], visible=(1 in visibility), variant="primary"))
for i in list(range(2,len(changeableElemNames))):
l.append(gr.Button(changeableElemNames[i], visible=(i in visibility)))
#print("Toggler:\n\nelemIDs="+str(elemIDs)+"\n\nVisibility="+str(visibility)+"\n\nl="+str(l))
return l
def listener(btn):
r=0
for i in list(range(1,len(changeableElemNames))):
if(btn==changeableElemNames[i]): r=i
#print("Listener:\n\nbtn="+str(btn)+"\n\nr="+str(r))
return gr.Radio([str(e) for e in elemIDs],label="Button clicked",value=str(r))
def respond(state, chat_history, visible_elemIDs,internal_state):
previous_state = internal_state
internal_state = state # in this example the internal state is set to the button clicked
if(str(state) in [str(e) for e in visible_elemIDs]): # react to Buttons
internal_state=previous_state # default is to not change the state
intent=intents[int(state)-2]
reply=replies[int(state)-2]
condition=conditions[int(state)-2]
consequence=consequences[int(state)-2]
if(int(consequence)>=0): internal_state=consequence # react to consequence if specified
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>"
#print("respond to button!")
chat_history.append((intent, bot_message)) #((str(state), bot_message))
else: # react to Text-Input
internal_state="1"
if("Login" in state):internal_state="2"
if("Logout" in state):internal_state="1"
bot_message = f"I was in state \"{previous_state}\" and am now in state \"{internal_state}\" after processing \"{state}\". please click another button."
#print("respond to text!")
chat_history.append((str(state), bot_message))
state="1"
visible_elemIDs= [e for e in elemIDs
#if ((str(e) != str(internal_state))|(str(e)=="1")|(str(e)=="0"))] # delete some buttons
if(
(str(e)=="1")|(str(e)=="0")|
(str(conditions[int(e)-2])=="0")|
(str(conditions[int(e)-2])==str(internal_state)))]
if(len(visible_elemIDs)<=2): # if visible_elemIDs has length below 3, reset
print("restore visibility to "+str(defaultElemIDs))
visible_elemIDs=defaultElemIDs
print("respond:\n\nvisible_elemIDs="+str(visible_elemIDs)+"\n\nbot_message="+str(bot_message))
return state, chat_history, visible_elemIDs, internal_state
with gr.Blocks() as demo:
chatbot = gr.Chatbot(value=[[None, f"I'm in state \"{initialState}\". Please click a button!"]])
changeableElems=toggler()
for name, value in zip(changeableElemNames, changeableElems):
globals()[name] = value
hideInternals=True
internalState=gr.Radio(
[str(e) for e in elemIDs],
value=initialState,
visible=hideInternals==False,
label="Internal state") # determine internal state of the chatbot
visibility=gr.Dropdown(
elemIDs,
value=defaultElemIDs,
multiselect=True,
visible=hideInternals==False,
label="Active Elements") # determine which elements are visible
clicked=gr.Radio(
[str(e) for e in elemIDs],
value="0",
visible=hideInternals==False,
label="Button clicked") # determine which Element/Button was clicked last
visibility.change(toggler, inputs=visibility, outputs=changeableElems) # change elemIDs when visibility is changed
chatbot.change(toggler, inputs=visibility, outputs=changeableElems) # change elemIDs when chatbot responds
clicked.change(respond,inputs=[clicked,chatbot,visibility,internalState], outputs=[clicked,chatbot,visibility,internalState])
globals()[changeableElemNames[1]].click(
respond,
inputs=[globals()[changeableElemNames[0]],chatbot,visibility,internalState],
outputs=[globals()[changeableElemNames[0]],chatbot,visibility,internalState])
for n in [changeableElemNames[x] for x in list(range(2,len(changeableElemNames)))]: # for all elements except 0&1:
globals()[n].click(listener, inputs=globals()[n], outputs=clicked)
with gr.Row():
chatbot
gr.Column([changeableElemNames[x] for x in list(range(1,len(changeableElemNames)))])
demo.launch()