File size: 3,257 Bytes
124816b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | """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()
|