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

How to access local media (image/audio/video) within gradio-lite? #8330

Closed
1 task done
Jason-cs18 opened this issue May 19, 2024 · 5 comments
Closed
1 task done

How to access local media (image/audio/video) within gradio-lite? #8330

Jason-cs18 opened this issue May 19, 2024 · 5 comments

Comments

@Jason-cs18
Copy link

Jason-cs18 commented May 19, 2024

  • I have searched to see if a similar issue already exists.
    I want to access the local media within <gradio-lite> but I cannot find solutions. I go through all relevant documents. As shown above, I create an audio component within <gradio-lite> and want to set the default value to a local media (sample_output.mp3). However, I meet 404 error.

environments

  • Ubuntu 20.04 LTS
  • Python: 3.9.19
  • Gradio: 4.31.4
<gradio-lite>
        import gradio as gr
        audio=gr.Audio(label="数字人", scale=0.5, interactive=False, value="/home/ecs-user/web_data/sample_output.mp3")
</gradio-lite>

image

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like
A clear and concise description of what you want to happen.

Additional context
Add any other context or screenshots about the feature request here.

@Jason-cs18 Jason-cs18 changed the title How to add local media (image/audio/video) to gradio-lite? How to access local media (image/audio/video) within gradio-lite? May 19, 2024
@Jason-cs18
Copy link
Author

@whitphx

@whitphx
Copy link
Member

whitphx commented May 20, 2024

Thank you for this post, but unfortunately it is not possible to access local files from Gradio-Lite apps running in the browser because the browser environment (precisely, the Python/Pyodide environment in the browser in which Gradio-Lite runs) is completely isolated from the host OS.

An alternative solution is:

  1. serve the file through a web server so users can access it via a URL, e.g. http://your-host/sample.mp3
  2. mount it onto the Python env on the browser by using the gradio-file tag as below:
<gradio-lite>
import gradio as gr

demo = gr.Interface(
  fn=lambda x: x,
  inputs=gr.Audio(value="sample.mp3"),
  outputs=gr.Audio()
)

demo.launch()

<gradio-file name="sample.mp3" url="http://your-host/sample.mp3" />

</gradio-lite>

@Jason-cs18
Copy link
Author

Jason-cs18 commented May 20, 2024

Thank you for this post, but unfortunately it is not possible to access local files from Gradio-Lite apps running in the browser because the browser environment (precisely, the Python/Pyodide environment in the browser in which Gradio-Lite runs) is completely isolated from the host OS.

An alternative solution is:

  1. serve the file through a web server so users can access it via a URL, e.g. http://your-host/sample.mp3
  2. mount it onto the Python env on the browser by using the gradio-file tag as below:
<gradio-lite>
import gradio as gr

demo = gr.Interface(
  fn=lambda x: x,
  inputs=gr.Audio(value="sample.mp3"),
  outputs=gr.Audio()
)

demo.launch()

<gradio-file name="sample.mp3" url="http://your-host/sample.mp3" />

</gradio-lite>

Thanks for your reply. If I use requests to get a video (named example.mp4) from remote API, how to access it?

<gradio-lite>
    audio_response = requests.post("https://xxxx/app/audio", json=myobj)
    audio_path = f"sample_output.mp3"
    with open(audio_path, 'wb') as file:
        for chunk in audio_response.iter_content(chunk_size=1024):
            file.write(chunk)

    # how to access sample_output.mp3?
</gradio-lite>

@whitphx
Copy link
Member

whitphx commented May 20, 2024

In that case, your Gradio-Lite app should be able to access the locally saved file "sample_output.mp3" by simply opening it, e.g. open("sample_output.mp3") or gr.Audio(value="sample_output.mp3").

@abidlabs
Copy link
Member

Thanks @whitphx and @Jason-cs18, I'll go ahead and close this.

@abidlabs abidlabs closed this as not planned Won't fix, can't repro, duplicate, stale May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants