0

I have a project in Gradio and I want to change it to API. I have some questions :

  • How Gradio manage session ? Can I do the same with Flask ? ; I see that Gradio doesn't use user ID but still keep managing users efficiently. The thing I want to do with Flask.
  • Is it possible to use Gradio as API ?
welu
  • 329
  • 3
  • 12

1 Answers1

0

Gradio supports session state, where data persists across multiple submits within a page load. Session state is useful for building demos of, for example, chatbots where you want to persist data as the user interacts with the model. Note that session state does not share data between different users of your model.

To store data in a session state, you need to do three things:

Pass in an extra parameter into your function, which represents the state of the interface. At the end of the function, return the updated value of the state as an extra return value. Add the ‘state’ input and ‘state’ output components when creating your Interface. See the chatbot example below:

import random

import gradio as gr


def chat(message, history):
    history = history or []
    if message.startswith("How many"):
        response = random.randint(1, 10)
    elif message.startswith("How"):
        response = random.choice(["Great", "Good", "Okay", "Bad"])
    elif message.startswith("Where"):
        response = random.choice(["Here", "There", "Somewhere"])
    else:
        response = "I don't know"
    history.append((message, response))
    return history, history


iface = gr.Interface(
    chat,
    ["text", "state"],
    ["chatbot", "state"],
    allow_screenshot=False,
    allow_flagging="never",
)
iface.launch()
Luke
  • 11
  • 2