Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
162 lines (133 loc) · 5.51 KB
/
Copy pathserver.py
File metadata and controls
162 lines (133 loc) · 5.51 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""
Somada Backend
Serves health data and proxies Vertex AI for the AI chat.
Usage: python server.py
"""
from http.server import HTTPServer, SimpleHTTPRequestHandler
from socketserver import ThreadingMixIn
import json
import os
import subprocess
import sys
def load_env(path=".env"):
"""Load KEY=value pairs from a .env file into os.environ."""
if not os.path.exists(path):
return
with open(path) as f:
for line in f:
line = line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, _, value = line.partition("=")
os.environ.setdefault(key.strip(), value.strip())
load_env()
from api.chat_core import chat_with_vertex, get_vertex_model, load_health_data # noqa: E402
HEALTH_DATA_FILE = "health_data.json"
def resolve_chat_api_key(body):
mode = body.get("chatMode", "server")
if mode == "byok":
return (body.get("vertexApiKey") or "").strip(), "byok"
return os.environ.get("VERTEX_API_KEY", "").strip(), "server"
class Handler(SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
self.health_data = load_health_data(local_path=HEALTH_DATA_FILE)
super().__init__(*args, directory=os.getcwd(), **kwargs)
def log_message(self, format, *args):
pass
def do_OPTIONS(self):
self.send_response(200)
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()
def do_GET(self):
if self.path == "/api/health":
self.send_json(self.health_data if self.health_data else {"error": "No health data found. Run parse_health.py first."})
elif self.path == "/api/config":
self.send_json({
"chat": {
"serverVertex": bool(os.environ.get("VERTEX_API_KEY")),
"serverModel": get_vertex_model(),
"byokSupported": True,
}
})
elif self.path == "/" or self.path == "/index.html":
self.path = "/index.html"
super().do_GET()
else:
super().do_GET()
def do_POST(self):
if self.path == "/api/reparse":
if not os.path.exists("export.xml"):
self.send_json({"ok": False, "error": "export.xml not found. Upload it first."})
return
result = subprocess.run(
[sys.executable, "parse_health.py", "export.xml"],
capture_output=True, text=True
)
if result.returncode == 0:
self.health_data = load_health_data(local_path=HEALTH_DATA_FILE)
self.send_json({"ok": True, "output": result.stdout})
else:
self.send_json({"ok": False, "error": result.stderr or result.stdout})
elif self.path == "/api/upload":
length = int(self.headers.get("Content-Length", 0))
if length == 0:
self.send_json({"ok": False, "error": "Empty file"})
return
data = self.rfile.read(length)
with open("export.xml", "wb") as f:
f.write(data)
self.send_json({"ok": True, "size": length})
elif self.path == "/api/chat":
length = int(self.headers.get("Content-Length", 0))
body = json.loads(self.rfile.read(length))
message = body.get("message", "")
history = body.get("history", [])
events = body.get("events", None)
api_key, mode = resolve_chat_api_key(body)
model = get_vertex_model()
if not self.health_data:
self.send_json({"reply": "No health data loaded. Please run parse_health.py first."})
return
health_data = self.health_data
if events is not None:
health_data = dict(self.health_data)
health_data["events"] = events
if mode == "byok" and not api_key:
reply = "Add your Vertex AI API key in Settings before using your own key."
else:
reply = chat_with_vertex(message, history, health_data, api_key, model)
self.send_json({"reply": reply, "mode": mode, "model": model})
else:
self.send_response(404)
self.end_headers()
def send_json(self, data):
response = json.dumps(data).encode()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", len(response))
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
self.wfile.write(response)
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle each request in a separate thread so long-running reparsing doesn't block the UI."""
daemon_threads = True
if __name__ == "__main__":
port = 8080
vertex_status = "configured" if os.environ.get("VERTEX_API_KEY") else "NOT SET"
vertex_model = get_vertex_model()
print(f"""
Somada Server
=======================
Starting on http://localhost:{port}
API Keys:
VERTEX_API_KEY : {vertex_status}
VERTEX_MODEL : {vertex_model}
Make sure you have run parse_health.py to generate health_data.json
""")
server = ThreadedHTTPServer(("", port), Handler)
try:
server.serve_forever()
except KeyboardInterrupt:
print("\nServer stopped.")