cloudunity commited on
Commit
905b22d
·
verified ·
1 Parent(s): e172dfc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+ import sys
4
+
5
+ PORT = int(os.environ.get("PORT", 7860))
6
+ WORKSPACE = "/data/workspace"
7
+ CLAUDE_HOME = "/data/claude-config"
8
+ INDEX_HTML = "/app/www/index.html"
9
+
10
+ os.makedirs(WORKSPACE, exist_ok=True)
11
+ os.makedirs(CLAUDE_HOME, exist_ok=True)
12
+
13
+ env = os.environ.copy()
14
+ env["HOME"] = CLAUDE_HOME
15
+
16
+ # --- Custom API endpoint support ---
17
+ # Two modes:
18
+ #
19
+ # 1. Direct Anthropic-compatible server:
20
+ # Set ANTHROPIC_BASE_URL to a server that already implements the
21
+ # Anthropic /v1/messages schema. Nothing else needed.
22
+ #
23
+ # 2. OpenAI-compatible server (e.g. your LiteRT-LM Flask server):
24
+ # Set OPENAI_BASE_URL (and OPENAI_API_KEY / OPENAI_MODEL as needed).
25
+ # This launches proxy.py locally, which translates Claude Code's
26
+ # Anthropic-format requests into OpenAI-format calls against your
27
+ # server, then translates responses back. Claude Code is pointed at
28
+ # the local proxy automatically.
29
+ custom_base_url = os.environ.get("ANTHROPIC_BASE_URL")
30
+ openai_base_url = os.environ.get("OPENAI_BASE_URL")
31
+
32
+ proxy_proc = None
33
+ if openai_base_url and not custom_base_url:
34
+ proxy_port = os.environ.get("PROXY_PORT", "8317")
35
+ print(f"Starting OpenAI->Anthropic proxy for backend: {openai_base_url}", flush=True)
36
+ proxy_env = env.copy()
37
+ proxy_env["PROXY_PORT"] = proxy_port
38
+ proxy_proc = subprocess.Popen(
39
+ [sys.executable, "/app/proxy.py"],
40
+ env=proxy_env,
41
+ )
42
+ env["ANTHROPIC_BASE_URL"] = f"http://localhost:{proxy_port}"
43
+ print(f"Claude Code will use proxy at http://localhost:{proxy_port}", flush=True)
44
+ elif custom_base_url:
45
+ env["ANTHROPIC_BASE_URL"] = custom_base_url
46
+ print(f"Using custom Anthropic-compatible endpoint: {custom_base_url}", flush=True)
47
+ else:
48
+ print("No custom endpoint set, using default api.anthropic.com", flush=True)
49
+
50
+ ttyd_cmd = [
51
+ "ttyd",
52
+ "-p", str(PORT),
53
+ "-W",
54
+ "-I", INDEX_HTML,
55
+ "-t", "fontSize=13",
56
+ "-t", "theme={\"background\":\"#1e1e1e\"}",
57
+ "-t", "disableLeaveAlert=true",
58
+ "bash", "-c", f"cd {WORKSPACE} && claude",
59
+ ]
60
+
61
+ print(f"Starting ttyd on port {PORT}...", flush=True)
62
+
63
+ proc = subprocess.Popen(ttyd_cmd, env=env)
64
+
65
+ try:
66
+ proc.wait()
67
+ except KeyboardInterrupt:
68
+ proc.terminate()
69
+ finally:
70
+ if proxy_proc:
71
+ proxy_proc.terminate()
72
+ sys.exit(0)