File size: 4,307 Bytes
4f96544 b3f1931 0a16bb6 b3f1931 4f96544 a7442af b3f1931 a7442af b3f1931 a7442af 4f96544 | 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | from __future__ import annotations
import os
import shutil
import sys
from pathlib import Path
DEFAULT_ENV = {
"BRAIN_AUTO_NGROK": "0",
"BRAIN_AUTO_PUBLISH_URL_ON_STARTUP": "0",
"BRAIN_LANGUAGES": "ar,en",
"BRAIN_PLATFORM_NAME": "ai_coder_main",
"BRAIN_PROVIDER": "huggingface",
"BRAIN_REUSE_PUBLIC_URL_ON_RESTART": "0",
"BRAIN_ROLES": "coding,planner,fallback",
"BRAIN_TEMPLATE": "hf-space-cpu",
"BRAIN_TUNNEL_PROVIDER": "none",
"FIREBASE_ENABLED": "0",
"FIREBASE_NAMESPACE": "kapo",
"FIREBASE_PROJECT_ID": "citadel4travels",
"GOOGLE_DRIVE_BOOTSTRAP_URL": "https://drive.google.com/uc?export=download&id=19jyBWsQ9ciJVPi2PUigu5ti3gJ24A6TG",
"HF_ACCELERATOR": "cpu",
"HF_SPACE_DOCKER": "1",
"KAGGLE_AUTO_BOOTSTRAP": "0",
"KAPO_BOOTSTRAP_URL": "https://drive.google.com/uc?export=download&id=19jyBWsQ9ciJVPi2PUigu5ti3gJ24A6TG",
"KAPO_COMPUTE_PROFILE": "cpu",
"KAPO_HF_INFERENCE_API": "1",
"KAPO_HF_TRANSFORMERS_RUNTIME": "0",
"KAPO_LAZY_EMBED_STARTUP": "1",
"KAPO_LAZY_MODEL_STARTUP": "1",
"KAPO_PATCH_BUNDLE_URL": "https://drive.google.com/uc?export=download&id=16rIe05GZihhAz7ba8E-WibKaJKbh9eu1",
"KAPO_PATCH_MANIFEST_URL": "https://drive.google.com/uc?export=download&id=1jLuPMCA3hp9qstZZtpBNzTK0XOmLrV8b",
"KAPO_REMOTE_ENV_PASSWORD_B64": "cENvSnljQ0Z6RUN2azRpcUFLVUdQLW9BbVBoTEtNOFE",
"KAPO_REMOTE_ENV_URL_B64": "aHR0cHM6Ly9kcml2ZS5nb29nbGUuY29tL3VjP2V4cG9ydD1kb3dubG9hZCZpZD0xdjlZRDlNYlBKNUdYNk9WanVHM3hOR05xLWJfQlJfTGY",
"KAPO_SHARED_STATE_BACKEND": "google_drive",
"MODEL_PROFILE_ID": "hf-coder-qwen25-coder-7b-instruct",
"MODEL_REPO": "Qwen/Qwen2.5-Coder-1.5B-Instruct",
"REMOTE_BRAIN_ONLY": "1",
"SPACE_PUBLIC_URL": "https://MrA7A1-AiCoder.hf.space"
}
def _copy_tree(source: Path, target: Path) -> None:
if target.exists():
shutil.rmtree(target, ignore_errors=True)
shutil.copytree(
source,
target,
ignore=shutil.ignore_patterns('__pycache__', '*.pyc', '.git', '.venv'),
)
def _merge_overlay(overlay_root: Path, runtime_root: Path) -> None:
if not overlay_root.exists():
return
for path in sorted(overlay_root.rglob('*')):
rel = path.relative_to(overlay_root)
dst = runtime_root / rel
if path.is_dir():
dst.mkdir(parents=True, exist_ok=True)
continue
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(path, dst)
def main() -> None:
source_root = Path(os.getenv('KAPO_SPACE_SOURCE_ROOT', '/app')).resolve()
default_root = Path('/data/kapo_runtime/current') if Path('/data').exists() else Path('/tmp/kapo_runtime/current')
runtime_root = Path(os.getenv('KAPO_RUNTIME_ROOT', str(default_root))).resolve()
overlay_root = Path(os.getenv('KAPO_OVERLAY_ROOT', str(runtime_root.parent / 'overlay'))).resolve()
runtime_pkg = runtime_root / 'brain_server'
source_pkg = source_root / 'brain_server'
for key, value in DEFAULT_ENV.items():
os.environ.setdefault(str(key), str(value))
space_host = str(os.getenv('SPACE_HOST', '')).strip().rstrip('/')
space_id = str(os.getenv('SPACE_ID', '')).strip()
public_url = ''
if space_host:
public_url = space_host if space_host.startswith('http://') or space_host.startswith('https://') else f'https://{space_host}'
elif space_id and '/' in space_id:
public_url = 'https://' + space_id.replace('/', '-').lower() + '.hf.space'
if public_url:
os.environ['BRAIN_PUBLIC_URL'] = public_url.rstrip('/')
runtime_root.mkdir(parents=True, exist_ok=True)
overlay_root.mkdir(parents=True, exist_ok=True)
if not runtime_pkg.exists():
_copy_tree(source_pkg, runtime_pkg)
_merge_overlay(overlay_root, runtime_root)
os.environ['KAPO_RUNTIME_ROOT'] = str(runtime_root)
os.environ['KAPO_SYNC_ROOT'] = str(runtime_root)
os.environ['KAPO_OVERLAY_ROOT'] = str(overlay_root)
port = str(os.getenv('PORT', '7860') or '7860')
os.execvp(
sys.executable,
[
sys.executable,
'-m',
'uvicorn',
'api.main:app',
'--host',
'0.0.0.0',
'--port',
port,
'--app-dir',
str(runtime_pkg),
],
)
if __name__ == '__main__':
main()
|