#!/usr/bin/env python3 """ HuggingRun default demo: minimal HTTP app on port 7860 with persistence. - Serves a simple page showing visit counter and PERSIST_PATH status. - Counter is stored in PERSIST_PATH/counter.txt so it survives restarts (via HF Dataset sync). """ import os import http.server import socketserver from pathlib import Path PORT = int(os.environ.get("PORT", "7860")) PERSIST_PATH = Path(os.environ.get("PERSIST_PATH", "/data")) COUNTER_FILE = PERSIST_PATH / "counter.txt" def get_counter(): PERSIST_PATH.mkdir(parents=True, exist_ok=True) try: return int(COUNTER_FILE.read_text().strip()) except (FileNotFoundError, ValueError): return 0 def inc_counter(): c = get_counter() + 1 PERSIST_PATH.mkdir(parents=True, exist_ok=True) COUNTER_FILE.write_text(str(c)) return c class DemoHandler(http.server.BaseHTTPRequestHandler): def do_GET(self): count = inc_counter() body = f"""
This is the default HuggingRun demo. Persistence path: {PERSIST_PATH}
Visit count (persisted): {count}
Set RUN_CMD in your Space secrets to run your own app.