Space

UI Spaces Echo

ui-spaces/ui-spaces-echo

1.0 KBHistoryRawDownload
Viewing revisionmain
import os

import gradio as gr
import humanize


def echo(text):
    n = len(text or "")
    return f"{text} ({n} characters, {humanize.apnumber(n)})"


def env_check():
    greeting = os.environ.get("UI_GREETING", "<unset>")
    token_set = "set" if os.environ.get("UI_TOKEN") else "unset"
    data_ok = os.path.isdir("/data") and os.access("/data", os.W_OK)
    if data_ok:
        path = "/data/ui-spaces-counter.txt"
        count = int(open(path).read()) + 1 if os.path.exists(path) else 1
        with open(path, "w") as f:
            f.write(str(count))
    else:
        count = "no /data"
    return f"UI_GREETING={greeting}; UI_TOKEN is {token_set}; /data writable={data_ok}; boots={count}"


BOOT = env_check()
print("ui-spaces env check:", BOOT, flush=True)

with gr.Blocks(title="UI Spaces Echo") as demo:
    gr.Markdown("# UI Spaces Echo")
    gr.Markdown(BOOT)
    inp = gr.Textbox(label="Text")
    out = gr.Textbox(label="Echo")
    gr.Button("Submit").click(echo, inp, out, api_name="predict")

if __name__ == "__main__":
    demo.launch()