Gradio lauch demo when webpage is opened and close demo when webpage is closed

I made a chatbot web app that uses Gradio Blocks for the interface and makes API calls to ChatGPT to generate responses, similar to this: gradio-chatgpt-app/mywebgpt.py at main · aimerou/gradio-chatgpt-app · GitHub.

I want the model to start fresh and clear all the message history every time I re-load the browser where my app is hosted. But, the only time that happens now is when I re-run my code entirely and run demo.launch() again.

How do I launch my demo each time the browser is opened and close the demo when the browser is closed?

hi @SloganTapleton , have you tried the Block.load() method? it runs every time the demo is refresh in the browser. I’m sure you can leverage it to refresh the model as you need.

Instance method: adds event that runs as soon as the demo loads in the browser.

example

import gradio as gr
import datetime
with gr.Blocks() as demo:
    def get_time():
        return datetime.datetime.now().time()
    dt = gr.Textbox(label="Current time")
    demo.load(get_time, inputs=None, outputs=dt)
demo.launch()
1 Like

This worked! Thank you!

1 Like