Upload app (2).py
Browse files- app (2).py +58 -0
app (2).py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Hugging Face Space entrypoint for opencode.
|
| 3 |
+
|
| 4 |
+
Serves the opencode coding agent (HTTP API + built-in web UI) on 0.0.0.0:7860.
|
| 5 |
+
|
| 6 |
+
The opencode binary itself is installed by the Dockerfile. This script only:
|
| 7 |
+
* locates the binary,
|
| 8 |
+
* launches `opencode serve --hostname 0.0.0.0 --port 7860`,
|
| 9 |
+
* forwards SIGTERM/SIGINT to it and mirrors its exit code.
|
| 10 |
+
|
| 11 |
+
Model credentials are read by opencode straight from the environment, so just
|
| 12 |
+
add them as Space secrets (e.g. ANTHROPIC_API_KEY, OPENAI_API_KEY, ...).
|
| 13 |
+
"""
|
| 14 |
+
import os
|
| 15 |
+
import shutil
|
| 16 |
+
import signal
|
| 17 |
+
import subprocess
|
| 18 |
+
import sys
|
| 19 |
+
|
| 20 |
+
HOST = os.environ.get("OPENCODE_HOST", "0.0.0.0")
|
| 21 |
+
PORT = str(os.environ.get("OPENCODE_PORT", os.environ.get("PORT", "7860")))
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def find_opencode() -> str:
|
| 25 |
+
found = shutil.which("opencode")
|
| 26 |
+
if found:
|
| 27 |
+
return found
|
| 28 |
+
for candidate in (
|
| 29 |
+
os.path.expanduser("~/.opencode/bin/opencode"),
|
| 30 |
+
"/root/.opencode/bin/opencode",
|
| 31 |
+
"/home/user/.opencode/bin/opencode",
|
| 32 |
+
):
|
| 33 |
+
if os.path.isfile(candidate) and os.access(candidate, os.X_OK):
|
| 34 |
+
return candidate
|
| 35 |
+
sys.exit("[space] FATAL: opencode binary not found in PATH or ~/.opencode/bin")
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def main() -> int:
|
| 39 |
+
binary = find_opencode()
|
| 40 |
+
cmd = [binary, "serve", "--hostname", HOST, "--port", PORT]
|
| 41 |
+
print(f"[space] starting: {' '.join(cmd)}", flush=True)
|
| 42 |
+
|
| 43 |
+
proc = subprocess.Popen(cmd)
|
| 44 |
+
|
| 45 |
+
def forward(signum, _frame):
|
| 46 |
+
try:
|
| 47 |
+
proc.send_signal(signum)
|
| 48 |
+
except ProcessLookupError:
|
| 49 |
+
pass
|
| 50 |
+
|
| 51 |
+
signal.signal(signal.SIGTERM, forward)
|
| 52 |
+
signal.signal(signal.SIGINT, forward)
|
| 53 |
+
|
| 54 |
+
return proc.wait()
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
raise SystemExit(main())
|