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.34k stars 2.26k forks source link

Absolute paths are not working for images mount_gradio_app #8235

Closed snps-ravinu closed 1 month ago

snps-ravinu commented 1 month ago

Describe the bug

I have an image in my "assests" folder, while using mount_gradio_app, the gradio.Blocks object contains "img" in html like this,, , but this is not rendering the image, I tried to give relative path as well. Whenever image is in a different directory which is outside the app.py(where mounting is done), I am seeing this issue.

Have you searched existing issues? 🔎

Reproduction

import gradio as gr
  1. create a simple flask/fast app
  2. use mount_gradio_app to mount your ui.
  3. use gradio.Markdown in your gradio.Blocks and add img to the html.
  4. Make sure the image is in a folder outside the file containing the fast app

Screenshot

No response

Logs

No response

System Info

4.16.0

Severity

I can work around it

freddyaboulton commented 1 month ago

Hi @snps-ravinu ! This is expected. Gradio can't serve arbitrary files from your computer/server for security reasons. To serve a file from outside the app directory, use the allowed_paths argument of mount_gradio_app.

The following works for me. Note that I am running this from the same directory as the app.py. If I was running it from a different directory, I would have to use the full path to the static dir in the markdown component.

import gradio as gr
from fastapi import FastAPI

with gr.Blocks() as demo:
    gr.Markdown("<img src='/file=static/cheetah.jpg' width='300px'>")

app = FastAPI()

app = gr.mount_gradio_app(app=app, blocks=demo, path="/",
    allowed_paths=["<full-path-to-static-dir>/static"])

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, port=7860)
snps-ravinu commented 1 month ago

Thanks @freddyaboulton, to update on the issue, I was using allowed paths, but the issue was with the path I was giving in the img src, my ui mount path was "/ui", so I had to insert "/ui" to the src path and that resolved the issue