2023-09-22 04:20:47 +08:00
|
|
|
import asyncio
|
2023-06-26 22:26:28 +08:00
|
|
|
import json
|
2022-01-26 14:05:50 +08:00
|
|
|
import os
|
2023-06-21 01:19:15 +08:00
|
|
|
import shutil
|
2023-06-22 02:34:33 +08:00
|
|
|
import subprocess
|
2022-09-22 00:53:06 +08:00
|
|
|
import tempfile
|
2023-06-26 22:26:28 +08:00
|
|
|
import time
|
2023-06-08 04:05:01 +08:00
|
|
|
from pathlib import Path
|
2022-09-20 22:48:52 +08:00
|
|
|
from unittest.mock import patch
|
2022-01-26 14:05:50 +08:00
|
|
|
|
2023-12-13 07:24:46 +08:00
|
|
|
import gradio_client as grc
|
2022-08-12 03:08:06 +08:00
|
|
|
import pytest
|
2023-12-19 09:07:38 +08:00
|
|
|
from gradio_client import media_data
|
|
|
|
from gradio_client import utils as client_utils
|
2023-08-24 02:47:07 +08:00
|
|
|
from pydub import AudioSegment
|
2023-01-16 10:51:05 +08:00
|
|
|
from starlette.testclient import TestClient
|
2023-06-26 22:26:28 +08:00
|
|
|
from tqdm import tqdm
|
2022-08-12 03:08:06 +08:00
|
|
|
|
2022-08-09 01:35:26 +08:00
|
|
|
import gradio as gr
|
2023-12-19 09:07:38 +08:00
|
|
|
from gradio import utils
|
2022-01-26 14:05:50 +08:00
|
|
|
|
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
@patch("gradio.utils.get_cache_folder", return_value=Path(tempfile.mkdtemp()))
|
2022-08-09 01:35:26 +08:00
|
|
|
class TestExamples:
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_handle_single_input(self, patched_cache_folder):
|
2022-08-09 01:35:26 +08:00
|
|
|
examples = gr.Examples(["hello", "hi"], gr.Textbox())
|
|
|
|
assert examples.processed_examples == [["hello"], ["hi"]]
|
|
|
|
|
|
|
|
examples = gr.Examples([["hello"]], gr.Textbox())
|
|
|
|
assert examples.processed_examples == [["hello"]]
|
|
|
|
|
|
|
|
examples = gr.Examples(["test/test_files/bus.png"], gr.Image())
|
2023-10-31 12:46:02 +08:00
|
|
|
assert (
|
2023-12-19 09:07:38 +08:00
|
|
|
client_utils.encode_file_to_base64(
|
|
|
|
examples.processed_examples[0][0]["path"]
|
|
|
|
)
|
2023-10-31 12:46:02 +08:00
|
|
|
== media_data.BASE64_IMAGE
|
|
|
|
)
|
2022-08-09 01:35:26 +08:00
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_handle_multiple_inputs(self, patched_cache_folder):
|
2022-08-09 01:35:26 +08:00
|
|
|
examples = gr.Examples(
|
|
|
|
[["hello", "test/test_files/bus.png"]], [gr.Textbox(), gr.Image()]
|
|
|
|
)
|
2023-10-31 12:46:02 +08:00
|
|
|
assert examples.processed_examples[0][0] == "hello"
|
|
|
|
assert (
|
2023-12-19 09:07:38 +08:00
|
|
|
client_utils.encode_file_to_base64(
|
|
|
|
examples.processed_examples[0][1]["path"]
|
|
|
|
)
|
2023-10-31 12:46:02 +08:00
|
|
|
== media_data.BASE64_IMAGE
|
|
|
|
)
|
2022-08-09 01:35:26 +08:00
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_handle_directory(self, patched_cache_folder):
|
2022-08-09 01:35:26 +08:00
|
|
|
examples = gr.Examples("test/test_files/images", gr.Image())
|
2023-10-31 12:46:02 +08:00
|
|
|
assert len(examples.processed_examples) == 2
|
|
|
|
for row in examples.processed_examples:
|
|
|
|
for output in row:
|
|
|
|
assert (
|
2023-12-19 09:07:38 +08:00
|
|
|
client_utils.encode_file_to_base64(output["path"])
|
2023-10-31 12:46:02 +08:00
|
|
|
== media_data.BASE64_IMAGE
|
|
|
|
)
|
2022-08-09 01:35:26 +08:00
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_handle_directory_with_log_file(self, patched_cache_folder):
|
2022-08-09 01:35:26 +08:00
|
|
|
examples = gr.Examples(
|
|
|
|
"test/test_files/images_log", [gr.Image(label="im"), gr.Text()]
|
|
|
|
)
|
2023-12-19 09:07:38 +08:00
|
|
|
ex = client_utils.traverse(
|
2023-10-31 12:46:02 +08:00
|
|
|
examples.processed_examples,
|
2023-12-19 09:07:38 +08:00
|
|
|
lambda s: client_utils.encode_file_to_base64(s["path"]),
|
2023-10-31 12:46:02 +08:00
|
|
|
lambda x: isinstance(x, dict) and Path(x["path"]).exists(),
|
|
|
|
)
|
|
|
|
assert ex == [
|
2023-04-14 07:20:33 +08:00
|
|
|
[media_data.BASE64_IMAGE, "hello"],
|
|
|
|
[media_data.BASE64_IMAGE, "hi"],
|
2022-08-09 01:35:26 +08:00
|
|
|
]
|
2022-12-02 03:41:54 +08:00
|
|
|
for sample in examples.dataset.samples:
|
2024-01-11 08:35:25 +08:00
|
|
|
assert os.path.isabs(sample[0]["path"])
|
2022-08-09 01:35:26 +08:00
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_examples_per_page(self, patched_cache_folder):
|
2022-12-21 06:10:23 +08:00
|
|
|
examples = gr.Examples(["hello", "hi"], gr.Textbox(), examples_per_page=2)
|
|
|
|
assert examples.dataset.get_config()["samples_per_page"] == 2
|
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_no_preprocessing(self, patched_cache_folder):
|
2022-10-05 03:51:20 +08:00
|
|
|
with gr.Blocks():
|
|
|
|
image = gr.Image()
|
|
|
|
textbox = gr.Textbox()
|
|
|
|
|
|
|
|
examples = gr.Examples(
|
|
|
|
examples=["test/test_files/bus.png"],
|
|
|
|
inputs=image,
|
|
|
|
outputs=textbox,
|
2023-10-31 12:46:02 +08:00
|
|
|
fn=lambda x: x["path"],
|
2022-10-05 03:51:20 +08:00
|
|
|
cache_examples=True,
|
|
|
|
preprocess=False,
|
|
|
|
)
|
|
|
|
|
2023-10-07 02:05:11 +08:00
|
|
|
prediction = examples.load_from_cache(0)
|
2023-12-19 09:07:38 +08:00
|
|
|
assert (
|
|
|
|
client_utils.encode_file_to_base64(prediction[0]) == media_data.BASE64_IMAGE
|
|
|
|
)
|
2022-10-05 03:51:20 +08:00
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_no_postprocessing(self, patched_cache_folder):
|
2022-10-05 03:51:20 +08:00
|
|
|
def im(x):
|
2023-10-31 12:46:02 +08:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
"image": {
|
|
|
|
"path": "test/test_files/bus.png",
|
|
|
|
},
|
|
|
|
"caption": "hi",
|
|
|
|
}
|
|
|
|
]
|
2022-10-05 03:51:20 +08:00
|
|
|
|
|
|
|
with gr.Blocks():
|
|
|
|
text = gr.Textbox()
|
|
|
|
gall = gr.Gallery()
|
2022-08-09 01:35:26 +08:00
|
|
|
|
2022-10-05 03:51:20 +08:00
|
|
|
examples = gr.Examples(
|
|
|
|
examples=["hi"],
|
|
|
|
inputs=text,
|
|
|
|
outputs=gall,
|
|
|
|
fn=im,
|
|
|
|
cache_examples=True,
|
|
|
|
postprocess=False,
|
|
|
|
)
|
|
|
|
|
2023-10-07 02:05:11 +08:00
|
|
|
prediction = examples.load_from_cache(0)
|
2023-10-31 12:46:02 +08:00
|
|
|
file = prediction[0].root[0].image.path
|
2023-12-19 09:07:38 +08:00
|
|
|
assert client_utils.encode_url_or_file_to_base64(
|
2023-10-31 12:46:02 +08:00
|
|
|
file
|
2023-12-19 09:07:38 +08:00
|
|
|
) == client_utils.encode_url_or_file_to_base64("test/test_files/bus.png")
|
|
|
|
|
|
|
|
|
|
|
|
def test_setting_cache_dir_env_variable(monkeypatch):
|
|
|
|
temp_dir = tempfile.mkdtemp()
|
|
|
|
monkeypatch.setenv("GRADIO_EXAMPLES_CACHE", temp_dir)
|
|
|
|
with gr.Blocks():
|
|
|
|
image = gr.Image()
|
|
|
|
image2 = gr.Image()
|
|
|
|
|
|
|
|
examples = gr.Examples(
|
|
|
|
examples=["test/test_files/bus.png"],
|
|
|
|
inputs=image,
|
|
|
|
outputs=image2,
|
|
|
|
fn=lambda x: x,
|
|
|
|
cache_examples=True,
|
|
|
|
)
|
|
|
|
prediction = examples.load_from_cache(0)
|
|
|
|
path_to_cached_file = Path(prediction[0].path)
|
|
|
|
assert utils.is_in_or_equal(path_to_cached_file, temp_dir)
|
|
|
|
monkeypatch.delenv("GRADIO_EXAMPLES_CACHE", raising=False)
|
2022-10-05 03:51:20 +08:00
|
|
|
|
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
@patch("gradio.utils.get_cache_folder", return_value=Path(tempfile.mkdtemp()))
|
2022-08-09 01:35:26 +08:00
|
|
|
class TestExamplesDataset:
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_no_headers(self, patched_cache_folder):
|
2022-08-09 01:35:26 +08:00
|
|
|
examples = gr.Examples("test/test_files/images_log", [gr.Image(), gr.Text()])
|
|
|
|
assert examples.dataset.headers == []
|
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_all_headers(self, patched_cache_folder):
|
2022-08-09 01:35:26 +08:00
|
|
|
examples = gr.Examples(
|
|
|
|
"test/test_files/images_log",
|
|
|
|
[gr.Image(label="im"), gr.Text(label="your text")],
|
|
|
|
)
|
|
|
|
assert examples.dataset.headers == ["im", "your text"]
|
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_some_headers(self, patched_cache_folder):
|
2022-08-09 01:35:26 +08:00
|
|
|
examples = gr.Examples(
|
|
|
|
"test/test_files/images_log", [gr.Image(label="im"), gr.Text()]
|
|
|
|
)
|
|
|
|
assert examples.dataset.headers == ["im", ""]
|
|
|
|
|
|
|
|
|
2023-09-21 03:03:55 +08:00
|
|
|
def test_example_caching_relaunch(connect):
|
|
|
|
def combine(a, b):
|
|
|
|
return a + " " + b
|
|
|
|
|
|
|
|
with gr.Blocks() as demo:
|
|
|
|
txt = gr.Textbox(label="Input")
|
|
|
|
txt_2 = gr.Textbox(label="Input 2")
|
|
|
|
txt_3 = gr.Textbox(value="", label="Output")
|
|
|
|
btn = gr.Button(value="Submit")
|
|
|
|
btn.click(combine, inputs=[txt, txt_2], outputs=[txt_3])
|
|
|
|
gr.Examples(
|
|
|
|
[["hi", "Adam"], ["hello", "Eve"]],
|
|
|
|
[txt, txt_2],
|
|
|
|
txt_3,
|
|
|
|
combine,
|
|
|
|
cache_examples=True,
|
|
|
|
api_name="examples",
|
|
|
|
)
|
|
|
|
|
|
|
|
with connect(demo) as client:
|
|
|
|
assert client.predict(1, api_name="/examples") == (
|
|
|
|
"hello",
|
|
|
|
"Eve",
|
|
|
|
"hello Eve",
|
|
|
|
)
|
|
|
|
|
|
|
|
# Let the server shut down
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
with connect(demo) as client:
|
|
|
|
assert client.predict(1, api_name="/examples") == (
|
|
|
|
"hello",
|
|
|
|
"Eve",
|
|
|
|
"hello Eve",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
@patch("gradio.utils.get_cache_folder", return_value=Path(tempfile.mkdtemp()))
|
2022-08-09 01:35:26 +08:00
|
|
|
class TestProcessExamples:
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_caching(self, patched_cache_folder):
|
2022-08-09 01:35:26 +08:00
|
|
|
io = gr.Interface(
|
2023-04-28 04:09:50 +08:00
|
|
|
lambda x: f"Hello {x}",
|
2022-03-26 02:14:28 +08:00
|
|
|
"text",
|
|
|
|
"text",
|
|
|
|
examples=[["World"], ["Dunya"], ["Monde"]],
|
2022-10-25 07:32:37 +08:00
|
|
|
cache_examples=True,
|
2022-03-26 02:14:28 +08:00
|
|
|
)
|
2023-10-07 02:05:11 +08:00
|
|
|
prediction = io.examples_handler.load_from_cache(1)
|
2022-08-09 01:35:26 +08:00
|
|
|
assert prediction[0] == "Hello Dunya"
|
2022-09-20 22:48:52 +08:00
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_example_caching_relaunch(self, patched_cache_folder, connect):
|
2023-09-21 03:03:55 +08:00
|
|
|
def combine(a, b):
|
|
|
|
return a + " " + b
|
|
|
|
|
|
|
|
with gr.Blocks() as demo:
|
|
|
|
txt = gr.Textbox(label="Input")
|
|
|
|
txt_2 = gr.Textbox(label="Input 2")
|
|
|
|
txt_3 = gr.Textbox(value="", label="Output")
|
|
|
|
btn = gr.Button(value="Submit")
|
|
|
|
btn.click(combine, inputs=[txt, txt_2], outputs=[txt_3])
|
|
|
|
gr.Examples(
|
|
|
|
[["hi", "Adam"], ["hello", "Eve"]],
|
|
|
|
[txt, txt_2],
|
|
|
|
txt_3,
|
|
|
|
combine,
|
|
|
|
cache_examples=True,
|
|
|
|
api_name="examples",
|
|
|
|
)
|
|
|
|
|
|
|
|
with connect(demo) as client:
|
|
|
|
assert client.predict(1, api_name="/examples") == (
|
|
|
|
"hello",
|
|
|
|
"Eve",
|
|
|
|
"hello Eve",
|
|
|
|
)
|
|
|
|
|
|
|
|
with connect(demo) as client:
|
|
|
|
assert client.predict(1, api_name="/examples") == (
|
|
|
|
"hello",
|
|
|
|
"Eve",
|
|
|
|
"hello Eve",
|
|
|
|
)
|
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_caching_image(self, patched_cache_folder):
|
2022-10-07 13:08:30 +08:00
|
|
|
io = gr.Interface(
|
|
|
|
lambda x: x,
|
|
|
|
"image",
|
|
|
|
"image",
|
|
|
|
examples=[["test/test_files/bus.png"]],
|
2022-10-25 07:32:37 +08:00
|
|
|
cache_examples=True,
|
2022-10-07 13:08:30 +08:00
|
|
|
)
|
2023-10-07 02:05:11 +08:00
|
|
|
prediction = io.examples_handler.load_from_cache(0)
|
2023-12-19 09:07:38 +08:00
|
|
|
assert client_utils.encode_url_or_file_to_base64(prediction[0].path).startswith(
|
2023-10-31 12:46:02 +08:00
|
|
|
"data:image/png;base64,iVBORw0KGgoAAA"
|
|
|
|
)
|
2022-10-07 13:08:30 +08:00
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_caching_audio(self, patched_cache_folder):
|
2022-10-07 13:08:30 +08:00
|
|
|
io = gr.Interface(
|
|
|
|
lambda x: x,
|
|
|
|
"audio",
|
|
|
|
"audio",
|
|
|
|
examples=[["test/test_files/audio_sample.wav"]],
|
2022-10-25 07:32:37 +08:00
|
|
|
cache_examples=True,
|
2022-10-07 13:08:30 +08:00
|
|
|
)
|
2023-10-07 02:05:11 +08:00
|
|
|
prediction = io.examples_handler.load_from_cache(0)
|
2023-10-31 12:46:02 +08:00
|
|
|
file = prediction[0].path
|
2023-12-19 09:07:38 +08:00
|
|
|
assert client_utils.encode_url_or_file_to_base64(file).startswith(
|
2023-08-24 02:47:07 +08:00
|
|
|
"data:audio/wav;base64,UklGRgA/"
|
|
|
|
)
|
2022-10-07 13:08:30 +08:00
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_caching_with_update(self, patched_cache_folder):
|
2022-09-22 00:53:06 +08:00
|
|
|
io = gr.Interface(
|
|
|
|
lambda x: gr.update(visible=False),
|
|
|
|
"text",
|
|
|
|
"image",
|
|
|
|
examples=[["World"], ["Dunya"], ["Monde"]],
|
2022-10-25 07:32:37 +08:00
|
|
|
cache_examples=True,
|
2022-09-22 00:53:06 +08:00
|
|
|
)
|
2023-10-07 02:05:11 +08:00
|
|
|
prediction = io.examples_handler.load_from_cache(1)
|
2022-11-16 00:53:35 +08:00
|
|
|
assert prediction[0] == {
|
|
|
|
"visible": False,
|
|
|
|
"__type__": "update",
|
|
|
|
}
|
2022-09-22 00:53:06 +08:00
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_caching_with_mix_update(self, patched_cache_folder):
|
2022-09-22 00:53:06 +08:00
|
|
|
io = gr.Interface(
|
|
|
|
lambda x: [gr.update(lines=4, value="hello"), "test/test_files/bus.png"],
|
|
|
|
"text",
|
|
|
|
["text", "image"],
|
|
|
|
examples=[["World"], ["Dunya"], ["Monde"]],
|
2022-10-25 07:32:37 +08:00
|
|
|
cache_examples=True,
|
2022-09-22 00:53:06 +08:00
|
|
|
)
|
2023-10-07 02:05:11 +08:00
|
|
|
prediction = io.examples_handler.load_from_cache(1)
|
2022-11-16 00:53:35 +08:00
|
|
|
assert prediction[0] == {
|
|
|
|
"lines": 4,
|
|
|
|
"value": "hello",
|
|
|
|
"__type__": "update",
|
|
|
|
}
|
2022-09-22 00:53:06 +08:00
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_caching_with_dict(self, patched_cache_folder):
|
2022-09-22 00:53:06 +08:00
|
|
|
text = gr.Textbox()
|
|
|
|
out = gr.Label()
|
|
|
|
|
|
|
|
io = gr.Interface(
|
2022-11-16 00:53:35 +08:00
|
|
|
lambda _: {text: gr.update(lines=4, interactive=False), out: "lion"},
|
2022-09-22 00:53:06 +08:00
|
|
|
"textbox",
|
|
|
|
[text, out],
|
|
|
|
examples=["abc"],
|
|
|
|
cache_examples=True,
|
|
|
|
)
|
2023-10-07 02:05:11 +08:00
|
|
|
prediction = io.examples_handler.load_from_cache(0)
|
2022-11-16 00:53:35 +08:00
|
|
|
assert prediction == [
|
2023-11-03 07:35:27 +08:00
|
|
|
{"lines": 4, "__type__": "update", "interactive": False},
|
2023-10-31 12:46:02 +08:00
|
|
|
gr.Label.data_model(**{"label": "lion", "confidences": None}),
|
2022-11-16 00:53:35 +08:00
|
|
|
]
|
2022-09-20 22:48:52 +08:00
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_caching_with_generators(self, patched_cache_folder):
|
2023-07-15 07:38:22 +08:00
|
|
|
def test_generator(x):
|
|
|
|
for y in range(len(x)):
|
|
|
|
yield "Your output: " + x[: y + 1]
|
|
|
|
|
|
|
|
io = gr.Interface(
|
|
|
|
test_generator,
|
|
|
|
"textbox",
|
|
|
|
"textbox",
|
|
|
|
examples=["abcdef"],
|
|
|
|
cache_examples=True,
|
|
|
|
)
|
2023-10-07 02:05:11 +08:00
|
|
|
prediction = io.examples_handler.load_from_cache(0)
|
2023-07-15 07:38:22 +08:00
|
|
|
assert prediction[0] == "Your output: abcdef"
|
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_caching_with_generators_and_streamed_output(self, patched_cache_folder):
|
2023-08-24 02:47:07 +08:00
|
|
|
file_dir = Path(Path(__file__).parent, "test_files")
|
|
|
|
audio = str(file_dir / "audio_sample.wav")
|
|
|
|
|
|
|
|
def test_generator(x):
|
|
|
|
for y in range(int(x)):
|
|
|
|
yield audio, y * 5
|
|
|
|
|
|
|
|
io = gr.Interface(
|
|
|
|
test_generator,
|
|
|
|
"number",
|
|
|
|
[gr.Audio(streaming=True), "number"],
|
|
|
|
examples=[3],
|
|
|
|
cache_examples=True,
|
|
|
|
)
|
2023-10-07 02:05:11 +08:00
|
|
|
prediction = io.examples_handler.load_from_cache(0)
|
2023-08-24 02:47:07 +08:00
|
|
|
len_input_audio = len(AudioSegment.from_wav(audio))
|
2023-10-31 12:46:02 +08:00
|
|
|
len_output_audio = len(AudioSegment.from_wav(prediction[0].path))
|
2023-08-24 02:47:07 +08:00
|
|
|
length_ratio = len_output_audio / len_input_audio
|
|
|
|
assert round(length_ratio, 1) == 3.0 # might not be exactly 3x
|
|
|
|
assert float(prediction[1]) == 10.0
|
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_caching_with_async_generators(self, patched_cache_folder):
|
2023-07-15 07:38:22 +08:00
|
|
|
async def test_generator(x):
|
|
|
|
for y in range(len(x)):
|
|
|
|
yield "Your output: " + x[: y + 1]
|
|
|
|
|
|
|
|
io = gr.Interface(
|
|
|
|
test_generator,
|
|
|
|
"textbox",
|
|
|
|
"textbox",
|
|
|
|
examples=["abcdef"],
|
|
|
|
cache_examples=True,
|
|
|
|
)
|
2023-10-07 02:05:11 +08:00
|
|
|
prediction = io.examples_handler.load_from_cache(0)
|
2023-07-15 07:38:22 +08:00
|
|
|
assert prediction[0] == "Your output: abcdef"
|
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_raise_helpful_error_message_if_providing_partial_examples(
|
|
|
|
self, patched_cache_folder, tmp_path
|
|
|
|
):
|
2022-09-22 00:53:06 +08:00
|
|
|
def foo(a, b):
|
|
|
|
return a + b
|
2022-09-20 22:48:52 +08:00
|
|
|
|
|
|
|
with pytest.warns(
|
|
|
|
UserWarning,
|
|
|
|
match="^Examples are being cached but not all input components have",
|
|
|
|
):
|
|
|
|
with pytest.raises(Exception):
|
|
|
|
gr.Interface(
|
|
|
|
foo,
|
|
|
|
inputs=["text", "text"],
|
|
|
|
outputs=["text"],
|
|
|
|
examples=[["foo"], ["bar"]],
|
|
|
|
cache_examples=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.warns(
|
|
|
|
UserWarning,
|
|
|
|
match="^Examples are being cached but not all input components have",
|
|
|
|
):
|
|
|
|
with pytest.raises(Exception):
|
|
|
|
gr.Interface(
|
|
|
|
foo,
|
|
|
|
inputs=["text", "text"],
|
|
|
|
outputs=["text"],
|
|
|
|
examples=[["foo", "bar"], ["bar", None]],
|
|
|
|
cache_examples=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
def foo_no_exception(a, b=2):
|
|
|
|
return a * b
|
|
|
|
|
|
|
|
gr.Interface(
|
|
|
|
foo_no_exception,
|
|
|
|
inputs=["text", "number"],
|
|
|
|
outputs=["text"],
|
|
|
|
examples=[["foo"], ["bar"]],
|
|
|
|
cache_examples=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
def many_missing(a, b, c):
|
|
|
|
return a * b
|
|
|
|
|
|
|
|
with pytest.warns(
|
|
|
|
UserWarning,
|
|
|
|
match="^Examples are being cached but not all input components have",
|
|
|
|
):
|
|
|
|
with pytest.raises(Exception):
|
|
|
|
gr.Interface(
|
|
|
|
many_missing,
|
|
|
|
inputs=["text", "number", "number"],
|
|
|
|
outputs=["text"],
|
|
|
|
examples=[["foo", None, None], ["bar", 2, 3]],
|
|
|
|
cache_examples=True,
|
|
|
|
)
|
2022-10-25 07:32:37 +08:00
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_caching_with_batch(self, patched_cache_folder):
|
2022-10-25 07:32:37 +08:00
|
|
|
def trim_words(words, lens):
|
2023-04-04 06:48:18 +08:00
|
|
|
trimmed_words = [word[:length] for word, length in zip(words, lens)]
|
2022-10-25 07:32:37 +08:00
|
|
|
return [trimmed_words]
|
|
|
|
|
|
|
|
io = gr.Interface(
|
|
|
|
trim_words,
|
|
|
|
["textbox", gr.Number(precision=0)],
|
|
|
|
["textbox"],
|
|
|
|
batch=True,
|
|
|
|
max_batch_size=16,
|
|
|
|
examples=[["hello", 3], ["hi", 4]],
|
|
|
|
cache_examples=True,
|
|
|
|
)
|
2023-10-07 02:05:11 +08:00
|
|
|
prediction = io.examples_handler.load_from_cache(0)
|
2022-10-25 07:32:37 +08:00
|
|
|
assert prediction == ["hel"]
|
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_caching_with_batch_multiple_outputs(self, patched_cache_folder):
|
2022-10-25 07:32:37 +08:00
|
|
|
def trim_words(words, lens):
|
2023-04-04 06:48:18 +08:00
|
|
|
trimmed_words = [word[:length] for word, length in zip(words, lens)]
|
2022-10-25 07:32:37 +08:00
|
|
|
return trimmed_words, lens
|
|
|
|
|
|
|
|
io = gr.Interface(
|
|
|
|
trim_words,
|
|
|
|
["textbox", gr.Number(precision=0)],
|
|
|
|
["textbox", gr.Number(precision=0)],
|
|
|
|
batch=True,
|
|
|
|
max_batch_size=16,
|
|
|
|
examples=[["hello", 3], ["hi", 4]],
|
|
|
|
cache_examples=True,
|
|
|
|
)
|
2023-10-07 02:05:11 +08:00
|
|
|
prediction = io.examples_handler.load_from_cache(0)
|
2022-10-25 07:32:37 +08:00
|
|
|
assert prediction == ["hel", "3"]
|
2023-01-16 10:51:05 +08:00
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_caching_with_non_io_component(self, patched_cache_folder):
|
2023-02-04 08:46:56 +08:00
|
|
|
def predict(name):
|
|
|
|
return name, gr.update(visible=True)
|
|
|
|
|
|
|
|
with gr.Blocks():
|
|
|
|
t1 = gr.Textbox()
|
|
|
|
with gr.Column(visible=False) as c:
|
|
|
|
t2 = gr.Textbox()
|
|
|
|
|
|
|
|
examples = gr.Examples(
|
|
|
|
[["John"], ["Mary"]],
|
|
|
|
fn=predict,
|
|
|
|
inputs=[t1],
|
|
|
|
outputs=[t2, c],
|
|
|
|
cache_examples=True,
|
|
|
|
)
|
|
|
|
|
2023-10-07 02:05:11 +08:00
|
|
|
prediction = examples.load_from_cache(0)
|
2023-02-04 08:46:56 +08:00
|
|
|
assert prediction == ["John", {"visible": True, "__type__": "update"}]
|
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_end_to_end(self, patched_cache_folder):
|
2023-01-16 10:51:05 +08:00
|
|
|
def concatenate(str1, str2):
|
|
|
|
return str1 + str2
|
|
|
|
|
2023-06-30 03:05:25 +08:00
|
|
|
with gr.Blocks() as demo:
|
|
|
|
t1 = gr.Textbox()
|
|
|
|
t2 = gr.Textbox()
|
|
|
|
t1.submit(concatenate, [t1, t2], t2)
|
2023-01-16 10:51:05 +08:00
|
|
|
|
2023-06-30 03:05:25 +08:00
|
|
|
gr.Examples(
|
|
|
|
[["Hello,", None], ["Michael", None]],
|
|
|
|
inputs=[t1, t2],
|
|
|
|
api_name="load_example",
|
|
|
|
)
|
|
|
|
|
|
|
|
app, _, _ = demo.launch(prevent_thread_lock=True)
|
2023-01-16 10:51:05 +08:00
|
|
|
client = TestClient(app)
|
|
|
|
|
2023-06-14 08:18:14 +08:00
|
|
|
response = client.post("/api/load_example/", json={"data": [0]})
|
2023-10-31 12:46:02 +08:00
|
|
|
assert response.json()["data"] == [
|
|
|
|
{
|
|
|
|
"lines": 1,
|
|
|
|
"max_lines": 20,
|
|
|
|
"show_label": True,
|
|
|
|
"container": True,
|
|
|
|
"min_width": 160,
|
|
|
|
"autofocus": False,
|
|
|
|
"autoscroll": True,
|
|
|
|
"elem_classes": [],
|
|
|
|
"rtl": False,
|
|
|
|
"show_copy_button": False,
|
|
|
|
"__type__": "update",
|
|
|
|
"visible": True,
|
|
|
|
"value": "Hello,",
|
|
|
|
"type": "text",
|
|
|
|
}
|
|
|
|
]
|
2023-01-16 10:51:05 +08:00
|
|
|
|
2023-06-14 08:18:14 +08:00
|
|
|
response = client.post("/api/load_example/", json={"data": [1]})
|
2023-10-31 12:46:02 +08:00
|
|
|
assert response.json()["data"] == [
|
|
|
|
{
|
|
|
|
"lines": 1,
|
|
|
|
"max_lines": 20,
|
|
|
|
"show_label": True,
|
|
|
|
"container": True,
|
|
|
|
"min_width": 160,
|
|
|
|
"autofocus": False,
|
|
|
|
"autoscroll": True,
|
|
|
|
"elem_classes": [],
|
|
|
|
"rtl": False,
|
|
|
|
"show_copy_button": False,
|
|
|
|
"__type__": "update",
|
|
|
|
"visible": True,
|
|
|
|
"value": "Michael",
|
|
|
|
"type": "text",
|
|
|
|
}
|
|
|
|
]
|
2023-01-16 10:51:05 +08:00
|
|
|
|
2023-12-19 09:07:38 +08:00
|
|
|
def test_end_to_end_cache_examples(self, patched_cache_folder):
|
2023-01-16 10:51:05 +08:00
|
|
|
def concatenate(str1, str2):
|
2023-04-28 04:09:50 +08:00
|
|
|
return f"{str1} {str2}"
|
2023-01-16 10:51:05 +08:00
|
|
|
|
2023-06-30 03:05:25 +08:00
|
|
|
with gr.Blocks() as demo:
|
|
|
|
t1 = gr.Textbox()
|
|
|
|
t2 = gr.Textbox()
|
|
|
|
t1.submit(concatenate, [t1, t2], t2)
|
|
|
|
|
|
|
|
gr.Examples(
|
|
|
|
examples=[["Hello,", "World"], ["Michael", "Jordan"]],
|
|
|
|
inputs=[t1, t2],
|
|
|
|
outputs=[t2],
|
|
|
|
fn=concatenate,
|
|
|
|
cache_examples=True,
|
|
|
|
api_name="load_example",
|
|
|
|
)
|
2023-01-16 10:51:05 +08:00
|
|
|
|
2023-06-30 03:05:25 +08:00
|
|
|
app, _, _ = demo.launch(prevent_thread_lock=True)
|
2023-01-16 10:51:05 +08:00
|
|
|
client = TestClient(app)
|
|
|
|
|
2023-06-14 08:18:14 +08:00
|
|
|
response = client.post("/api/load_example/", json={"data": [0]})
|
2023-01-16 10:51:05 +08:00
|
|
|
assert response.json()["data"] == ["Hello,", "World", "Hello, World"]
|
|
|
|
|
2023-06-14 08:18:14 +08:00
|
|
|
response = client.post("/api/load_example/", json={"data": [1]})
|
2023-01-16 10:51:05 +08:00
|
|
|
assert response.json()["data"] == ["Michael", "Jordan", "Michael Jordan"]
|
2023-06-06 06:33:31 +08:00
|
|
|
|
|
|
|
|
2023-10-07 02:05:11 +08:00
|
|
|
def test_multiple_file_flagging(tmp_path):
|
2023-12-19 09:07:38 +08:00
|
|
|
with patch("gradio.utils.get_cache_folder", return_value=tmp_path):
|
2023-06-06 06:33:31 +08:00
|
|
|
io = gr.Interface(
|
|
|
|
fn=lambda *x: list(x),
|
|
|
|
inputs=[
|
2023-10-31 12:46:02 +08:00
|
|
|
gr.Image(type="filepath", label="frame 1"),
|
|
|
|
gr.Image(type="filepath", label="frame 2"),
|
2023-06-06 06:33:31 +08:00
|
|
|
],
|
|
|
|
outputs=[gr.Files()],
|
|
|
|
examples=[["test/test_files/cheetah1.jpg", "test/test_files/bus.png"]],
|
|
|
|
cache_examples=True,
|
|
|
|
)
|
2023-10-07 02:05:11 +08:00
|
|
|
prediction = io.examples_handler.load_from_cache(0)
|
2023-06-06 06:33:31 +08:00
|
|
|
|
2023-10-31 12:46:02 +08:00
|
|
|
assert len(prediction[0].root) == 2
|
|
|
|
assert all(isinstance(d, gr.FileData) for d in prediction[0].root)
|
2023-06-08 04:05:01 +08:00
|
|
|
|
|
|
|
|
2023-10-07 02:05:11 +08:00
|
|
|
def test_examples_keep_all_suffixes(tmp_path):
|
2023-12-19 09:07:38 +08:00
|
|
|
with patch("gradio.utils.get_cache_folder", return_value=Path(tempfile.mkdtemp())):
|
2023-06-08 04:05:01 +08:00
|
|
|
file_1 = tmp_path / "foo.bar.txt"
|
|
|
|
file_1.write_text("file 1")
|
|
|
|
file_2 = tmp_path / "file_2"
|
|
|
|
file_2.mkdir(parents=True)
|
|
|
|
file_2 = file_2 / "foo.bar.txt"
|
|
|
|
file_2.write_text("file 2")
|
|
|
|
io = gr.Interface(
|
|
|
|
fn=lambda x: x.name,
|
|
|
|
inputs=gr.File(),
|
|
|
|
outputs=[gr.File()],
|
|
|
|
examples=[[str(file_1)], [str(file_2)]],
|
|
|
|
cache_examples=True,
|
|
|
|
)
|
2023-10-07 02:05:11 +08:00
|
|
|
prediction = io.examples_handler.load_from_cache(0)
|
2023-10-31 12:46:02 +08:00
|
|
|
assert Path(prediction[0].path).read_text() == "file 1"
|
|
|
|
assert prediction[0].orig_name == "foo.bar.txt"
|
|
|
|
assert prediction[0].path.endswith("foo.bar.txt")
|
2023-10-07 02:05:11 +08:00
|
|
|
prediction = io.examples_handler.load_from_cache(1)
|
2023-10-31 12:46:02 +08:00
|
|
|
assert Path(prediction[0].path).read_text() == "file 2"
|
|
|
|
assert prediction[0].orig_name == "foo.bar.txt"
|
|
|
|
assert prediction[0].path.endswith("foo.bar.txt")
|
2023-06-21 01:19:15 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_make_waveform_with_spaces_in_filename():
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
|
|
audio = os.path.join(tmpdirname, "test audio.wav")
|
|
|
|
shutil.copy("test/test_files/audio_sample.wav", audio)
|
|
|
|
waveform = gr.make_waveform(audio)
|
|
|
|
assert waveform.endswith(".mp4")
|
2023-06-22 02:34:33 +08:00
|
|
|
|
2023-08-24 02:45:49 +08:00
|
|
|
try:
|
|
|
|
command = [
|
|
|
|
"ffprobe",
|
|
|
|
"-v",
|
|
|
|
"error",
|
|
|
|
"-select_streams",
|
|
|
|
"v:0",
|
|
|
|
"-show_entries",
|
|
|
|
"stream=width,height",
|
|
|
|
"-of",
|
|
|
|
"json",
|
|
|
|
waveform,
|
|
|
|
]
|
|
|
|
|
|
|
|
result = subprocess.run(command, capture_output=True, text=True, check=True)
|
|
|
|
output = result.stdout
|
|
|
|
data = json.loads(output)
|
|
|
|
|
|
|
|
width = data["streams"][0]["width"]
|
|
|
|
height = data["streams"][0]["height"]
|
|
|
|
assert width == 1000
|
|
|
|
assert height == 400
|
|
|
|
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
print("Error retrieving resolution of output waveform video:", e)
|
|
|
|
|
2023-06-22 02:34:33 +08:00
|
|
|
|
|
|
|
def test_make_waveform_raises_if_ffmpeg_fails(tmp_path, monkeypatch):
|
|
|
|
"""
|
|
|
|
Test that make_waveform raises an exception if ffmpeg fails,
|
|
|
|
instead of returning a path to a non-existent or empty file.
|
|
|
|
"""
|
|
|
|
audio = tmp_path / "test audio.wav"
|
|
|
|
shutil.copy("test/test_files/audio_sample.wav", audio)
|
|
|
|
|
|
|
|
def _failing_ffmpeg(*args, **kwargs):
|
|
|
|
raise subprocess.CalledProcessError(1, "ffmpeg")
|
|
|
|
|
|
|
|
monkeypatch.setattr(subprocess, "call", _failing_ffmpeg)
|
|
|
|
with pytest.raises(Exception):
|
|
|
|
gr.make_waveform(str(audio))
|
2023-06-26 22:26:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestProgressBar:
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_progress_bar(self):
|
|
|
|
with gr.Blocks() as demo:
|
|
|
|
name = gr.Textbox()
|
|
|
|
greeting = gr.Textbox()
|
|
|
|
button = gr.Button(value="Greet")
|
|
|
|
|
|
|
|
def greet(s, prog=gr.Progress()):
|
|
|
|
prog(0, desc="start")
|
|
|
|
time.sleep(0.15)
|
|
|
|
for _ in prog.tqdm(range(4), unit="iter"):
|
|
|
|
time.sleep(0.15)
|
|
|
|
time.sleep(0.15)
|
|
|
|
for _ in tqdm(["a", "b", "c"], desc="alphabet"):
|
|
|
|
time.sleep(0.15)
|
|
|
|
return f"Hello, {s}!"
|
|
|
|
|
|
|
|
button.click(greet, name, greeting)
|
|
|
|
demo.queue(max_size=1).launch(prevent_thread_lock=True)
|
|
|
|
|
2023-12-13 07:24:46 +08:00
|
|
|
client = grc.Client(demo.local_url)
|
|
|
|
job = client.submit("Gradio")
|
|
|
|
|
|
|
|
status_updates = []
|
|
|
|
while not job.done():
|
|
|
|
status = job.status()
|
|
|
|
update = (
|
|
|
|
status.progress_data[0].index if status.progress_data else None,
|
|
|
|
status.progress_data[0].desc if status.progress_data else None,
|
|
|
|
)
|
|
|
|
if update != (None, None) and (
|
|
|
|
len(status_updates) == 0 or status_updates[-1] != update
|
|
|
|
):
|
|
|
|
status_updates.append(update)
|
|
|
|
time.sleep(0.05)
|
|
|
|
|
|
|
|
assert status_updates == [
|
|
|
|
(None, "start"),
|
|
|
|
(0, None),
|
|
|
|
(1, None),
|
|
|
|
(2, None),
|
|
|
|
(3, None),
|
|
|
|
(4, None),
|
2023-06-26 22:26:28 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_progress_bar_track_tqdm(self):
|
|
|
|
with gr.Blocks() as demo:
|
|
|
|
name = gr.Textbox()
|
|
|
|
greeting = gr.Textbox()
|
|
|
|
button = gr.Button(value="Greet")
|
|
|
|
|
|
|
|
def greet(s, prog=gr.Progress(track_tqdm=True)):
|
|
|
|
prog(0, desc="start")
|
|
|
|
time.sleep(0.15)
|
|
|
|
for _ in prog.tqdm(range(4), unit="iter"):
|
|
|
|
time.sleep(0.15)
|
|
|
|
time.sleep(0.15)
|
|
|
|
for _ in tqdm(["a", "b", "c"], desc="alphabet"):
|
|
|
|
time.sleep(0.15)
|
|
|
|
return f"Hello, {s}!"
|
|
|
|
|
|
|
|
button.click(greet, name, greeting)
|
|
|
|
demo.queue(max_size=1).launch(prevent_thread_lock=True)
|
|
|
|
|
2023-12-13 07:24:46 +08:00
|
|
|
client = grc.Client(demo.local_url)
|
|
|
|
job = client.submit("Gradio")
|
|
|
|
|
|
|
|
status_updates = []
|
|
|
|
while not job.done():
|
|
|
|
status = job.status()
|
|
|
|
update = (
|
|
|
|
status.progress_data[0].index if status.progress_data else None,
|
|
|
|
status.progress_data[0].desc if status.progress_data else None,
|
|
|
|
)
|
|
|
|
if update != (None, None) and (
|
|
|
|
len(status_updates) == 0 or status_updates[-1] != update
|
|
|
|
):
|
|
|
|
status_updates.append(update)
|
|
|
|
time.sleep(0.05)
|
|
|
|
|
|
|
|
assert status_updates == [
|
|
|
|
(None, "start"),
|
|
|
|
(0, None),
|
|
|
|
(1, None),
|
|
|
|
(2, None),
|
|
|
|
(3, None),
|
|
|
|
(4, None),
|
|
|
|
(0, "alphabet"),
|
|
|
|
(1, "alphabet"),
|
|
|
|
(2, "alphabet"),
|
2023-06-26 22:26:28 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_progress_bar_track_tqdm_without_iterable(self):
|
|
|
|
def greet(s, _=gr.Progress(track_tqdm=True)):
|
|
|
|
with tqdm(total=len(s)) as progress_bar:
|
|
|
|
for _c in s:
|
|
|
|
progress_bar.update()
|
|
|
|
time.sleep(0.15)
|
|
|
|
return f"Hello, {s}!"
|
|
|
|
|
|
|
|
demo = gr.Interface(greet, "text", "text")
|
|
|
|
demo.queue().launch(prevent_thread_lock=True)
|
|
|
|
|
2023-12-13 07:24:46 +08:00
|
|
|
client = grc.Client(demo.local_url)
|
|
|
|
job = client.submit("Gradio")
|
|
|
|
|
|
|
|
status_updates = []
|
|
|
|
while not job.done():
|
|
|
|
status = job.status()
|
|
|
|
update = (
|
|
|
|
status.progress_data[0].index if status.progress_data else None,
|
|
|
|
status.progress_data[0].unit if status.progress_data else None,
|
|
|
|
)
|
|
|
|
if update != (None, None) and (
|
|
|
|
len(status_updates) == 0 or status_updates[-1] != update
|
|
|
|
):
|
|
|
|
status_updates.append(update)
|
|
|
|
time.sleep(0.05)
|
|
|
|
|
|
|
|
assert status_updates == [
|
|
|
|
(1, "steps"),
|
|
|
|
(2, "steps"),
|
|
|
|
(3, "steps"),
|
|
|
|
(4, "steps"),
|
|
|
|
(5, "steps"),
|
|
|
|
(6, "steps"),
|
2023-06-26 22:26:28 +08:00
|
|
|
]
|
2023-07-04 05:28:39 +08:00
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_info_and_warning_alerts(self):
|
|
|
|
def greet(s):
|
|
|
|
for _c in s:
|
|
|
|
gr.Info(f"Letter {_c}")
|
|
|
|
time.sleep(0.15)
|
|
|
|
if len(s) < 5:
|
|
|
|
gr.Warning("Too short!")
|
2023-12-13 07:24:46 +08:00
|
|
|
time.sleep(0.15)
|
2023-07-04 05:28:39 +08:00
|
|
|
return f"Hello, {s}!"
|
|
|
|
|
|
|
|
demo = gr.Interface(greet, "text", "text")
|
|
|
|
demo.queue().launch(prevent_thread_lock=True)
|
|
|
|
|
2023-12-13 07:24:46 +08:00
|
|
|
client = grc.Client(demo.local_url)
|
|
|
|
job = client.submit("Jon")
|
|
|
|
|
|
|
|
status_updates = []
|
|
|
|
while not job.done():
|
|
|
|
status = job.status()
|
|
|
|
update = status.log
|
|
|
|
if update is not None and (
|
|
|
|
len(status_updates) == 0 or status_updates[-1] != update
|
|
|
|
):
|
|
|
|
status_updates.append(update)
|
|
|
|
time.sleep(0.05)
|
|
|
|
|
|
|
|
assert status_updates == [
|
|
|
|
("Letter J", "info"),
|
|
|
|
("Letter o", "info"),
|
|
|
|
("Letter n", "info"),
|
|
|
|
("Too short!", "warning"),
|
2023-07-04 05:28:39 +08:00
|
|
|
]
|
2023-09-22 04:20:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
@pytest.mark.parametrize("async_handler", [True, False])
|
|
|
|
async def test_info_isolation(async_handler: bool):
|
|
|
|
async def greet_async(name):
|
|
|
|
await asyncio.sleep(2)
|
|
|
|
gr.Info(f"Hello {name}")
|
2023-12-13 07:24:46 +08:00
|
|
|
await asyncio.sleep(1)
|
2023-09-22 04:20:47 +08:00
|
|
|
return name
|
|
|
|
|
|
|
|
def greet_sync(name):
|
|
|
|
time.sleep(2)
|
|
|
|
gr.Info(f"Hello {name}")
|
2023-12-13 07:24:46 +08:00
|
|
|
time.sleep(1)
|
2023-09-22 04:20:47 +08:00
|
|
|
return name
|
|
|
|
|
2023-10-31 17:41:19 +08:00
|
|
|
demo = gr.Interface(
|
|
|
|
greet_async if async_handler else greet_sync,
|
|
|
|
"text",
|
|
|
|
"text",
|
|
|
|
concurrency_limit=2,
|
|
|
|
)
|
|
|
|
demo.launch(prevent_thread_lock=True)
|
2023-09-22 04:20:47 +08:00
|
|
|
|
|
|
|
async def session_interaction(name, delay=0):
|
2023-12-13 07:24:46 +08:00
|
|
|
client = grc.Client(demo.local_url)
|
|
|
|
job = client.submit(name)
|
|
|
|
|
|
|
|
status_updates = []
|
|
|
|
while not job.done():
|
|
|
|
status = job.status()
|
|
|
|
update = status.log
|
|
|
|
if update is not None and (
|
|
|
|
len(status_updates) == 0 or status_updates[-1] != update
|
|
|
|
):
|
|
|
|
status_updates.append(update)
|
|
|
|
time.sleep(0.05)
|
|
|
|
return status_updates[-1][0] if status_updates else None
|
2023-09-22 04:20:47 +08:00
|
|
|
|
|
|
|
alice_logs, bob_logs = await asyncio.gather(
|
|
|
|
session_interaction("Alice"),
|
|
|
|
session_interaction("Bob", delay=1),
|
|
|
|
)
|
|
|
|
|
2023-12-13 07:24:46 +08:00
|
|
|
assert alice_logs == "Hello Alice"
|
|
|
|
assert bob_logs == "Hello Bob"
|