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
29.49k stars 2.19k forks source link

Feature Request/Question: How to access fastapi request object within gradio demo for multiuser auth? #8212

Closed Hellisotherpeople closed 2 weeks ago

Hellisotherpeople commented 2 weeks ago

How does one access a fastapi request object within gradio itself?

Let's say I do auth like this:

@app.route('/auth')
async def auth(request: Request):
    try:
        token = await oauth.google.authorize_access_token(request)
    except OAuthError:
        return RedirectResponse(url='/')
    user = token.get('userinfo')
    if user:
        request.session['user'] = dict(user)
        request.session['token'] = token

    return RedirectResponse(url='/')

How do I access the request.session object from within a Gr.blocks demo? like this

with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            chatbot = gr.Chatbot(label="Personalized Chatbot 1", show_copy_button = True)
        with gr.Column():
            chatbot2 = gr.Chatbot(label = "Personalized Chatbot 2", show_copy_button = True)
        with gr.Column():
            chatbot3 = gr.Chatbot(label = "Personalized Chatbot 3", show_copy_button = True) 
    system_prompt = gr.Textbox(label="System Prompt", value="You are a helpful AI named Wandy")
    msg = gr.Textbox(label="User Input", value="What do you think is the most interesting event in history?")
    clear = gr.Button("Clear")
    emails_textbox = gr.Textbox(label="Extracted Emails", interactive=False)  # Add this line    
    with gr.Accordion("Advanced Settings", open = False):
        temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature")
        max_length = gr.Slider(minimum=10, maximum=1024, value=100, step=1, label="Max Length")
    with gr.Accordion("Chatbot Ratings", open = True):
        with gr.Row():
            model1_score = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="Model 1 Output Quality")
            model2_score = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="Model 2 Output Quality")
            model3_score = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="Model 1 Output Quality")
        with gr.Row():
            model1_score_per = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="Model 1 Personalization Quality")
            model2_score_per = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="Model 2 Personalization Quality")
            model3_score_per = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="Model 1 Personalization Quality") 
        rating_button = gr.Button("Rate Chatbots")

    chatbot_models = random.sample(models, 3)

    msg.submit(user, [msg, chatbot, chatbot2, chatbot3, system_prompt], [msg, chatbot, chatbot2, chatbot3, system_prompt], queue=True).then(
        bot, [chatbot, chatbot2, chatbot3, temperature, max_length, system_prompt], [chatbot, chatbot2, chatbot3, system_prompt]
    )
    clear.click(clear_history, None, [chatbot, chatbot2, chatbot3, system_prompt], queue=True)
    rating_button.click(save_ratings, [chatbot, chatbot2, chatbot3, model1_score, model2_score, model3_score, model1_score_per, model2_score_per, model3_score_per], None)

app = gr.mount_gradio_app(app, demo, path="/gradio")

app.add_middleware(HTTPSRedirectMiddleware)

if __name__ == '__main__':
    uvicorn.run(app, port=8080, host='0.0.0.0', ssl_certfile='/etc/letsencrypt/live/llm-arena.wand.ai/fullchain.pem', ssl_keyfile='/etc/letsencrypt/live/llm-arena.wand.ai/privkey.pem')

I ask this question as I am implementing an app based on the gradio google oauth example, but the example doesn't show how to handle multiple users being simultaneously logged in while not overwriting their auth tokens - this causes data leakage, and means that (at least with my code) if a new user logs into google oauth when someone else is using the app, than the new users token is leaked to all other users.

freddyaboulton commented 2 weeks ago

Hi @Hellisotherpeople ! You can pass gr.Request to your function. Please see the docs here: https://www.gradio.app/docs/gradio/request#request-header .

Gonna close for now.

Hellisotherpeople commented 2 weeks ago

@freddyaboulton I'm aware of the existence of gr.Request, but I do not know how to use it in the context of the exact code that I've linked here.

Beyond that, I am extremely frustrated when I am linked to documentation with almost no other assistance on my question. I have seen this documentation that you have linked but it's not good enough alone, and I am still stuck

Please show me a specific example of using gr.Request in the context of the code I specified to get access to the request.session object.

Hellisotherpeople commented 2 weeks ago

Please reopen this issue or show me a real code example in the context of the code I linked.

Hellisotherpeople commented 2 weeks ago

BTW, I'm pretty desperate to get a proper, working solution for this. I'm willing to pay 200USD for a working solution.

I tried to get folks on the discord to help, but no one there is helpful.