gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
30.56k stars 2.27k forks source link

Can Gradio actively modify the State of a session based on its unique identifier? #8527

Closed coolboyqu closed 2 weeks ago

coolboyqu commented 2 weeks ago

Can the State of an opened session be modified based on the session hash or any unique session identifier?I want to record the session ID and proactively change the State corresponding to all sessions when user data changes.Is this currently possible?

Of course, I can also actively search the database every second to observe whether the corresponding user data has changed, but this may consume a lot of network resources in situations with a large number of users.

abidlabs commented 2 weeks ago

Hi @coolboyqu IIUC you could do something like this:

import gradio as gr

def update_state(request: gr.Request):
    session_hash = request.session_hash
    # do something with session_hash
    return 123

with gr.Blocks() as demo:
    s = gr.State()
    demo.load(update_state, None, s)  # at page load, s will store "123"

demo.launch()

if this does not answer your question, please provide more details and a code snippet that explains what you're trying to do. You can ask in this thread, or preferably, for general questions like this (that are not feature requests or bug reports), please ask in our Discord server. (I'll close this issue)

coolboyqu commented 2 weeks ago

Hi @coolboyqu IIUC you could do something like this:

import gradio as gr

def update_state(request: gr.Request):
    session_hash = request.session_hash
    # do something with session_hash
    return 123

with gr.Blocks() as demo:
    s = gr.State()
    demo.load(update_state, None, s)  # at page load, s will store "123"

demo.launch()

if this does not answer your question, please provide more details and a code snippet that explains what you're trying to do. You can ask in this thread, or preferably, for general questions like this (that are not feature requests or bug reports), please ask in our Discord server. (I'll close this issue)

I'm sorry I didn't describe my problem clearly. What I meant was to find this session based on its unique identifier. For example, I recorded a row of data {cookie: 'abcde', session identifier: [1,2,3,4,5]} in the database. Before updating this database cookie to a new value, I would like to modify the State of several session operations with cookie 'abcde' based on [1,2,3,4,5]

abidlabs commented 2 weeks ago

Its still not clear to me, what is this "session identifier" here?

coolboyqu commented 2 weeks ago

Its still not clear to me, what is this "session identifier" here?

Similar to session hash, a value which can uniquely mark the session.

abidlabs commented 2 weeks ago

Ok so I think my code snippet should provide a solution, right? You can access the session_hash and then look it up a db from within your function?