Spaces:
Sleeping
Sleeping
Toggle fix at runtime on stock gradio
Browse files
app.py
CHANGED
|
@@ -2,10 +2,48 @@ import os
|
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import httpx
|
|
|
|
|
|
|
| 5 |
|
| 6 |
PHOTO = "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/cheetah1.jpg"
|
|
|
|
| 7 |
PORT = int(os.environ.get("GRADIO_SERVER_PORT", 7860))
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
def describe(photo):
|
|
@@ -13,42 +51,46 @@ def describe(photo):
|
|
| 13 |
|
| 14 |
|
| 15 |
def advertised_default():
|
| 16 |
-
"""What /info tells an API client this endpoint's default photo is."""
|
| 17 |
info = httpx.get(f"http://127.0.0.1:{PORT}/gradio_api/info", timeout=30).json()
|
| 18 |
default = info["named_endpoints"]["/describe"]["parameters"][0]["parameter_default"]
|
| 19 |
url = default.get("url") or ""
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 33 |
f"| gradio | `{gr.__version__}` |\n"
|
| 34 |
-
f"|
|
| 35 |
f"| `url` | `{url}` |\n"
|
| 36 |
)
|
| 37 |
-
return url, report
|
| 38 |
|
| 39 |
|
| 40 |
-
with gr.Blocks(title="Default file url
|
| 41 |
gr.Markdown(
|
| 42 |
"# The default file a client actually gets\n"
|
| 43 |
-
"This app's `photo` parameter has a default.
|
| 44 |
-
"default resolves to **for an API caller reading `/info`** β which
|
| 45 |
-
"be the same cheetah
|
| 46 |
)
|
| 47 |
with gr.Row():
|
| 48 |
-
photo = gr.Image(
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
report = gr.Markdown()
|
| 51 |
-
path_out = gr.Textbox(
|
| 52 |
|
| 53 |
gr.Button("Describe", visible=False).click(
|
| 54 |
describe, photo, path_out, api_name="describe"
|
|
|
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import httpx
|
| 5 |
+
from gradio import route_utils
|
| 6 |
+
from gradio_client import utils as client_utils
|
| 7 |
|
| 8 |
PHOTO = "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/cheetah1.jpg"
|
| 9 |
+
BUS = "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png"
|
| 10 |
PORT = int(os.environ.get("GRADIO_SERVER_PORT", 7860))
|
| 11 |
+
APPLY_FIX = os.environ.get("APPLY_FIX", "").lower() in ("1", "true", "yes")
|
| 12 |
+
ROOT = (
|
| 13 |
+
f"https://{os.environ['SPACE_HOST']}"
|
| 14 |
+
if os.environ.get("SPACE_HOST")
|
| 15 |
+
else f"http://127.0.0.1:{PORT}"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def patched_update_example_values(api_info, root_url=ROOT):
|
| 20 |
+
"""gradio/route_utils.py with the root url actually supplied."""
|
| 21 |
+
|
| 22 |
+
def _add_root_url(file_dict):
|
| 23 |
+
default_value = file_dict.get("parameter_default")
|
| 24 |
+
if default_value is not None and client_utils.is_file_obj_with_url(
|
| 25 |
+
default_value
|
| 26 |
+
):
|
| 27 |
+
url = default_value["url"]
|
| 28 |
+
if client_utils.is_http_url_like(url):
|
| 29 |
+
return file_dict
|
| 30 |
+
if root_url and url:
|
| 31 |
+
default_value["url"] = f"{root_url.rstrip('/')}/{url.lstrip('/')}"
|
| 32 |
+
return file_dict
|
| 33 |
+
example_input = file_dict.get("example_input")
|
| 34 |
+
if isinstance(example_input, dict) and "url" in example_input:
|
| 35 |
+
default_value["url"] = example_input["url"]
|
| 36 |
+
return file_dict
|
| 37 |
+
|
| 38 |
+
return client_utils.traverse(
|
| 39 |
+
api_info,
|
| 40 |
+
_add_root_url,
|
| 41 |
+
lambda d: isinstance(d, dict) and "parameter_default" in d,
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
if APPLY_FIX:
|
| 46 |
+
route_utils.update_example_values_to_use_public_url = patched_update_example_values
|
| 47 |
|
| 48 |
|
| 49 |
def describe(photo):
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
def advertised_default():
|
|
|
|
| 54 |
info = httpx.get(f"http://127.0.0.1:{PORT}/gradio_api/info", timeout=30).json()
|
| 55 |
default = info["named_endpoints"]["/describe"]["parameters"][0]["parameter_default"]
|
| 56 |
url = default.get("url") or ""
|
| 57 |
|
| 58 |
+
if url == BUS:
|
| 59 |
+
verdict = (
|
| 60 |
+
"### β Broken\n`/info` advertises a **bus** β `Image.example_payload()` "
|
| 61 |
+
"standing in for the default. A client calling `/describe` without the "
|
| 62 |
+
"`photo` argument fetches the wrong file."
|
| 63 |
+
)
|
| 64 |
+
else:
|
| 65 |
+
verdict = (
|
| 66 |
+
"### β
Fixed\n`/info` advertises the **cheetah** this app really serves, "
|
| 67 |
+
"at an absolute url the client can fetch."
|
| 68 |
+
)
|
| 69 |
+
return url, (
|
| 70 |
+
f"{verdict}\n\n| | |\n|---|---|\n"
|
| 71 |
f"| gradio | `{gr.__version__}` |\n"
|
| 72 |
+
f"| fix applied | `{APPLY_FIX}` |\n"
|
| 73 |
f"| `url` | `{url}` |\n"
|
| 74 |
)
|
|
|
|
| 75 |
|
| 76 |
|
| 77 |
+
with gr.Blocks(title="Default file url") as demo:
|
| 78 |
gr.Markdown(
|
| 79 |
"# The default file a client actually gets\n"
|
| 80 |
+
"This app's `photo` parameter has a default. On the right is the image "
|
| 81 |
+
"that default resolves to **for an API caller reading `/info`** β which "
|
| 82 |
+
"should be the same cheetah."
|
| 83 |
)
|
| 84 |
with gr.Row():
|
| 85 |
+
photo = gr.Image(
|
| 86 |
+
label="The app's default (what you see)", value=PHOTO,
|
| 87 |
+
type="filepath", height=320,
|
| 88 |
+
)
|
| 89 |
+
served = gr.Image(
|
| 90 |
+
label="What /info advertises (what a client gets)", height=320,
|
| 91 |
+
)
|
| 92 |
report = gr.Markdown()
|
| 93 |
+
path_out = gr.Textbox(visible=False)
|
| 94 |
|
| 95 |
gr.Button("Describe", visible=False).click(
|
| 96 |
describe, photo, path_out, api_name="describe"
|