Spaces:
Build error
Build error
| import gradio as gr | |
| changeableElemNames=["Input Text","Submit","Button 1","Button 2","Button 3","Button 4"] # Input Text, Submit Button and further buttons | |
| elemIDs=list(range(len(changeableElemNames))) # IDs of elements | |
| defaultElemIDs=list(range(0,len(changeableElemNames))) # IDs of elements visible at the beginnning | |
| def toggler(visibility=defaultElemIDs): | |
| l=[] | |
| l.append(gr.Textbox("Input Text", visible=(0 in visibility))) | |
| l.append(gr.Button("Submit", visible=(1 in visibility))) | |
| l.append(gr.Button("Button 2", visible=(2 in visibility))) | |
| l.append(gr.Button("Button 3", visible=(3 in visibility))) | |
| l.append(gr.Button("Button 4", visible=(4 in visibility))) | |
| l.append(gr.Button("Button 5", visible=(5 in visibility))) | |
| print("Toggler:\n\nelemIDs="+str(elemIDs)+"\n\nVisibility="+str(visibility)+"\n\nl="+str(l)) #+"\n\ntest4="+str(4 in visibility)) | |
| return l | |
| def listener(btn): | |
| r=0 | |
| if(btn=="Submit"): r=1 | |
| if(btn=="Button 2"): r=2 | |
| if(btn=="Button 3"): r=3 | |
| if(btn=="Button 4"): r=4 | |
| if(btn=="Button 5"): r=5 | |
| 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 | |
| bot_message = f"I was in state \"{previous_state}\" and am now in state \"{internal_state}\". please click another button." | |
| #print("respond to button!") | |
| chat_history.append((state, bot_message)) | |
| else: # react to Text-Input | |
| internal_state="1" | |
| bot_message = f"I was in state \"{previous_state}\" and am now in state \"{internal_state}\" and ready to process \"{state}\". please click another button." | |
| #print("respond to text!") | |
| chat_history.append((str(state), bot_message)) | |
| state="1" | |
| visible_elemIDs= [e for e in visible_elemIDs | |
| if ((str(e) != str(internal_state))|(str(e)=="1")|(str(e)=="0"))] # delete some buttons | |
| if(len(visible_elemIDs)==0): # if visible_elemIDs has zero length, 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 \"0\". 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="0", | |
| 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() | |