File size: 3,891 Bytes
73268e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""Hugging Face Gradio Space entrypoint.

Copied to the Space repo root as app.py. Starts FastAPI on 127.0.0.1:8000,
then launches the existing Gradio Blocks. Demo results come from
CODEBUDDY_SPACE_DEMO=1 — this file does not load Gemma, Whisper, or TTS.
"""

from __future__ import annotations

import importlib.util
import os
import subprocess
import sys
import tempfile
import time
import urllib.error
import urllib.request
from pathlib import Path

import spaces


def _repo_root() -> Path:
    """GitHub: spaces/app.py. Space: /app.py next to backend/."""
    here = Path(__file__).resolve().parent
    if (here / "backend").is_dir():
        return here
    parent = here.parent
    if (parent / "backend").is_dir():
        return parent
    raise RuntimeError(
        "Tidak menemukan folder backend/ di samping atau di atas app.py. "
        "Salin spaces/app.py ke akar Space bersama pohon backend/ dan frontend/."
    )


_ROOT = _repo_root()
sys.path.insert(0, str(_ROOT / "backend"))
sys.path.insert(0, str(_ROOT / "frontend"))

os.environ.setdefault("CODEBUDDY_SPACE_DEMO", "1")
os.environ.setdefault("CODEBUDDY_API", "http://127.0.0.1:8000")
os.environ.setdefault(
    "DATABASE_URL",
    "sqlite+aiosqlite:////tmp/codebuddy-data/codebuddy.db",
)

_HEALTH = "http://127.0.0.1:8000/health"
_HEALTH_TRIES = 60
_API_LOG = Path("/tmp/codebuddy-api.log")


@spaces.GPU(duration=15)
def _zero_gpu_placeholder() -> None:
    """Unused. Present so this Gradio Space can be created on ZeroGPU hardware."""
    return None


def _wait_backend() -> None:
    last_error = "tidak ada respons"
    for _ in range(_HEALTH_TRIES):
        try:
            with urllib.request.urlopen(_HEALTH, timeout=2) as resp:
                if resp.status == 200:
                    return
                last_error = f"HTTP {resp.status}"
        except (urllib.error.URLError, TimeoutError, OSError) as exc:
            last_error = str(exc) or type(exc).__name__
        time.sleep(1)
    raise RuntimeError(
        f"Backend FastAPI tidak siap di {_HEALTH} setelah {_HEALTH_TRIES} detik "
        f"({last_error}). Cek {_API_LOG}."
    )


def _start_backend() -> None:
    Path("/tmp/codebuddy-data").mkdir(parents=True, exist_ok=True)
    env = os.environ.copy()
    backend = str(_ROOT / "backend")
    existing = env.get("PYTHONPATH", "")
    env["PYTHONPATH"] = backend if not existing else backend + os.pathsep + existing
    log = open(_API_LOG, "ab", buffering=0)
    subprocess.Popen(
        [
            sys.executable,
            "-m",
            "uvicorn",
            "main:app",
            "--host",
            "127.0.0.1",
            "--port",
            "8000",
        ],
        cwd=str(_ROOT),
        env=env,
        stdout=log,
        stderr=subprocess.STDOUT,
        start_new_session=True,
    )


def _load_frontend_demo():
    path = _ROOT / "frontend" / "app.py"
    spec = importlib.util.spec_from_file_location("codebuddy_frontend", path)
    if spec is None or spec.loader is None:
        raise RuntimeError(f"Tidak bisa memuat Gradio Blocks dari {path}")
    module = importlib.util.module_from_spec(spec)
    sys.modules["codebuddy_frontend"] = module
    spec.loader.exec_module(module)
    blok = getattr(module, "demo", None)
    if blok is None:
        raise RuntimeError("frontend/app.py tidak mengekspor objek demo")
    return blok, module


_start_backend()
_wait_backend()
demo, _frontend = _load_frontend_demo()

demo.launch(
    server_name="0.0.0.0",
    server_port=7860,
    show_error=True,
    theme=_frontend.gr.themes.Soft(
        primary_hue="indigo",
        secondary_hue="violet",
        neutral_hue="slate",
    ),
    css=_frontend.dapatkan_css_gradi(),
    head=_frontend.FORCE_LIGHT_JS,
    allowed_paths=[
        str(_ROOT / "frontend" / "assets"),
        str(Path(tempfile.gettempdir()) / "codebuddy_demo"),
    ],
)