Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrent users face the mixing of sessions #8561

Closed
1 task done
sajjadmosaheb opened this issue Jun 16, 2024 · 2 comments · Fixed by #8676
Closed
1 task done

Concurrent users face the mixing of sessions #8561

sajjadmosaheb opened this issue Jun 16, 2024 · 2 comments · Fixed by #8676
Labels
bug Something isn't working docs/website Related to documentation or website

Comments

@sajjadmosaheb
Copy link

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? 🔎

  • I have searched and found no 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

@sajjadmosaheb sajjadmosaheb added the bug Something isn't working label Jun 16, 2024
@abidlabs abidlabs added the docs/website Related to documentation or website label Jun 19, 2024
@abidlabs
Copy link
Member

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
Copy link
Author

sajjadmosaheb commented Jun 22, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working docs/website Related to documentation or website
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants