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

404 Assets Not Found Error with OAuth2 Gradio on Localhost #8453

Open yjtan0819 opened 3 weeks ago

yjtan0819 commented 3 weeks ago

Describe the bug

Hello,

I've been experimenting with the OAuth2 Gradio example and have encountered an issue. Whenever I launch a server on my localhost, I consistently receive a "404 Not Found" error related to assets.

I've tried troubleshooting this issue but haven't been able to resolve it yet. Any assistance or guidance on how to fix this would be greatly appreciated.

Thank you.

Have you searched existing issues? πŸ”Ž

Reproduction

from authlib.integrations.starlette_client import OAuth, OAuthError
from fastapi import FastAPI, Depends, Request
from starlette.config import Config
from starlette.responses import RedirectResponse
from starlette.middleware.sessions import SessionMiddleware
import uvicorn
import gradio as gr
import hashlib

app = FastAPI()

# Replace these with your own OAuth settings
AZURE_CLIENT_ID = ""
AZURE_CLIENT_SECRET = ""
AZURE_TENANT_ID = ""
SECRET_KEY = hashlib.sha256(AZURE_CLIENT_SECRET.encode()).hexdigest()

config_data = {'AZURE_CLIENT_ID': AZURE_CLIENT_ID, 'AZURE_CLIENT_SECRET': AZURE_CLIENT_SECRET}
starlette_config = Config(environ=config_data)
oauth = OAuth(starlette_config)
oauth.register(
    name='azure',
    server_metadata_url='https://login.microsoftonline.com/{tenant_id}/v2.0/.well-known/openid-configuration'.format(tenant_id=AZURE_TENANT_ID),
    client_kwargs={'scope': 'openid email profile'},
)
app.add_middleware(SessionMiddleware, secret_key=SECRET_KEY)

# Dependency to get the current user
def get_user(request: Request):
    user = request.session.get('user')
    if user:
        return user
    return None

@app.get('/')
def public(user: dict = Depends(get_user)):
    if user:
        return RedirectResponse(url='/gradio')
    else:
        return RedirectResponse(url='/login-demo')

@app.route('/logout')
async def logout(request: Request):
    request.session.pop('user', None)
    return RedirectResponse(url='/')

@app.route('/login')
async def login(request: Request):
    redirect_uri = request.url_for('auth')
    return await oauth.azure.authorize_redirect(request, redirect_uri)

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

with gr.Blocks() as login_demo:
    gr.Button("Login", link="/login")

app = gr.mount_gradio_app(app, login_demo, path="/login-demo", root_path="/login-demo")

def greet(request: gr.Request):
    name = request.username['name']
    preferred_username = request.username['preferred_username']
    roles = request.username['roles']
    return f"Welcome to Gradio, {name}! Your preferred username is {preferred_username} and your roles are {roles}."

with gr.Blocks() as main_demo:
    m = gr.Markdown("Welcome to Gradio!")
    gr.Button("Logout", link="/logout")
    main_demo.load(greet, None, m)

app = gr.mount_gradio_app(app, main_demo, path="/gradio", auth_dependency=get_user, root_path="/gradio")

if __name__ == '__main__':
    uvicorn.run("app:app", host='localhost', port=8000)

Screenshot

image

Logs

No response

System Info

Operating System: Linux
gradio version: 4.32.2
gradio_client version: 0.17.0

Severity

I can work around it