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 update a label while a method function call is running #7419

Closed
FurkanGozukara opened this issue Feb 14, 2024 · 7 comments
Closed

How to update a label while a method function call is running #7419

FurkanGozukara opened this issue Feb 14, 2024 · 7 comments
Labels
bug Something isn't working

Comments

@FurkanGozukara
Copy link

FurkanGozukara commented Feb 14, 2024

Asking GPT-4 but all answers it gives me is wrong. This drives me crazy but cant be helped.

What I want is so simple.

I want to update a gr.label or whatever that is possible while a function is running

E.g. like below. so meanwhile batch_caption_images is running, i will update status label and display to the user the status of the operation instead of looking at the CMD window

Gradio latest version

status_label = gr.Label()

    def batch_caption_images(folder_path, text_input, status_label):
        if not os.path.isdir(folder_path):
            return "Invalid folder path"
        valid_extensions = ('.png', '.jpg', '.jpeg')
        global status_message
        images = [f for f in os.listdir(folder_path) if f.lower().endswith(valid_extensions)]
        total_images = len(images)
        captions = {}
        start_time = time.time()
        for idx, img_name in enumerate(images):
            img_path = os.path.join(folder_path, img_name)
            img = Image.open(img_path)
            caption = generate_predictions(img, text_input, is_batch=True)
            captions[img_name] = caption

            # Update Gradio label
            processed_images = idx + 1
            remaining_images = total_images - processed_images
            average_time_per_image = (time.time() - start_time) / processed_images
            estimated_time_remaining = average_time_per_image * remaining_images
            status_message = f"Processed {processed_images}/{total_images} images, " \
                             f"{remaining_images} images left, " \
                             f"Average processing time: {average_time_per_image:.2f} seconds, " \
                             f"Estimated time remaining: {estimated_time_remaining:.2f} seconds"
            status_label = status_message
        timestamp = datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
        batch_file_name = f"{timestamp}_batch_list.txt"
        with open(os.path.join(folder_path, batch_file_name), 'w', encoding='utf-8') as f:
            for img_name, caption in captions.items():
                f.write(f"{img_name}: {caption}\n")

        return captions
@FurkanGozukara FurkanGozukara added the bug Something isn't working label Feb 14, 2024
@freddyaboulton
Copy link
Collaborator

Hi @FurkanGozukara I think you have the following options:

  • Use gr.Progress to display progress over the output component (https://www.gradio.app/docs/progress#progress-header)
  • Turn your function into a generator, e.g. yield None, status_label each time you want to update progress and at the end yield captions, None.

Will close because I don't think this is a bug in gradio - happy to discuss this further! Thanks!

@FurkanGozukara
Copy link
Author

@freddyaboulton can you give me how to use progress according to my script?

thank you so much

@FurkanGozukara
Copy link
Author

FurkanGozukara commented Feb 14, 2024

@freddyaboulton i made it work like this but still not working as expected can you help?


    def batch_caption_images(folder_path, text_input, status_label, progress=gr.Progress(track_tqdm=True)):
        if not os.path.isdir(folder_path):
            return "Invalid folder path"
        valid_extensions = ('.png', '.jpg', '.jpeg')
        global status_message
        images = [f for f in os.listdir(folder_path) if f.lower().endswith(valid_extensions)]
        total_images = len(images)
        captions = {}
        start_time = time.time()
        
        for idx, img_name in tqdm.tqdm(enumerate(images), desc="Batch Captioning Images", total=len(images)):

it is shown like this

image

@FurkanGozukara
Copy link
Author

@abidlabs can you also help me with this issue if possible? thank you

#7419 (comment)

@abidlabs
Copy link
Member

Sure @FurkanGozukara happy to help. Have you seen this demo: https://github.com/gradio-app/gradio/blob/main/demo/progress/run.py

It shows how to use gr.Progress in different ways, including track_tqdm

@FurkanGozukara
Copy link
Author

Sure @FurkanGozukara happy to help. Have you seen this demo: https://github.com/gradio-app/gradio/blob/main/demo/progress/run.py

It shows how to use gr.Progress in different ways, including track_tqdm

awesome made it work

that demos was great. how do we reach such demos for other stuff?

image

@abidlabs
Copy link
Member

All of our latest demos are here: https://github.com/gradio-app/gradio/tree/main/demo (someone needs to feed these into a LLM + RAG)

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