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

Handle async funcs within sync scopes #1547

Merged
merged 10 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 51 additions & 25 deletions gradio.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,36 @@ Description-Content-Type: text/markdown
License-File: LICENSE

<!-- DO NOT EDIT THIS FILE DIRECTLY. INSTEAD PLEASE EDIT: "readme_template.md" or
"guides/getting_started.md", AND THEN RUN: "python render_readme.py" -->

[![CircleCI](https://circleci.com/gh/gradio-app/gradio.svg?style=svg)](https://circleci.com/gh/gradio-app/gradio) [![PyPI version](https://badge.fury.io/py/gradio.svg)](https://badge.fury.io/py/gradio) [![codecov](https://codecov.io/gh/gradio-app/gradio/branch/master/graph/badge.svg?token=NNVPX9KEGS)](https://codecov.io/gh/gradio-app/gradio) [![PyPI - Downloads](https://img.shields.io/pypi/dm/gradio)](https://pypi.org/project/gradio/) [![Twitter Follow](https://img.shields.io/twitter/follow/gradio.svg?style=social&label=Follow)](https://twitter.com/gradio)

# Gradio: Build Machine Learning Web Apps — in Python

Gradio (pronounced GRAY-dee-oh) is an open-source Python library that is used to build machine learning and data science demos and web applications.

With Gradio, you can quickly create a beautiful user interface around your machine learning models or data science workflow and let people "try it out" by dragging-and-dropping in their own images, pasting text, recording their own voice, and interacting with your demo, all through the browser.
"guides/getting_started.md", AND THEN RUN: "python render_readme.py" -->
Copy link
Member

@abidlabs abidlabs Jun 14, 2022

Choose a reason for hiding this comment

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

Why are these changes inside the gradio.egg-info here? Can we remove them to clean up the PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you talking about all of them or some of them?
These are automatically generated when I used bash install_gradio.sh, it seems my machine generates it a little different, could you also try running it on your machine, maybe that will fix them?

Copy link
Member

Choose a reason for hiding this comment

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

Interesting ok

<p align="center">
<a href="https://gradio.app"><img src="website/homepage/src/assets/img/logos/gradio.svg" alt="gradio" width=300></a>
</p>
<p align="center">
<em>Build & share delightful machine learning apps easily</em>
<p align="center">
<a href="https://circleci.com/gh/gradio-app/gradio" target="_blank">
<img src="https://circleci.com/gh/gradio-app/gradio.svg?style=svg" alt="circleci">
</a>
<a href="https://badge.fury.io/py/gradio" target="_blank">
<img src="https://badge.fury.io/py/gradio.svg" alt="pypi">
</a>
<a href="https://codecov.io/gh/gradio-app/gradio/branch/master/graph/badge.svg?token=NNVPX9KEGS" target="_blank">
<img src="https://codecov.io/gh/gradio-app/gradio/branch/master/graph/badge.svg?token=NNVPX9KEGS" alt="Codecov">
</a>
<a href="https://pypi.org/project/gradio/" target="_blank">
<img src="https://img.shields.io/pypi/dm/gradio" alt="pypi">
</a>
<a href="https://twitter.com/gradio " target="_blank">
<img src="https://img.shields.io/twitter/follow/gradio.svg?style=social&label=Follow" alt="twitter">
</a>
</p>

# Gradio: Build Machine Learning Web Apps — in Python

Gradio is an open-source Python library that is used to build machine learning and data science demos and web applications.

With Gradio, you can quickly create a beautiful user interface around your machine learning models or data science workflow and let people "try it out" by dragging-and-dropping in their own images,
pasting text, recording their own voice, and interacting with your demo, all through the browser.

![Interface montage](website/homepage/src/assets/img/meta-image-2.png)

Expand All @@ -35,8 +56,7 @@ Gradio is useful for:

**You can find an interactive version of the following Getting Started at [https://gradio.app/getting_started](https://gradio.app/getting_started).**


## Quickstart
## Quickstart

**Prerequisite**: Gradio requires Python 3.7 or above, that's it!

Expand All @@ -54,7 +74,7 @@ Gradio allows you to **build demos and share them, all in Python.** And usually

To get Gradio running with a simple "Hello, World" example, follow these three steps:

<span>1.</span> Install Gradio from pip.
<span>1.</span> Install Gradio from pip. Note, the minimal supported Python version is 3.7.

```bash
pip install gradio
Expand Down Expand Up @@ -88,7 +108,7 @@ The core `Interface` class is initialized with three required parameters:
- `inputs`: which component(s) to use for the input, e.g. `"text"` or `"image"` or `"audio"`
- `outputs`: which component(s) to use for the output, e.g. `"text"` or `"image"` `"label"`

Gradio includes more than 20 different components, most of which can be used as inputs or outputs. ([See docs for complete list](/docs))
Gradio includes more than 20 different components, most of which can be used as inputs or outputs. ([See docs for complete list](https://gradio.app/docs))

### Components Attributes 💻

Expand Down Expand Up @@ -241,7 +261,6 @@ demo = gr.Interface(
],
title="test calculator",
description="heres a sample toy calculator. enjoy!",
flagging_options=["this", "or", "that"],
)

demo.launch()
Expand Down Expand Up @@ -398,25 +417,33 @@ Now that you're familiar with the basics of Gradio, here are some good next step
* Gradio offers two APIs to users: **Interface**, a high level abstraction for quickly creating demos, and **Blocks**, a more flexible API for designing web apps with more controlled layouts and data flows. [Read more about Blocks here](https://gradio.app/introduction_to_blocks/) 🧱
* If you'd like to stick with **Interface**, but want to add more advanced features to your demo (like authentication, interpretation, or state), check out our guide on [advanced features with the Interface class](https://gradio.app/advanced_interface_features/) 💪
* If you just want to explore what demos other people have built with Gradio and see the underlying Python code, [browse public Hugging Face Spaces](https://hf.space/), and be inspired 🤗


## System Requirements:

- Python 3.7+

## System Requirements:
## Open Source Stack

Gradio requires Python `3.7+` and has been tested on the latest versions of Windows, MacOS, and various common Linux distributions (e.g. Ubuntu). For Python package requirements, please see the `setup.py` file.
Gradio is built with many wonderful open-source libraries, please support them as well!
<p>
<a href="https://hf.co/"><img src="website/homepage/src/assets/img/logos/huggingface_mini.svg" alt="huggingface" height=40></a>
<a href="https://www.python.org/"><img src="website/homepage/src/assets/img/logos/python.svg" alt="python" height=40></a>
<a href="https://fastapi.tiangolo.com/"><img src="website/homepage/src/assets/img/logos/fastapi.svg" alt="fastapi" height=40></a>
<a href="https://github.com/encode/"><img src="website/homepage/src/assets/img/logos/encode.svg" alt="encode" height=40></a>
<a href="https://svelte.dev"><img src="website/homepage/src/assets/img/logos/svelte.svg" alt="svelte" height=40></a>
<a href="https://vitejs.dev/"><img src="website/homepage/src/assets/img/logos/vite.svg" alt="vite" height=40></a>
<a href="https://pnpm.io/"><img src="website/homepage/src/assets/img/logos/pnpm.svg" alt="pnpm" height=40></a>
<a href="https://tailwindcss.com/"><img src="website/homepage/src/assets/img/logos/tailwind.svg" alt="tailwind" height=40></a>
</p>

## Contributing:

If you would like to contribute and your contribution is small, you can directly open a pull request (PR). If you would like to contribute a larger feature, we recommend first creating an issue with a proposed design for discussion. Please see our [contributing guidelines](https://github.com/gradio-app/gradio/blob/master/CONTRIBUTING.md) for more info.

## License:
## License:

Gradio is licensed under the Apache License 2.0

## See more:

## See more:

You can find many more examples as well as more info on usage on our website: www.gradio.app
You can find many more examples as well as more info on usage on our website: https://gradio.app

See, also, the accompanying paper: ["Gradio: Hassle-Free Sharing and Testing of ML Models in the Wild"](https://arxiv.org/pdf/1906.02569.pdf), *ICML HILL 2019*, and please use the citation below.

Expand All @@ -428,4 +455,3 @@ journal={arXiv preprint arXiv:1906.02569},
year={2019}
}
```

1 change: 1 addition & 0 deletions gradio.egg-info/requires.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ pydub
requests
uvicorn
Jinja2
fsspec
7 changes: 3 additions & 4 deletions gradio/blocks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import asyncio
import copy
import getpass
import inspect
Expand Down Expand Up @@ -375,9 +376,7 @@ def __call__(self, *params, fn_index=0):
processed_input = self.preprocess_data(fn_index, serialized_params, None)

if inspect.iscoroutinefunction(block_fn.fn):
raise ValueError(
"Cannot call Blocks object as a function if the function is a coroutine"
)
predictions = utils.synchronize_async(block_fn.fn, *processed_input)
else:
predictions = block_fn.fn(*processed_input)

Expand Down Expand Up @@ -745,7 +744,7 @@ def launch(
self.enable_queue = True
else:
self.enable_queue = enable_queue or False
utils.synchronize_async(self.create_limiter, max_threads)
utils.run_coro_in_background(self.create_limiter, max_threads)
self.config = self.get_config_file()
self.share = share
self.encrypt = encrypt
Expand Down
36 changes: 33 additions & 3 deletions gradio/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import aiohttp
import analytics
import fsspec.asyn
import pkg_resources
import requests

Expand Down Expand Up @@ -312,7 +313,36 @@ def component_or_layout_class(cls_name: str) -> Component | BlockContext:
raise ValueError(f"No such component or layout: {cls_name}")


def synchronize_async(func: Callable, *args: object, callback_func: Callable = None):
def synchronize_async(func: Callable, *args, **kwargs):
"""
Runs async functions in sync scopes.

Example:
if inspect.iscoroutinefunction(block_fn.fn):
predictions = utils.synchronize_async(block_fn.fn, *processed_input)

Args:
func:
*args:
**kwargs:
"""
return fsspec.asyn.sync(fsspec.asyn.get_loop(), func, *args, **kwargs)


def run_coro_in_background(func: Callable, *args, **kwargs):
"""
Runs coroutines in background.

Example:
utils.run_coro_in_background(fn, *args, **kwargs)

Args:
func:
*args:
**kwargs:

Returns:

"""
event_loop = asyncio.get_event_loop()
task = event_loop.create_task(func(*args))
task.add_done_callback(callback_func)
_ = event_loop.create_task(func(*args, **kwargs))
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"pydub",
"requests",
"uvicorn",
"Jinja2"
"Jinja2",
"fsspec",
],
entry_points={
'console_scripts': ['gradio=gradio.reload:run_in_reload_mode']
Expand Down