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

Issue with Mounting Multiple Gradio Interfaces using gr.mount_gradio_app #8428

Closed
1 task done
yjtan0819 opened this issue May 31, 2024 · 4 comments
Closed
1 task done
Labels
bug Something isn't working

Comments

@yjtan0819
Copy link

Describe the bug

I am currently facing an issue when trying to mount two Gradio interfaces using the gr.mount_gradio_app function.

Here's what I am experiencing:

  • When I mount the first Gradio interface at the root path ('/'), it works as expected and I can access it without any problems.
  • However, when I try to mount the second Gradio interface at the path '/gradio', I am unable to reach it.

Have you searched existing issues? 🔎

  • I have searched and found no existing issues

Reproduction

import gradio as gr
from fastapi import FastAPI
import uvicorn

app = FastAPI()

def check_login_state(is_logged_in):
    if is_logged_in:
        return "Login", "Logout"
    else:
        return "Logout", "Login"
 
io = gr.TabbedInterface([
    gr.Interface(fn=lambda x: x, inputs="text", outputs="text"),
    gr.Interface(fn=lambda x: x, inputs="text", outputs="text")
])

app = gr.mount_gradio_app(app, io, path='/gradio')

app = gr.mount_gradio_app(app, io, path='/', auth_dependency = check_login_state)

if __name__ == "__main__":
    uvicorn.run(app)

Screenshot

image

Logs

No response

System Info

Gradio Environment Information:
------------------------------
Operating System: Linux
gradio version: 4.32.1
gradio_client version: 0.17.0

------------------------------------------------

Severity

I can work around it

@yjtan0819 yjtan0819 added the bug Something isn't working label May 31, 2024
@freddyaboulton
Copy link
Collaborator

I have not looked into it but I think this makes sense. When you mount gradio at the root /, the request to /gradio will be sent to the gradio app at the root, which does not have a way to fulfill that request.

Does this problem happen if the first app is not mounted at /? Or if you switch the order of the mounting (first mount at /gradio then /)?

@yjtan0819
Copy link
Author

yjtan0819 commented May 31, 2024

The problem does not happen if the first app is not mounted at /, but it still happens if I switch the order of the mounting.

@freddyaboulton
Copy link
Collaborator

This is the intended behavior of FastAPI so I will close.

from fastapi import FastAPI

parent = FastAPI()

root = FastAPI()
child = FastAPI()

@root.get("/")
def hello():
    return "Hello"

@child.get("/hi")
def hi():
    return "hi"

parent.mount("/", root)
parent.mount("/child", child)

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(parent, port=7860)

I suggest you don't place either app at the root and redirect visitors of the home page to whichever child app you want.

from fastapi import FastAPI
from fastapi.responses import RedirectResponse

parent = FastAPI()

root = FastAPI()
child = FastAPI()

@parent.get("/")
def go_to_root():
    return RedirectResponse("/root")

@root.get("/")
def hello():
    return "Hello"

@child.get("/")
def hi():
    return "hi"

parent.mount("/root", root)
parent.mount("/child", child)

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(parent, port=7860)

@pievalentin
Copy link

Thank you, @freddyaboulton very helpful !

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

No branches or pull requests

3 participants