abidlabs's picture
abidlabs HF Staff
Gradio from PR 14014 at 00fa4a6a5
c43092c verified
Raw History Blame Contribute Delete
1.72 kB
"""Process-executor check on Spaces: SSR, and ZeroGPU when ZERO=1."""
import os
import subprocess
import sys
# The Space installs Gradio 6.29.1; this app needs the PR's build, which a
# Space's build step cannot see, so install it here once and start again
if not os.environ.get("PR_GRADIO_INSTALLED"):
here = os.path.dirname(os.path.abspath(__file__))
subprocess.check_call(
[
sys.executable,
"-m",
"pip",
"install",
"--no-cache-dir",
f"{here}/wheels/gradio_client-2.7.2-py3-none-any.whl",
f"{here}/wheels/gradio-6.30.0-py3-none-any.whl",
]
)
os.environ["PR_GRADIO_INSTALLED"] = "1"
os.execv(sys.executable, [sys.executable, *sys.argv])
import gradio as gr
ZERO = os.environ.get("ZERO") == "1"
if ZERO:
import spaces
import torch
@spaces.GPU
def gpu(x: str) -> str:
return f"{x} on {torch.cuda.get_device_name(0)} (app pid {os.getpid()})"
def echo(x: str) -> str:
return f"{x[::-1]} (pid {os.getpid()})"
def info() -> str:
return (
f"gradio {gr.__version__} (PR commit {open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'PR_COMMIT')).read().strip()}); executor "
f"{'process' if getattr(demo, '_process_worker', None) else 'thread'}; "
f"ssr {getattr(demo, 'ssr_mode', None)}; pid {os.getpid()}"
)
with gr.Blocks() as demo:
text = gr.Textbox(label="input")
out = gr.Textbox(label="output")
gr.Button("echo").click(echo, text, out, api_name="echo")
gr.Button("info").click(info, None, out, api_name="info")
if ZERO:
gr.Button("gpu").click(gpu, text, out, api_name="gpu")
demo.launch()