Spaces:
Running on Zero
Running on Zero
Download app.py from JoaoK90/token_Explorer: direct link, hf CLI and curl.
- Browser
- Download file 2.57 kB
-
https://huggingface.co/spaces/JoaoK90/token_Explorer/resolve/main/app.py
- Command line
-
hf download hf://spaces/JoaoK90/token_Explorer/app.py
-
curl -L -o app.py https://huggingface.co/spaces/JoaoK90/token_Explorer/resolve/main/app.py
2.57 kB
| """Hugging Face Space entry point for the step-by-step token explorer. | |
| Downloads GPT-2 once into the Space's cache, then starts the same interface as | |
| the local explorer, using the SynthID Text code bundled in ``synthid_text_repo``. | |
| """ | |
| from __future__ import annotations | |
| import os | |
| from pathlib import Path | |
| import sys | |
| try: # On Spaces this package must be imported before anything touches CUDA. | |
| import spaces # noqa: F401 | |
| except ImportError: | |
| pass | |
| HERE = Path(__file__).resolve().parent | |
| os.environ.setdefault("EXPLORER_MODELS", "gpt2") | |
| MODELS = [name.strip() for name in os.environ["EXPLORER_MODELS"].split(",") | |
| if name.strip()] | |
| WEIGHTS = ["config.json", "generation_config.json", "*.safetensors", | |
| "model.safetensors.index.json", "vocab.json", "merges.txt", "tokenizer.json", | |
| "tokenizer.model", "tokenizer_config.json", "special_tokens_map.json"] | |
| def fetch_models() -> None: | |
| """Make the checkpoints available in the local cache (the explorer runs offline). | |
| Gemma checkpoints are gated: accept the licence on the model page and give | |
| the Space a read token in a secret named HF_TOKEN. | |
| """ | |
| from huggingface_hub import snapshot_download | |
| for name in MODELS: | |
| try: | |
| snapshot_download(name, allow_patterns=WEIGHTS, local_files_only=True) | |
| except Exception: # not cached yet | |
| snapshot_download(name, allow_patterns=WEIGHTS) | |
| def build(): | |
| """Download the models, load the SynthID code and create the interface.""" | |
| os.environ.setdefault("SYNTHID_REPO", str(HERE / "synthid_text_repo")) | |
| os.environ.setdefault("GRADIO_ANALYTICS_ENABLED", "False") | |
| sys.path.insert(0, str(HERE)) | |
| fetch_models() # before bootstrap switches Hugging Face to offline mode | |
| from synthid_demo import bootstrap | |
| bootstrap.load() | |
| from synthid_demo import engine, ui | |
| # Load the model(s) now: ZeroGPU needs them placed on the GPU at start-up. | |
| for name in (MODELS if engine.ZERO_GPU else MODELS[:1]): | |
| engine.get_bundle(name) | |
| return ui, ui.build(llm_only=os.environ.get("EXPLORER_LLM_ONLY", "") == "1") | |
| # Module-level `demo`, where Spaces (and Gradio's reloader) look for the app. | |
| ui, demo = build() | |
| if __name__ == "__main__": | |
| demo.queue(default_concurrency_limit=2).launch( | |
| server_name=os.environ.get("GRADIO_SERVER_NAME", "0.0.0.0"), | |
| server_port=int(os.environ.get("GRADIO_SERVER_PORT", "7860")), | |
| theme=ui.THEME, css=ui.STYLE, head=ui.HEAD, ssr_mode=False, | |
| footer_links=["gradio", "settings"], | |
| ) | |