Spaces:
Build error
Build error
File size: 4,175 Bytes
26bc02a 6d92cf2 26bc02a b0b903f 26bc02a 4f6f8bd 26bc02a 6d92cf2 26bc02a 6d92cf2 26bc02a 6d92cf2 26bc02a 6d92cf2 78c8391 6169ebe 78c8391 4a1c22a 78c8391 6169ebe 4a1c22a c34d1b1 78c8391 26bc02a 6d92cf2 26bc02a 6d92cf2 26bc02a 6d92cf2 26bc02a 6d92cf2 26bc02a 6d92cf2 26bc02a 6d92cf2 26bc02a | 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 |
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()
|