Skip to content

Commit

Permalink
A few small fixes to docs / demos (#3611)
Browse files Browse the repository at this point in the history
* fixes

* remove binaries

* doc

* changelog

* typing

* run on windows

* cancels

* added clarifications
  • Loading branch information
abidlabs authored Mar 24, 2023
1 parent 4c00103 commit c9b8a0c
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
## Documentation Changes:

- Makes some fixes to the Theme Guide related to naming of variables, by [@abidlabs](https://github.com/abidlabs) in [PR 3561](https://github.com/gradio-app/gradio/pull/3561)
- Makes some additions to documentation of `Audio` and `State` components, and fixes the `pictionary` demo by [@abidlabs](https://github.com/abidlabs) in [PR 3611](https://github.com/gradio-app/gradio/pull/3611)


## Testing and Infrastructure Changes:

Expand Down
12 changes: 6 additions & 6 deletions demo/generate_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def git_tracked(demo, file):
text = f"# Gradio Demo: {demo}"

if os.path.exists(os.path.join(GRADIO_DEMO_DIR, demo, "DESCRIPTION.md")):
with open(os.path.join(GRADIO_DEMO_DIR, demo, "DESCRIPTION.md"), "r") as f:
with open(os.path.join(GRADIO_DEMO_DIR, demo, "DESCRIPTION.md"), "r", encoding="utf8") as f:
description = f.read()
text += f"""\n### {description}
"""
Expand All @@ -45,13 +45,13 @@ def git_tracked(demo, file):

requirements = ""
if os.path.exists(os.path.join(GRADIO_DEMO_DIR, demo, "requirements.txt")):
with open(os.path.join(GRADIO_DEMO_DIR, demo, "requirements.txt"), "r") as f:
with open(os.path.join(GRADIO_DEMO_DIR, demo, "requirements.txt"), "r", encoding="utf8") as f:
requirements = f.read().split("\n")
requirements = " ".join(requirements)

installs = f"!pip install -q gradio {requirements}"

with open(os.path.join(GRADIO_DEMO_DIR, demo, "run.py"), "r") as f:
with open(os.path.join(GRADIO_DEMO_DIR, demo, "run.py"), "r", encoding="utf8") as f:
code = f.read()
code = code.replace("os.path.dirname(__file__)", "os.path.abspath('')")

Expand All @@ -67,16 +67,16 @@ def git_tracked(demo, file):

output_notebook = os.path.join(GRADIO_DEMO_DIR, demo, "run.ipynb")

with open(output_notebook, 'w') as f:
with open(output_notebook, 'w', encoding="utf8") as f:
nbf.write(nb, f)

with open(output_notebook, "r") as f:
with open(output_notebook, "r", encoding="utf8") as f:
content = f.read()

content = json.loads(content)
for i, cell in enumerate(content["cells"]):
random.seed(i)
cell["id"] = random.getrandbits(128)

with open(output_notebook, "w") as f:
with open(output_notebook, "w", encoding="utf8") as f:
f.write(json.dumps(content))
3 changes: 2 additions & 1 deletion demo/pictionary/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
torch
gdown
gdown
numpy
2 changes: 1 addition & 1 deletion demo/pictionary/run.ipynb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"cells": [{"cell_type": "markdown", "id": 302934307671667531413257853548643485645, "metadata": {}, "source": ["# Gradio Demo: pictionary"]}, {"cell_type": "code", "execution_count": null, "id": 272996653310673477252411125948039410165, "metadata": {}, "outputs": [], "source": ["!pip install -q gradio torch gdown"]}, {"cell_type": "code", "execution_count": null, "id": 288918539441861185822528903084949547379, "metadata": {}, "outputs": [], "source": ["# Downloading files from the demo repo\n", "import os\n", "!wget -q https://github.com/gradio-app/gradio/raw/main/demo/pictionary/class_names.txt"]}, {"cell_type": "code", "execution_count": null, "id": 44380577570523278879349135829904343037, "metadata": {}, "outputs": [], "source": ["from pathlib import Path\n", "\n", "import torch\n", "import gradio as gr\n", "from torch import nn\n", "import gdown \n", "\n", "url = 'https://drive.google.com/uc?id=1dsk2JNZLRDjC-0J4wIQX_FcVurPaXaAZ'\n", "output = 'pytorch_model.bin'\n", "gdown.download(url, output, quiet=False)\n", "\n", "LABELS = Path('class_names.txt').read_text().splitlines()\n", "\n", "model = nn.Sequential(\n", " nn.Conv2d(1, 32, 3, padding='same'),\n", " nn.ReLU(),\n", " nn.MaxPool2d(2),\n", " nn.Conv2d(32, 64, 3, padding='same'),\n", " nn.ReLU(),\n", " nn.MaxPool2d(2),\n", " nn.Conv2d(64, 128, 3, padding='same'),\n", " nn.ReLU(),\n", " nn.MaxPool2d(2),\n", " nn.Flatten(),\n", " nn.Linear(1152, 256),\n", " nn.ReLU(),\n", " nn.Linear(256, len(LABELS)),\n", ")\n", "state_dict = torch.load('pytorch_model.bin', map_location='cpu')\n", "model.load_state_dict(state_dict, strict=False)\n", "model.eval()\n", "\n", "def predict(input):\n", " im = input\n", " if im is None:\n", " return None\n", " \n", " x = torch.tensor(im, dtype=torch.float32).unsqueeze(0).unsqueeze(0) / 255.\n", "\n", " with torch.no_grad():\n", " out = model(x)\n", "\n", " probabilities = torch.nn.functional.softmax(out[0], dim=0)\n", "\n", " values, indices = torch.topk(probabilities, 5)\n", "\n", " return {LABELS[i]: v.item() for i, v in zip(indices, values)}\n", "\n", "\n", "interface = gr.Interface(predict, inputs=gr.templates.Sketchpad(label=\"Draw Here\"), outputs=gr.Label(label=\"Guess\"), theme=\"default\", css=\".footer{display:none !important}\", live=True)\n", "interface.launch(enable_queue=False)\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
{"cells": [{"cell_type": "markdown", "id": 302934307671667531413257853548643485645, "metadata": {}, "source": ["# Gradio Demo: pictionary"]}, {"cell_type": "code", "execution_count": null, "id": 272996653310673477252411125948039410165, "metadata": {}, "outputs": [], "source": ["!pip install -q gradio torch gdown numpy"]}, {"cell_type": "code", "execution_count": null, "id": 288918539441861185822528903084949547379, "metadata": {}, "outputs": [], "source": ["# Downloading files from the demo repo\n", "import os\n", "!wget -q https://github.com/gradio-app/gradio/raw/main/demo/pictionary/class_names.txt"]}, {"cell_type": "code", "execution_count": null, "id": 44380577570523278879349135829904343037, "metadata": {}, "outputs": [], "source": ["from pathlib import Path\n", "\n", "import numpy as np\n", "import torch\n", "import gradio as gr\n", "from torch import nn\n", "import gdown \n", "\n", "url = 'https://drive.google.com/uc?id=1dsk2JNZLRDjC-0J4wIQX_FcVurPaXaAZ'\n", "output = 'pytorch_model.bin'\n", "gdown.download(url, output, quiet=False)\n", "\n", "LABELS = Path('class_names.txt').read_text().splitlines()\n", "\n", "model = nn.Sequential(\n", " nn.Conv2d(1, 32, 3, padding='same'),\n", " nn.ReLU(),\n", " nn.MaxPool2d(2),\n", " nn.Conv2d(32, 64, 3, padding='same'),\n", " nn.ReLU(),\n", " nn.MaxPool2d(2),\n", " nn.Conv2d(64, 128, 3, padding='same'),\n", " nn.ReLU(),\n", " nn.MaxPool2d(2),\n", " nn.Flatten(),\n", " nn.Linear(1152, 256),\n", " nn.ReLU(),\n", " nn.Linear(256, len(LABELS)),\n", ")\n", "state_dict = torch.load('pytorch_model.bin', map_location='cpu')\n", "model.load_state_dict(state_dict, strict=False)\n", "model.eval()\n", "\n", "def predict(im):\n", " if im is None:\n", " return None\n", " im = np.asarray(im.resize((28, 28)))\n", " \n", " x = torch.tensor(im, dtype=torch.float32).unsqueeze(0).unsqueeze(0) / 255.\n", "\n", " with torch.no_grad():\n", " out = model(x)\n", "\n", " probabilities = torch.nn.functional.softmax(out[0], dim=0)\n", "\n", " values, indices = torch.topk(probabilities, 5)\n", "\n", " return {LABELS[i]: v.item() for i, v in zip(indices, values)}\n", "\n", "\n", "interface = gr.Interface(predict, \n", " inputs=gr.Sketchpad(label=\"Draw Here\", brush_radius=5, type=\"pil\", shape=(120, 120)), \n", " outputs=gr.Label(label=\"Guess\"), \n", " live=True)\n", "\n", "interface.queue().launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
13 changes: 9 additions & 4 deletions demo/pictionary/run.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pathlib import Path

import numpy as np
import torch
import gradio as gr
from torch import nn
Expand Down Expand Up @@ -30,10 +31,10 @@
model.load_state_dict(state_dict, strict=False)
model.eval()

def predict(input):
im = input
def predict(im):
if im is None:
return None
im = np.asarray(im.resize((28, 28)))

x = torch.tensor(im, dtype=torch.float32).unsqueeze(0).unsqueeze(0) / 255.

Expand All @@ -47,5 +48,9 @@ def predict(input):
return {LABELS[i]: v.item() for i, v in zip(indices, values)}


interface = gr.Interface(predict, inputs=gr.templates.Sketchpad(label="Draw Here"), outputs=gr.Label(label="Guess"), theme="default", css=".footer{display:none !important}", live=True)
interface.launch(enable_queue=False)
interface = gr.Interface(predict,
inputs=gr.Sketchpad(label="Draw Here", brush_radius=5, type="pil", shape=(120, 120)),
outputs=gr.Label(label="Guess"),
live=True)

interface.queue().launch()
12 changes: 6 additions & 6 deletions gradio/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -1578,7 +1578,7 @@ def __init__(
elem_id: str | None = None,
elem_classes: List[str] | str | None = None,
mirror_webcam: bool = True,
brush_radius: int | None = None,
brush_radius: float | None = None,
**kwargs,
):
"""
Expand Down Expand Up @@ -1661,7 +1661,7 @@ def update(
show_label: bool | None = None,
interactive: bool | None = None,
visible: bool | None = None,
brush_radius: int | None = None,
brush_radius: float | None = None,
):
return {
"label": label,
Expand Down Expand Up @@ -2144,8 +2144,8 @@ class Audio(
):
"""
Creates an audio component that can be used to upload/record audio (as an input) or display audio (as an output).
Preprocessing: passes the uploaded audio as a {Tuple(int, numpy.array)} corresponding to (sample rate, data) or as a {str} filepath, depending on `type`
Postprocessing: expects a {Tuple(int, numpy.array)} corresponding to (sample rate, data) or as a {str} filepath or URL to an audio file, which gets displayed
Preprocessing: passes the uploaded audio as a {Tuple(int, numpy.array)} corresponding to (sample rate in Hz, audio data as a 16-bit int array whose values range from -32768 to 32767), or as a {str} filepath, depending on `type`.
Postprocessing: expects a {Tuple(int, numpy.array)} corresponding to (sample rate in Hz, audio data as a float or int numpy array) or as a {str} filepath or URL to an audio file, which gets displayed
Examples-format: a {str} filepath to a local file that contains audio.
Demos: main_note, generate_tone, reverse_audio
Guides: real_time_speech_recognition
Expand All @@ -2169,7 +2169,7 @@ def __init__(
):
"""
Parameters:
value: A path, URL, or [sample_rate, numpy array] tuple for the default value that Audio component is going to take. If callable, the function will be called whenever the app loads to set the initial value of the component.
value: A path, URL, or [sample_rate, numpy array] tuple (sample rate in Hz, audio data as a float or int numpy array) for the default value that Audio component is going to take. If callable, the function will be called whenever the app loads to set the initial value of the component.
source: Source of audio. "upload" creates a box where user can drop an audio file, "microphone" creates a microphone input.
type: The format the audio file is converted to before being passed into the prediction function. "numpy" converts the audio to a tuple consisting of: (int sample rate, numpy.array for the data), "filepath" passes a str path to a temporary file containing the audio.
label: component name in interface.
Expand Down Expand Up @@ -3113,7 +3113,7 @@ def __init__(
):
"""
Parameters:
value: the initial value of the state. If callable, the function will be called whenever the app loads to set the initial value of the component.
value: the initial value (of abitrary type) of the state. The provided argument is deepcopied. If a callable is provided, the function will be called whenever the app loads to set the initial value of the state.
"""
self.stateful = True
IOComponent.__init__(self, value=deepcopy(value), **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion gradio/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def __call__(
max_batch_size: Maximum number of inputs to batch together if this is called from the queue (only relevant if batch=True)
preprocess: If False, will not run preprocessing of component data before running 'fn' (e.g. leaving it as a base64 string if this method is called with the `Image` component).
postprocess: If False, will not run postprocessing of component data before returning 'fn' output to the browser.
cancels: A list of other events to cancel when this event is triggered. For example, setting cancels=[click_event] will cancel the click_event, where click_event is the return value of another components .click method.
cancels: A list of other events to cancel when this event is triggered. For example, setting cancels=[click_event] will cancel the click_event, where click_event is the return value of another components .click method. Functions that have not yet run (or generators that are iterating) will be cancelled, but functions that are currently running will be allowed to finish.
every: Run this event 'every' number of seconds while the client connection is open. Interpreted in seconds. Queue must be enabled.
"""
# _js: Optional frontend js method to run before running 'fn'. Input arguments for js method are values of 'inputs' and 'outputs', return should be a list of values for output components.
Expand Down
2 changes: 1 addition & 1 deletion gradio/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def __init__(
description: str | None = None,
article: str | None = None,
thumbnail: str | None = None,
theme: Theme | None = None,
theme: Theme | str | None = None,
css: str | None = None,
allow_flagging: str | None = None,
flagging_options: List[str] | List[Tuple[str, str]] | None = None,
Expand Down
12 changes: 12 additions & 0 deletions gradio/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def __init__(
streaming: bool = False,
elem_id: str | None = None,
mirror_webcam: bool = True,
brush_radius: float | None = None,
**kwargs,
):
super().__init__(
Expand All @@ -85,6 +86,7 @@ def __init__(
streaming=streaming,
elem_id=elem_id,
mirror_webcam=mirror_webcam,
brush_radius=brush_radius,
**kwargs,
)

Expand Down Expand Up @@ -113,6 +115,7 @@ def __init__(
streaming: bool = False,
elem_id: str | None = None,
mirror_webcam: bool = True,
brush_radius: float | None = None,
**kwargs,
):
super().__init__(
Expand All @@ -130,6 +133,7 @@ def __init__(
streaming=streaming,
elem_id=elem_id,
mirror_webcam=mirror_webcam,
brush_radius=brush_radius,
**kwargs,
)

Expand Down Expand Up @@ -158,6 +162,7 @@ def __init__(
streaming: bool = False,
elem_id: str | None = None,
mirror_webcam: bool = True,
brush_radius: float | None = None,
**kwargs,
):
super().__init__(
Expand All @@ -175,6 +180,7 @@ def __init__(
streaming=streaming,
elem_id=elem_id,
mirror_webcam=mirror_webcam,
brush_radius=brush_radius,
**kwargs,
)

Expand Down Expand Up @@ -203,6 +209,7 @@ def __init__(
streaming: bool = False,
elem_id: str | None = None,
mirror_webcam: bool = True,
brush_radius: float | None = None,
**kwargs,
):
super().__init__(
Expand All @@ -220,6 +227,7 @@ def __init__(
streaming=streaming,
elem_id=elem_id,
mirror_webcam=mirror_webcam,
brush_radius=brush_radius,
**kwargs,
)

Expand Down Expand Up @@ -248,6 +256,7 @@ def __init__(
streaming: bool = False,
elem_id: str | None = None,
mirror_webcam: bool = True,
brush_radius: float | None = None,
**kwargs,
):
super().__init__(
Expand All @@ -265,6 +274,7 @@ def __init__(
streaming=streaming,
elem_id=elem_id,
mirror_webcam=mirror_webcam,
brush_radius=brush_radius,
**kwargs,
)

Expand Down Expand Up @@ -293,6 +303,7 @@ def __init__(
streaming: bool = False,
elem_id: str | None = None,
mirror_webcam: bool = True,
brush_radius: float | None = None,
**kwargs,
):
super().__init__(
Expand All @@ -310,6 +321,7 @@ def __init__(
streaming=streaming,
elem_id=elem_id,
mirror_webcam=mirror_webcam,
brush_radius=brush_radius,
**kwargs,
)

Expand Down

0 comments on commit c9b8a0c

Please sign in to comment.