Spaces:
Build error
Build error
Commit
·
6d92cf2
1
Parent(s):
4a1c22a
Update run.py
Browse files
run.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
|
| 8 |
def toggler(visibility=defaultElemIDs):
|
| 9 |
l=[]
|
|
@@ -12,6 +12,7 @@ def toggler(visibility=defaultElemIDs):
|
|
| 12 |
l.append(gr.Button("Button 2", visible=(2 in visibility)))
|
| 13 |
l.append(gr.Button("Button 3", visible=(3 in visibility)))
|
| 14 |
l.append(gr.Button("Button 4", visible=(4 in visibility)))
|
|
|
|
| 15 |
print("Toggler:\n\nelemIDs="+str(elemIDs)+"\n\nVisibility="+str(visibility)+"\n\nl="+str(l)) #+"\n\ntest4="+str(4 in visibility))
|
| 16 |
return l
|
| 17 |
|
|
@@ -21,13 +22,13 @@ def listener(btn):
|
|
| 21 |
if(btn=="Button 2"): r=2
|
| 22 |
if(btn=="Button 3"): r=3
|
| 23 |
if(btn=="Button 4"): r=4
|
|
|
|
| 24 |
print("Listener:\n\nbtn="+str(btn)+"\n\nr="+str(r))
|
| 25 |
return gr.Radio([str(e) for e in elemIDs],label="Button clicked",value=str(r))
|
| 26 |
|
| 27 |
-
def respond(state, chat_history, visible_elemIDs):
|
| 28 |
-
global internal_state
|
| 29 |
previous_state = internal_state
|
| 30 |
-
internal_state = state
|
| 31 |
if(str(state) in [str(e) for e in visible_elemIDs]): # react to Buttons
|
| 32 |
bot_message = f"I was in state \"{previous_state}\" and am now in state \"{internal_state}\". please click another button."
|
| 33 |
#print("respond to button!")
|
|
@@ -44,29 +45,41 @@ def respond(state, chat_history, visible_elemIDs):
|
|
| 44 |
print("restore visibility to "+str(defaultElemIDs))
|
| 45 |
visible_elemIDs=defaultElemIDs
|
| 46 |
print("respond:\n\nvisible_elemIDs="+str(visible_elemIDs)+"\n\nbot_message="+str(bot_message))
|
| 47 |
-
return state, chat_history, visible_elemIDs
|
| 48 |
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
-
chatbot = gr.Chatbot(value=[[None, f"I'm in state \"
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
visibility=gr.Dropdown(
|
| 54 |
elemIDs,
|
| 55 |
-
value=defaultElemIDs,
|
| 56 |
multiselect=True,
|
| 57 |
-
visible=
|
| 58 |
-
label="Active Elements") # determine which elements are visible
|
| 59 |
-
clicked=gr.Radio(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
visibility.change(toggler, inputs=visibility, outputs=changeableElems) # change elemIDs when visibility is changed
|
| 61 |
chatbot.change(toggler, inputs=visibility, outputs=changeableElems) # change elemIDs when chatbot responds
|
| 62 |
-
clicked.change(respond,inputs=[clicked,chatbot,visibility], outputs=[clicked,chatbot,visibility])
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 68 |
with gr.Row():
|
| 69 |
chatbot
|
| 70 |
-
gr.Column([
|
| 71 |
|
| 72 |
demo.launch()
|
|
|
|
| 1 |
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
changeableElemNames=["Input Text","Submit","Button 1","Button 2","Button 3","Button 4"] # Input Text, Submit Button and further buttons
|
| 5 |
+
elemIDs=list(range(len(changeableElemNames))) # IDs of elements
|
| 6 |
+
defaultElemIDs=list(range(0,len(changeableElemNames))) # IDs of elements visible at the beginnning
|
| 7 |
|
| 8 |
def toggler(visibility=defaultElemIDs):
|
| 9 |
l=[]
|
|
|
|
| 12 |
l.append(gr.Button("Button 2", visible=(2 in visibility)))
|
| 13 |
l.append(gr.Button("Button 3", visible=(3 in visibility)))
|
| 14 |
l.append(gr.Button("Button 4", visible=(4 in visibility)))
|
| 15 |
+
l.append(gr.Button("Button 5", visible=(5 in visibility)))
|
| 16 |
print("Toggler:\n\nelemIDs="+str(elemIDs)+"\n\nVisibility="+str(visibility)+"\n\nl="+str(l)) #+"\n\ntest4="+str(4 in visibility))
|
| 17 |
return l
|
| 18 |
|
|
|
|
| 22 |
if(btn=="Button 2"): r=2
|
| 23 |
if(btn=="Button 3"): r=3
|
| 24 |
if(btn=="Button 4"): r=4
|
| 25 |
+
if(btn=="Button 5"): r=5
|
| 26 |
print("Listener:\n\nbtn="+str(btn)+"\n\nr="+str(r))
|
| 27 |
return gr.Radio([str(e) for e in elemIDs],label="Button clicked",value=str(r))
|
| 28 |
|
| 29 |
+
def respond(state, chat_history, visible_elemIDs,internal_state):
|
|
|
|
| 30 |
previous_state = internal_state
|
| 31 |
+
internal_state = state # in this example the internal state is set to the button clicked
|
| 32 |
if(str(state) in [str(e) for e in visible_elemIDs]): # react to Buttons
|
| 33 |
bot_message = f"I was in state \"{previous_state}\" and am now in state \"{internal_state}\". please click another button."
|
| 34 |
#print("respond to button!")
|
|
|
|
| 45 |
print("restore visibility to "+str(defaultElemIDs))
|
| 46 |
visible_elemIDs=defaultElemIDs
|
| 47 |
print("respond:\n\nvisible_elemIDs="+str(visible_elemIDs)+"\n\nbot_message="+str(bot_message))
|
| 48 |
+
return state, chat_history, visible_elemIDs, internal_state
|
| 49 |
|
| 50 |
with gr.Blocks() as demo:
|
| 51 |
+
chatbot = gr.Chatbot(value=[[None, f"I'm in state \"0\". Please click a button!"]])
|
| 52 |
+
changeableElems=toggler()
|
| 53 |
+
for name, value in zip(changeableElemNames, changeableElems):
|
| 54 |
+
globals()[name] = value
|
| 55 |
+
hideInternals=True
|
| 56 |
+
internalState=gr.Radio(
|
| 57 |
+
[str(e) for e in elemIDs],
|
| 58 |
+
value="0",
|
| 59 |
+
visible=hideInternals==False,
|
| 60 |
+
label="Internal state") # determine internal state of the chatbot
|
| 61 |
visibility=gr.Dropdown(
|
| 62 |
elemIDs,
|
| 63 |
+
value=defaultElemIDs,
|
| 64 |
multiselect=True,
|
| 65 |
+
visible=hideInternals==False,
|
| 66 |
+
label="Active Elements") # determine which elements are visible
|
| 67 |
+
clicked=gr.Radio(
|
| 68 |
+
[str(e) for e in elemIDs],
|
| 69 |
+
value="0",
|
| 70 |
+
visible=hideInternals==False,
|
| 71 |
+
label="Button clicked") # determine which Element/Button was clicked last
|
| 72 |
visibility.change(toggler, inputs=visibility, outputs=changeableElems) # change elemIDs when visibility is changed
|
| 73 |
chatbot.change(toggler, inputs=visibility, outputs=changeableElems) # change elemIDs when chatbot responds
|
| 74 |
+
clicked.change(respond,inputs=[clicked,chatbot,visibility,internalState], outputs=[clicked,chatbot,visibility,internalState])
|
| 75 |
+
globals()[changeableElemNames[1]].click(
|
| 76 |
+
respond,
|
| 77 |
+
inputs=[globals()[changeableElemNames[0]],chatbot,visibility,internalState],
|
| 78 |
+
outputs=[globals()[changeableElemNames[0]],chatbot,visibility,internalState])
|
| 79 |
+
for n in [changeableElemNames[x] for x in list(range(2,len(changeableElemNames)))]: # for all elements except 0&1:
|
| 80 |
+
globals()[n].click(listener, inputs=globals()[n], outputs=clicked)
|
| 81 |
with gr.Row():
|
| 82 |
chatbot
|
| 83 |
+
gr.Column([changeableElemNames[x] for x in list(range(1,len(changeableElemNames)))])
|
| 84 |
|
| 85 |
demo.launch()
|