Download scripts/space_runtime.py from m4az19/saef-api: direct link, hf CLI and curl.
- Browser
- Download file 3.26 kB
-
https://huggingface.co/spaces/m4az19/saef-api/resolve/main/scripts/space_runtime.py
- Command line
-
hf download hf://spaces/m4az19/saef-api/scripts/space_runtime.py
-
curl -L -o space_runtime.py https://huggingface.co/spaces/m4az19/saef-api/resolve/main/scripts/space_runtime.py
3.26 kB
| """Single-container Hugging Face runtime; Ollama never listens publicly.""" | |
| import argparse | |
| import os | |
| import signal | |
| import subprocess | |
| import sys | |
| import time | |
| import urllib.error | |
| import urllib.request | |
| from pathlib import Path | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument('--download-model', action='store_true') | |
| args = parser.parse_args() | |
| if not args.download_model and os.getenv('SPACE_ID'): | |
| missing = [key for key in ('SATFORGE_DASHBOARD_KEY', 'SATFORGE_DEVICE_KEY') | |
| if not os.getenv(key)] | |
| if missing: | |
| raise SystemExit('Add these Space Secrets before starting: ' + ', '.join(missing)) | |
| Path(os.environ['OLLAMA_MODELS']).mkdir(parents=True, exist_ok=True) | |
| # This supervisor owns a local instance, regardless of inherited host settings. | |
| os.environ['OLLAMA_HOST'] = '127.0.0.1:11434' | |
| os.environ['SATFORGE_OLLAMA_URL'] = 'http://127.0.0.1:11434' | |
| os.environ['OLLAMA_NO_CLOUD'] = '1' | |
| children = [] | |
| stopping = False | |
| def stop(signum, frame): | |
| nonlocal stopping | |
| stopping = True | |
| signal.signal(signal.SIGTERM, stop) | |
| signal.signal(signal.SIGINT, stop) | |
| try: | |
| ollama = subprocess.Popen(['ollama', 'serve']) | |
| children.append(ollama) | |
| deadline = time.monotonic() + 120 | |
| while not stopping: | |
| if ollama.poll() is not None: | |
| raise RuntimeError('Ollama exited before becoming ready') | |
| try: | |
| with urllib.request.urlopen('http://127.0.0.1:11434/api/tags', timeout=2): | |
| break | |
| except (urllib.error.URLError, TimeoutError): | |
| if time.monotonic() > deadline: | |
| raise RuntimeError('Ollama did not become ready within 120 seconds') | |
| time.sleep(1) | |
| if stopping: | |
| return | |
| if args.download_model: | |
| pull = subprocess.Popen(['ollama', 'pull', os.getenv('SATFORGE_NARRATOR_MODEL', 'qwen3.5:0.8b')]) | |
| children.append(pull) | |
| while pull.poll() is None and not stopping: | |
| time.sleep(1) | |
| if stopping or pull.returncode: | |
| raise RuntimeError('Explanation model download did not complete') | |
| return | |
| Path(os.environ['SATFORGE_DATA']).mkdir(parents=True, exist_ok=True) | |
| api = subprocess.Popen([sys.executable, '-m', 'uvicorn', 'backend.app:app', | |
| '--host', '0.0.0.0', '--port', os.getenv('PORT', '7860'), | |
| '--workers', '1']) | |
| children.append(api) | |
| while not stopping: | |
| if any(child.poll() is not None for child in children): | |
| raise RuntimeError('A Space service exited; stopping container for restart') | |
| time.sleep(1) | |
| finally: | |
| for child in reversed(children): | |
| if child.poll() is None: | |
| child.terminate() | |
| deadline = time.monotonic() + 20 | |
| for child in reversed(children): | |
| try: | |
| child.wait(timeout=max(0.1, deadline-time.monotonic())) | |
| except subprocess.TimeoutExpired: | |
| child.kill() | |
| child.wait() | |
| if __name__ == '__main__': | |
| main() | |