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

Allow caching generators and async generators #4927

Merged
merged 8 commits into from Jul 14, 2023
Merged

Conversation

abidlabs
Copy link
Member

@abidlabs abidlabs commented Jul 14, 2023

Previously, we couldn't cache examples if the function was a generator or an async generator. This fixes that by getting the last yielded value and using that as the output. I also add a print statement to print out the status of caching. If there's a generator that takes a long time to finish iterating, users can notice that something went wrong and terminate (as suggested by @hysts)

Example:

import gradio as gr
import time

def test_generator(x):
    for y in range(len(x)):
        time.sleep(0.3)
        yield x[: y + 1]

gr.Interface(test_generator, "textbox", "textbox", examples=[["abcdef"]], cache_examples=True).queue().launch()

This will be useful in gr.ChatInterface so that users can cache examples for streaming chatbots.

Closes: #3570

@vercel
Copy link

vercel bot commented Jul 14, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
gradio ✅ Ready (Inspect) Visit Preview Jul 14, 2023 11:33pm

@gradio-pr-bot
Copy link
Contributor

All the demos for this PR have been deployed at https://huggingface.co/spaces/gradio-pr-deploys/pr-4927-all-demos


You can install the changes in this PR by running:

pip install https://gradio-builds.s3.amazonaws.com/9137f1caa015ba361ab2dacdd9b0071645a692ef/gradio-3.36.1-py3-none-any.whl

Copy link
Collaborator

@freddyaboulton freddyaboulton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me @abidlabs - nice we can finally support this

@abidlabs
Copy link
Member Author

Thanks for the awesome fast review @freddyaboulton

@abidlabs abidlabs merged commit e90ad01 into main Jul 14, 2023
12 checks passed
@abidlabs abidlabs deleted the cachegenerators branch July 14, 2023 23:38
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

Successfully merging this pull request may close these issues.

Support generator functions as fn in gr.Example
3 participants