fix default variable value (#1381)

* fix default variable value

* format
This commit is contained in:
aliabid94 2022-05-26 14:22:05 -07:00 committed by GitHub
parent cb2713e705
commit 0a8222719e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 10 deletions

View File

@ -3,15 +3,23 @@ import gradio as gr
demo = gr.Blocks(css="#btn {color: red}")
with demo:
default_json = {"a": "a"}
num = gr.Variable(value=0)
squared = gr.Number(value=0).style(text_color="blue", container_bg_color="yellow")
btn = gr.Button("Next Square", elem_id="btn").style(rounded=False, bg_color="purple")
stats = gr.Variable(value=default_json)
table = gr.JSON()
def increase(var):
def increase(var, stats_history):
var += 1
return var, var**2
stats_history[str(var)] = var**2
return var, var**2, stats_history, stats_history
btn.click(increase, [num], [num, squared])
btn.click(increase, [num, stats], [num, squared, stats, table])
if __name__ == "__main__":
demo.launch()

View File

@ -2,7 +2,7 @@ Metadata-Version: 2.1
Name: gradio
Version: 3.0.4
Summary: Python library for easily interacting with trained machine learning models
Home-page: https://github.com/gradio-app/gradio-UI
Home-page: https://github.com/gradio-app/gradio
Author: Abubakar Abid, Ali Abid, Ali Abdalla, Dawood Khan, Ahsen Khaliq, Pete Allen, Ömer Faruk Özdemir
Author-email: team@gradio.app
License: Apache License 2.0

View File

@ -350,7 +350,6 @@ class Blocks(BlockContext):
else:
processed_input = raw_input
start = time.time()
if inspect.iscoroutinefunction(block_fn.fn):
predictions = await block_fn.fn(*processed_input)
else:

View File

@ -1309,7 +1309,7 @@ class Image(Editable, Clearable, Changeable, Streamable, IOComponent):
def __init__(
self,
value: Optional[str] = None,
value: Optional[str | PIL.Image | np.narray] = None,
*,
shape: Tuple[int, int] = None,
image_mode: str = "RGB",
@ -1327,7 +1327,7 @@ class Image(Editable, Clearable, Changeable, Streamable, IOComponent):
):
"""
Parameters:
value (str): A path or URL for the default value that Image component is going to take.
value (Optional[str | PIL.Image | np.narray]): A PIL Image, numpy array, path or URL for the default value that Image component is going to take.
shape (Tuple[int, int]): (width, height) shape to crop and resize image to; if None, matches input image size. Pass None for either width or height to only crop and resize the other.
image_mode (str): "RGB" if color, or "L" if black and white.
invert_colors (bool): whether to invert the image as a preprocessing step.
@ -1789,7 +1789,7 @@ class Audio(Changeable, Clearable, Playable, Streamable, IOComponent):
def __init__(
self,
value: Optional[str] = None,
value: Optional[str | Tuple[int, np.array]] = None,
*,
source: str = "upload",
type: str = "numpy",
@ -1803,7 +1803,7 @@ class Audio(Changeable, Clearable, Playable, Streamable, IOComponent):
):
"""
Parameters:
value (str): A path or URL for the default value that Audio component is going to take.
value (str | Tuple[int, numpy.array]): A path, URL, or [sample_rate, numpy array] tuple for the default value that Audio component is going to take.
source (str): Source of audio. "upload" creates a box where user can drop an audio file, "microphone" creates a microphone input.
type (str): 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 (Optional[str]): component name in interface.

View File

@ -9,6 +9,7 @@ import posixpath
import secrets
import traceback
import urllib
from copy import deepcopy
from pathlib import Path
from typing import Any, List, Optional, Type
@ -269,7 +270,7 @@ class App(FastAPI):
if hasattr(body, "session_hash"):
if body.session_hash not in app.state_holder:
app.state_holder[body.session_hash] = {
_id: getattr(block, "value", None)
_id: deepcopy(getattr(block, "value", None))
for _id, block in app.blocks.blocks.items()
if getattr(block, "stateful", False)
}