Spaces:
Runtime error
Runtime error
File size: 1,450 Bytes
2666aeb 5786c43 40f9783 2666aeb 4e700c1 a48f178 4e700c1 095b3d6 75a25b5 34eefa6 40f9783 095b3d6 d165717 c7eff79 095b3d6 d165717 34eefa6 40f9783 34eefa6 40f9783 095b3d6 d165717 c7eff79 34eefa6 095b3d6 7d28350 34eefa6 84a7612 944abf9 34eefa6 d165717 34eefa6 |
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 |
import gradio as gr
import time
import random
import json
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:
gr.Markdown("""## Gradio send queryparam to chatbot
type `read query`
""")
url_params = gr.JSON({}, visible=False, label="URL Params")
chatbot = gr.Chatbot().style(height=500)
msg = gr.Textbox()
clear = gr.Button("Clear")
def user(user_message, url_params, history):
return "", history + [[user_message, None]]
def bot(history, url_params):
if "read query" in history[-1][0]:
bot_message = f"""
here your URL params:
{json.dumps(url_params, indent=4)}
"""
else:
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(
fn=bot, inputs=[chatbot, url_params], outputs=[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()
|