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

Concurrent users face the mixing of sessions #8561

Open sajjadmosaheb opened 1 week ago

sajjadmosaheb commented 1 week ago

Describe the bug

I'm using huggingface space to share my app which is a chatbot based on the openai model. I implemented the Chatinterface methode that should handle the session state automatically. but when I open the app in different device and different user and ask about the history of the chat, it mixing the chat up. what should I do?

thanks in advance Sajjad

Have you searched existing issues? 🔎

Reproduction


import openai
import time
import gradio as gr
import os
import re

# Initialize the client

client = openai.OpenAI(api_key =os.getenv("OPENAI_API_KEY"))

assistant_id=os.getenv("assistant_id")

# Step 2: Create a Thread
thread = client.beta.threads.create()

def main(Question,history):
    # Step 3: Add a Message to a Thread
    message = client.beta.threads.messages.create(
        thread_id=thread.id,
        role="user",
        content=Question)

    # Step 4: Run the Assistant
    run = client.beta.threads.runs.create(
        thread_id=thread.id,
        assistant_id=assistant_id)

    while True:
        # Wait for 5 seconds
        time.sleep(5)

        # Retrieve the run status
        run_status = client.beta.threads.runs.retrieve(
            thread_id=thread.id,
            run_id=run.id
        )

        # If run is completed, get messages
        if run_status.status == 'completed':
            messages = client.beta.threads.messages.list(
                thread_id=thread.id
            )
            response = ""            
            content = messages.data[0].content[0].text.value
            response += f"\n\n{content }\n\n"
            return response
        else:
            continue

theme = gr.themes.Soft(
    primary_hue="red",
    secondary_hue="sky",
)

css="footer {visibility: hidden}"
sg = gr.ChatInterface(main,
                        title="Coworker-همکار(نسخه بتا)",
                        css=css,
                        fill_height=True,
                   chatbot=gr.Chatbot(rtl=True,bubble_full_width=False,show_share_button=False,label='Coworker',show_copy_button=True,likeable=True),
                        textbox=gr.Textbox(rtl=True,placeholder="از من بپرس ...", container=False, scale=7),
                        retry_btn=None,
                        analytics_enabled=False,
                        theme=theme,                
                        undo_btn=None,
                        submit_btn="ارسال",
                        clear_btn=gr.ClearButton(value=" 🗑️پاک کردن",size="lg")).queue(default_concurrency_limit =100)

sg.launch() 

Screenshot

No response

Logs

No response

System Info

hugging face space
gradio 4.

Severity

I can work around it

abidlabs commented 1 week ago

Hi @sajjadmosaheb the reason you are running into this issue is because you have a single thread instance that is being shared across all of the users of your session. You will need to create separate threads based on the user that is interaction. You can figure out which users is which by looking at the session_hash of the request, as described here: https://www.gradio.app/docs/gradio/request

We'll add an example in our docs to make this clearer

sajjadmosaheb commented 4 days ago

Hi @abidlabs , Thanks for your response, I saw the demo but can't figure out what's going on, what I should do., and where I add the request function.