import datetime as dt import os from pathlib import Path import gradio as gr def _build_label() -> str: version_file = Path("VERSION") version_from_file = "" if version_file.exists(): version_from_file = version_file.read_text(encoding="utf-8").strip() commit = ( os.getenv("GITHUB_SHA") or os.getenv("COMMIT_SHA") or os.getenv("SPACE_COMMIT_SHA") or version_from_file or "local" ) short_commit = commit[:7] if commit != "local" else commit version = os.getenv("APP_VERSION") or short_commit deployed_at = dt.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC") return f"Version: {version} | Commit: {short_commit} | Loaded: {deployed_at}" def echo_with_meta(message: str) -> str: clean = (message or "").strip() if not clean: return "Type something to test your CI/CD sync." now = dt.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC") return ( f"You typed: {clean}\n" f"Length: {len(clean)} characters\n" f"Built from GitHub sync demo\n" f"Timestamp: {now}" ) with gr.Blocks(title="GitHub to Hugging Face Sync Demo") as demo: gr.Markdown("# GitHub to HuggingFace CI/CD Demo") gr.Markdown(f"**{_build_label()}**") gr.Markdown( "Edit this app in GitHub, push a commit, and your Hugging Face Space updates automatically." ) text = gr.Textbox( label="Try a message", placeholder="Change this app in GitHub and test it here", lines=3, ) output = gr.Textbox(label="Result", lines=6) run_btn = gr.Button("Run") run_btn.click(fn=echo_with_meta, inputs=[text], outputs=[output]) if __name__ == "__main__": # server_name="0.0.0.0" is required inside HF Space containers. # root_path ensures Gradio resolves JS/CSS assets correctly when running # behind a reverse proxy or custom domain. demo.launch(server_name="0.0.0.0", root_path=os.getenv("GRADIO_ROOT_PATH", ""))