I have a simple Gradio Chatbot where the User selects a Name from a list.
Each Name has a different (respond_agent) script that needs to run when the User submits a message.
<code>import gradio as gr
from utils2_new_Andrew import *
from utils2_new_Jake import *
from utils2_new_Mike import *
from utils2_new_Alex import *
with gr.Blocks() as demo:
#Name List
supportedLLMs = {"Andrew": 1, "Jake": 2, "Mike": 3, "Alex": 4}
#Default Name
NAME = "Andrew"
#Dropdown
LLMChoice = gr.Radio(choices = list(supportedLLMs.keys()), label = "Avatar", value = "Andrew", interactive = True)
#Update Name variable on Selection
def updateName(newName):
NAME = newName
print("Changed Avatar to " + NAME)
return NAME
LLMChoice.change(updateName, inputs = [LLMChoice], outputs = [])
#Basic Chatbot UI
with gr.Row():
with gr.Column():
chatbot = gr.Chatbot(height=600)
msg = gr.Textbox(placeholder="Talk to me")
with gr.Row():
with gr.Column():
submit_bt = gr.Button(value="Submit", variant="primary")
with gr.Column():
clear = gr.ClearButton([msg, chatbot])
#Change Submit Button to go to Name's Specific Script
if NAME == "Andrew":
submit_bt.click(respond_agent_gradio_wrapper_Andrew,inputs=[msg, chatbot],outputs=[msg, chatbot],)
if NAME == 'Jake':
submit_bt.click(respond_agent_gradio_wrapper_Jake,inputs=[msg, chatbot],outputs=[msg, chatbot],)
if NAME == "Alex":
submit_bt.click(respond_agent_gradio_wrapper_Alex,inputs=[msg, chatbot],outputs=[msg, chatbot],)
if NAME == "Mike":
submit_bt.click(respond_agent_gradio_wrapper_Mike,inputs=[msg, chatbot],outputs=[msg, chatbot],)
if __name__ == "__main__":
print("TT")
demo.launch(share=False)
</code>
<code>import gradio as gr
from utils2_new_Andrew import *
from utils2_new_Jake import *
from utils2_new_Mike import *
from utils2_new_Alex import *
with gr.Blocks() as demo:
#Name List
supportedLLMs = {"Andrew": 1, "Jake": 2, "Mike": 3, "Alex": 4}
#Default Name
NAME = "Andrew"
#Dropdown
LLMChoice = gr.Radio(choices = list(supportedLLMs.keys()), label = "Avatar", value = "Andrew", interactive = True)
#Update Name variable on Selection
def updateName(newName):
NAME = newName
print("Changed Avatar to " + NAME)
return NAME
LLMChoice.change(updateName, inputs = [LLMChoice], outputs = [])
#Basic Chatbot UI
with gr.Row():
with gr.Column():
chatbot = gr.Chatbot(height=600)
msg = gr.Textbox(placeholder="Talk to me")
with gr.Row():
with gr.Column():
submit_bt = gr.Button(value="Submit", variant="primary")
with gr.Column():
clear = gr.ClearButton([msg, chatbot])
#Change Submit Button to go to Name's Specific Script
if NAME == "Andrew":
submit_bt.click(respond_agent_gradio_wrapper_Andrew,inputs=[msg, chatbot],outputs=[msg, chatbot],)
if NAME == 'Jake':
submit_bt.click(respond_agent_gradio_wrapper_Jake,inputs=[msg, chatbot],outputs=[msg, chatbot],)
if NAME == "Alex":
submit_bt.click(respond_agent_gradio_wrapper_Alex,inputs=[msg, chatbot],outputs=[msg, chatbot],)
if NAME == "Mike":
submit_bt.click(respond_agent_gradio_wrapper_Mike,inputs=[msg, chatbot],outputs=[msg, chatbot],)
if __name__ == "__main__":
print("TT")
demo.launch(share=False)
</code>
import gradio as gr
from utils2_new_Andrew import *
from utils2_new_Jake import *
from utils2_new_Mike import *
from utils2_new_Alex import *
with gr.Blocks() as demo:
#Name List
supportedLLMs = {"Andrew": 1, "Jake": 2, "Mike": 3, "Alex": 4}
#Default Name
NAME = "Andrew"
#Dropdown
LLMChoice = gr.Radio(choices = list(supportedLLMs.keys()), label = "Avatar", value = "Andrew", interactive = True)
#Update Name variable on Selection
def updateName(newName):
NAME = newName
print("Changed Avatar to " + NAME)
return NAME
LLMChoice.change(updateName, inputs = [LLMChoice], outputs = [])
#Basic Chatbot UI
with gr.Row():
with gr.Column():
chatbot = gr.Chatbot(height=600)
msg = gr.Textbox(placeholder="Talk to me")
with gr.Row():
with gr.Column():
submit_bt = gr.Button(value="Submit", variant="primary")
with gr.Column():
clear = gr.ClearButton([msg, chatbot])
#Change Submit Button to go to Name's Specific Script
if NAME == "Andrew":
submit_bt.click(respond_agent_gradio_wrapper_Andrew,inputs=[msg, chatbot],outputs=[msg, chatbot],)
if NAME == 'Jake':
submit_bt.click(respond_agent_gradio_wrapper_Jake,inputs=[msg, chatbot],outputs=[msg, chatbot],)
if NAME == "Alex":
submit_bt.click(respond_agent_gradio_wrapper_Alex,inputs=[msg, chatbot],outputs=[msg, chatbot],)
if NAME == "Mike":
submit_bt.click(respond_agent_gradio_wrapper_Mike,inputs=[msg, chatbot],outputs=[msg, chatbot],)
if __name__ == "__main__":
print("TT")
demo.launch(share=False)
The Name selection works, and is updated within the function, but the variable isn’t being updated outside of the function (it stays on the default “Andrew”)
New contributor
user21767968 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.