OneAlign / app_zerogpu.py
loveaa's picture
Deploy Home Pulse: grounded setup, isolated workspaces and shared HF visual inference
77aeef0 verified
Raw History Blame Contribute Delete
6.69 kB
"""Home Pulse's React UI + Node contracts + Gradio/ZeroGPU visual execution."""
import spaces # Must precede torch and model imports for ZeroGPU CUDA emulation.
import atexit
import json
import os
from pathlib import Path
import re
import secrets
import shutil
import subprocess
import threading
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import gradio as gr
import httpx
from fastapi import Request
from fastapi.responses import HTMLResponse, JSONResponse, StreamingResponse
from fastapi.staticfiles import StaticFiles
from starlette.background import BackgroundTask
from model_runtime import load_model, predict
ROOT=Path(__file__).resolve().parent
DATA=Path(os.getenv('HOME_PULSE_DATA_DIR','/tmp/home-pulse'))
INTERNAL_KEY=secrets.token_hex(32)
NODE_PORT=int(os.getenv('HOME_PULSE_API_PORT','8791'))
PORT=int(os.getenv('PORT','7860'))
WORKSPACE=re.compile(r'^[a-f0-9]{64}$')
VISUAL=re.compile(r'^/api/(?:setup/trial|analyze|contracts/observe)$')
os.environ.setdefault('TOKENIZERS_PARALLELISM','false')
os.environ.setdefault('HF_HUB_DISABLE_IMPLICIT_TOKEN','1')
os.environ.setdefault('GRADIO_ANALYTICS_ENABLED','False')
os.environ.update(HOME_PULSE_DATA_DIR=str(DATA), HOME_PULSE_CLOUD='1', HOME_PULSE_INTERNAL_KEY=INTERNAL_KEY,
HOME_PULSE_API_PORT=str(NODE_PORT),HOME_PULSE_MEDIA_ROOT=str(ROOT/'media-library'),HOME_PULSE_MODEL_URL=f'http://127.0.0.1:{PORT}/_model')
node=shutil.which('node')
if not node: raise RuntimeError('Node.js is unavailable')
node_process=subprocess.Popen([node,str(ROOT/'web/server-bundle.mjs')],cwd=ROOT/'web')
atexit.register(node_process.terminate)
backend,calibration=load_model()
app=gr.Server(title='Home Pulse',docs_url=None,redoc_url=None)
class ModelBridge(BaseHTTPRequestHandler):
def do_POST(self):
if self.path!='/v1/systemone' or self.headers.get('X-Bridge-Key')!=INTERNAL_KEY:
self.send_error(403);return
try:
length=int(self.headers.get('Content-Length','0'))
if not 0<length<=6*1024*1024: raise ValueError('Invalid request size')
value=predict(backend,calibration,json.loads(self.rfile.read(length)))
body=json.dumps(value).encode();self.send_response(200)
except Exception as exc:
print('Visual request failed:',type(exc).__name__,str(exc)[:200],flush=True)
body=json.dumps({'error':'视觉判断暂时未完成,请重试'}).encode();self.send_response(422)
self.send_header('Content-Type','application/json');self.send_header('Content-Length',str(len(body)));self.end_headers();self.wfile.write(body)
def log_message(self,*args): pass
def gpu_duration(route,body,workspace):
return 40 if route=='/api/setup/trial' else 12
@app.api(name='visual',concurrency_limit=1,api_visibility='undocumented')
@spaces.GPU(duration=gpu_duration)
def visual(route:str,body:str,workspace:str)->dict:
if not VISUAL.fullmatch(route) or not WORKSPACE.fullmatch(workspace):
return {'status':400,'body':{'error':'观察请求无效'}}
if len(body.encode())>6*1024*1024:return {'status':413,'body':{'error':'画面过大'}}
# This bridge lives inside the allocated GPU worker. The Node rule engine calls
# it synchronously while it checks the trial or advances a monitoring contract.
bridge=ThreadingHTTPServer(('127.0.0.1',0),ModelBridge)
thread=threading.Thread(target=bridge.serve_forever,daemon=True);thread.start()
try:
with httpx.Client(trust_env=False,timeout=115) as client:
response=client.post(f'http://127.0.0.1:{NODE_PORT}'+route,content=body,headers={
'Content-Type':'application/json','X-Home-Pulse-Internal':INTERNAL_KEY,
'X-Home-Pulse-Workspace':workspace,'X-Home-Pulse-Vision-Port':str(bridge.server_port)})
return {'status':response.status_code,'body':response.json()}
finally:
bridge.shutdown();bridge.server_close()
@app.get('/_model/v1/models')
def models():
return {'data':[{'id':backend.model}],'runtime':{'model':backend.model,'gpu':'HF ZeroGPU · 按需分配','memoryGiB':None,'quantization':'BF16','maxImageSide':640}}
@app.api_route('/api/{path:path}',methods=['GET','POST'])
async def api_proxy(path:str,request:Request):
workspace=request.headers.get('x-home-pulse-workspace') or request.cookies.get('hp_workspace','')
if not WORKSPACE.fullmatch(workspace):return JSONResponse({'error':'请刷新页面,重新连接你的空间'},status_code=401)
if request.method=='POST' and VISUAL.fullmatch('/api/'+path):
return JSONResponse({'error':'请通过页面发起 GPU 观察'},status_code=400)
headers={k:v for k,v in request.headers.items() if k.lower() in ('content-type','content-length','range','x-file-name','origin','host')}
headers.update({'X-Home-Pulse-Internal':INTERNAL_KEY,'X-Home-Pulse-Workspace':workspace})
client=httpx.AsyncClient(trust_env=False,timeout=220)
try:
target=f'http://127.0.0.1:{NODE_PORT}/api/'+path+('?' + request.url.query if request.url.query else '')
outbound=client.build_request(request.method,target,headers=headers,content=request.stream())
response=await client.send(outbound,stream=True)
except Exception:
await client.aclose();return JSONResponse({'error':'空间连接暂时中断,请重试'},status_code=502)
async def close(): await response.aclose();await client.aclose()
kept={k:v for k,v in response.headers.items() if k.lower() in ('content-type','content-length','content-range','accept-ranges','cache-control','content-disposition')}
return StreamingResponse(response.aiter_raw(),status_code=response.status_code,headers=kept,background=BackgroundTask(close))
app.mount('/assets',StaticFiles(directory=ROOT/'web/dist/assets'),name='assets')
app.mount('/media',StaticFiles(directory=ROOT/'web/dist/media'),name='media')
html=(ROOT/'web/dist/index.html').read_text(encoding='utf-8').replace('<head>','<head><script>window.__HOME_PULSE_CLOUD__=true</script>')
async def page():return HTMLResponse(html,headers={'Cache-Control':'no-cache'})
for route in ('/','/home','/watches','/create','/memories','/events','/review','/insights','/privacy','/watch/{item_id}','/observe/{item_id}'):
app.add_api_route(route,page,methods=['GET'],include_in_schema=False)
@app.get('/favicon.svg')
def favicon():
from fastapi.responses import FileResponse
return FileResponse(ROOT/'web/dist/favicon.svg')
if __name__=='__main__':
app.launch(server_name='0.0.0.0',server_port=PORT,show_error=False)