Spaces:
Sleeping
Sleeping
Deploy commited on
Commit ·
8ae2e16
1
Parent(s): 7d1f8cf
Deploy proxy
Browse files
app.py
CHANGED
|
@@ -119,5 +119,35 @@ async def proxy(request: Request):
|
|
| 119 |
|
| 120 |
if __name__ == "__main__":
|
| 121 |
import uvicorn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
port = int(os.environ.get("PORT", 7860))
|
| 123 |
uvicorn.run(app, host="0.0.0.0", port=port)
|
|
|
|
| 119 |
|
| 120 |
if __name__ == "__main__":
|
| 121 |
import uvicorn
|
| 122 |
+
import threading
|
| 123 |
+
import time
|
| 124 |
+
|
| 125 |
+
# 启动时自动注册
|
| 126 |
+
def auto_register():
|
| 127 |
+
time.sleep(10) # 等待服务启动
|
| 128 |
+
try:
|
| 129 |
+
import httpx
|
| 130 |
+
# 从 HF 环境变量获取 Space 信息
|
| 131 |
+
space_id = os.environ.get("SPACE_ID", "") # pyrq/ws-test-xxx
|
| 132 |
+
if space_id:
|
| 133 |
+
space_url = f"https://{space_id.replace('/', '-')}.hf.space"
|
| 134 |
+
|
| 135 |
+
# 注册到 Worker(新格式)
|
| 136 |
+
with httpx.Client(timeout=10.0) as client:
|
| 137 |
+
client.post(
|
| 138 |
+
"https://proxy-worker.busitest135.workers.dev/api/instances",
|
| 139 |
+
json={
|
| 140 |
+
"nodeId": space_id,
|
| 141 |
+
"url": space_url,
|
| 142 |
+
"platform": "hf",
|
| 143 |
+
"tags": ["auto"]
|
| 144 |
+
}
|
| 145 |
+
)
|
| 146 |
+
log_message(f"[AUTO_REGISTER] Registered {space_id} -> {space_url}")
|
| 147 |
+
except Exception as e:
|
| 148 |
+
log_message(f"[AUTO_REGISTER] Failed: {e}")
|
| 149 |
+
|
| 150 |
+
threading.Thread(target=auto_register, daemon=True).start()
|
| 151 |
+
|
| 152 |
port = int(os.environ.get("PORT", 7860))
|
| 153 |
uvicorn.run(app, host="0.0.0.0", port=port)
|