tchbcb commited on
Commit
2bbfd2a
·
verified ·
1 Parent(s): 12b2f89

200k ctx (rope_scaling hook) + openai-compatible api (key 1234) + bootstrap v2 (chat 7861 / api 7862) + model card

Browse files
Files changed (1) hide show
  1. control_server.py +81 -0
control_server.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """control_server.py — 远程控制通道 (port 5000). 重构自旧 colab_server 协议.
3
+
4
+ Endpoints:
5
+ POST /execute_stream {"code": "..."} -> SSE: {type: stdout|stderr|error|complete}
6
+ GET /health -> {"ok": true}
7
+
8
+ 注意: 这是无鉴权的任意代码执行端点, 仅用于开发调试, URL 为随机子域名不可枚举,
9
+ 用完建议关掉 Colab 会话.
10
+ """
11
+ import json
12
+ import queue
13
+ import sys
14
+ import threading
15
+ import traceback
16
+
17
+ from flask import Flask, Response, jsonify, request
18
+
19
+ app = Flask(__name__)
20
+
21
+
22
+ class _StreamToQ:
23
+ def __init__(self, q, tag):
24
+ self.q, self.tag = q, tag
25
+
26
+ def write(self, s):
27
+ if s:
28
+ self.q.put((self.tag, s))
29
+ return len(s)
30
+
31
+ def flush(self):
32
+ pass
33
+
34
+ def writelines(self, lines):
35
+ for s in lines:
36
+ self.write(s)
37
+
38
+ def isatty(self):
39
+ return False
40
+
41
+
42
+ @app.route("/health")
43
+ def health():
44
+ return jsonify({"ok": True, "service": "samai-control"})
45
+
46
+
47
+ @app.route("/execute_stream", methods=["POST"])
48
+ def execute_stream():
49
+ code = (request.get_json(force=True) or {}).get("code", "")
50
+ q = queue.Queue()
51
+
52
+ def worker():
53
+ g = {"__name__": "__main__"}
54
+ old = sys.stdout, sys.stderr
55
+ sys.stdout = _StreamToQ(q, "stdout")
56
+ sys.stderr = _StreamToQ(q, "stderr")
57
+ try:
58
+ exec(compile(code, "<remote>", "exec"), g) # noqa: S102
59
+ q.put(("complete", "ok"))
60
+ except BaseException: # noqa: BLE001
61
+ q.put(("error", traceback.format_exc()))
62
+ q.put(("complete", "error"))
63
+ finally:
64
+ sys.stdout, sys.stderr = old
65
+
66
+ threading.Thread(target=worker, daemon=True).start()
67
+
68
+ def gen():
69
+ while True:
70
+ t, c = q.get()
71
+ yield f"data: {json.dumps({'type': t, 'content': c})}\n\n"
72
+ if t == "complete":
73
+ break
74
+
75
+ return Response(gen(), mimetype="text/event-stream",
76
+ headers={"Cache-Control": "no-cache",
77
+ "X-Accel-Buffering": "no"})
78
+
79
+
80
+ if __name__ == "__main__":
81
+ app.run(host="0.0.0.0", port=5000, threaded=True)