radames's picture
Update app.py
5786c43
raw
history blame
1.06 kB
import gradio as gr
import time
import random
get_window_url_params = """
function() {
const params = new URLSearchParams(window.location.search);
const url_params = Object.fromEntries(params);
return url_params;
}
"""
with gr.Blocks() as demo:
url_params = gr.JSON({}, visible=True, label="URL Params")
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.Button("Clear")
def user(user_message, url_params, history):
return "", history + [[user_message, None]]
def bot(history):
bot_message = random.choice(["Yes", "No"])
history[-1][1] = bot_message
time.sleep(1)
return history
msg.submit(user, inputs=[msg, url_params, chatbot], outputs=[msg, chatbot], queue=False).then(
bot, chatbot, chatbot
)
clear.click(lambda: None, None, chatbot, queue=False)
demo.load(
fn=lambda x:x,
inputs=[url_params],
outputs=[url_params],
_js=get_window_url_params,
queue=False
)
demo.launch()