allenai / cached_path

A file utility for accessing both local and remote files through a unified interface.

Home Page:https://cached-path.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use progress bar from Gradio

fakerybakery opened this issue · comments

Hi, great package.
Gradio supports a custom progress bar TQDM-style. Is there any chance to support this?

Example:

import gradio as gr
from cached_path import cached_path
def update(url, progress=gr.Progress()):
	file = str(cached_path(url, progress=progress.tqdm))
	return file
with gr.Blocks() as demo:
    inp = gr.Textbox(label="Enter URL", info="Enter URL")
    out = gr.File(interactive=False)
    btn = gr.Button("Run")
    btn.click(fn=update, inputs=inp, outputs=out)

demo.launch()
commented

Hey @fakerybakery this is definitely doable. We could provide an API for this, but in the meantime you would just need to create a thin wrapper around gr.Progress to pass it to cached_path. I think something like this would suffice:

from typing import Optional


class GrProgressWrapper:
    def __init__(self, gr_progress):
        self.completed = 0
        self.total = 0
        self.inner = gr_progress

    def add_task(self, description: str, total: int) -> int:
        del description
        self.total = total
        return 0  # returns a task_id, doesn't matter unless you're doing multiple downloads with the same bar

    def advance(self, task_id: int, n: int):
        del task_id  # not needed unless you're doing multiple downloads with the same bar
        self.completed += n
        self.inner((self.completed, self.total))

    def update(self, task_id: int, total: Optional[int] = None, completed: Optional[int] = None):
        del task_id  # not needed unless you're doing multiple downloads with the same bar
        if total is not None:
            self.total = total
        if completed is not None:
            self.completed = completed
        self.inner((self.completed, self.total))

I haven't tested this so let me know if you have issues.

Thanks so much! I tried doing something like this previously but wasn't able to figure it out :) Should I close this issue now?

commented

If that worked for you feel free to close :)