From f44545d8180efcac5d9e0f06f5e6dd44a25575b3 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Sun, 14 Jun 2026 15:35:33 +0800 Subject: [PATCH] Add API endpoints (status/submit/task) --- app/api/status/index.dspy | 31 ++++++++++++++++++++++++ app/api/submit/index.dspy | 50 +++++++++++++++++++++++++++++++++++++++ app/api/task/index.dspy | 17 +++++++++++++ app/health.dspy | 4 +++- conf/config.json | 43 ++++++++++++++++++++++++++++----- start.sh | 2 +- 6 files changed, 139 insertions(+), 8 deletions(-) create mode 100644 app/api/status/index.dspy create mode 100644 app/api/submit/index.dspy create mode 100644 app/api/task/index.dspy diff --git a/app/api/status/index.dspy b/app/api/status/index.dspy new file mode 100644 index 0000000..0b971b4 --- /dev/null +++ b/app/api/status/index.dspy @@ -0,0 +1,31 @@ +# -*- coding:utf-8 -*- +# GET /api/status - Demucs服务状态 + +import subprocess +import json + +result = { + 'service': 'demucs-vocal-separation', + 'model': 'htdemucs', + 'gpu_id': 5, + 'gpus': [] +} + +try: + out = subprocess.check_output( + ['nvidia-smi', '--query-gpu=index,utilization.gpu,memory.used,memory.total', + '--format=csv,noheader,nounits'], + timeout=5 + ).decode().strip() + for line in out.split('\n'): + parts = [p.strip() for p in line.split(',')] + result['gpus'].append({ + 'id': int(parts[0]), + 'util': int(parts[1]), + 'mem_used': int(parts[2]), + 'mem_total': int(parts[3]) + }) +except Exception: + pass + +return json.dumps(result) diff --git a/app/api/submit/index.dspy b/app/api/submit/index.dspy new file mode 100644 index 0000000..a5875df --- /dev/null +++ b/app/api/submit/index.dspy @@ -0,0 +1,50 @@ +# -*- coding:utf-8 -*- +# POST /api/submit - 提交Demucs人声分离任务 + +import json +import uuid +from ahserver.serverenv import ServerEnv + +method = request.method + +if method == 'POST': + audio_path = params_kw.get('audio_path', '') + if not audio_path: + return json.dumps({'error': 'audio_path is required'}, ensure_ascii=False) + + task_id = params_kw.get('task_id', str(uuid.uuid4()).replace("-", "")[:12]) + model = params_kw.get('model', 'htdemucs') + + payload = { + 'task_type': 'separate', + 'task_id': task_id, + 'audio_path': audio_path, + 'model': model + } + + env = ServerEnv() + longtasks = env.longtasks + if longtasks is None: + return json.dumps({'error': 'service not ready'}, ensure_ascii=False) + + result = await longtasks.submit_task(payload) + real_task_id = result.get('task_id', str(result)) if isinstance(result, dict) else str(result) + + return json.dumps({ + 'task_id': real_task_id, + 'status': 'queued', + 'audio_path': audio_path, + 'model': model, + 'message': 'task submitted', + 'check_url': f'/api/task?task_id={real_task_id}' + }, ensure_ascii=False) + +else: + return json.dumps({ + 'usage': 'POST with JSON body', + 'params': { + 'audio_path': 'string (required, server path to audio file)', + 'model': 'string (default htdemucs, options: htdemucs/htdemucs_ft/mdx_extra_q)', + 'task_id': 'string (optional, auto-generated)', + } + }, ensure_ascii=False) diff --git a/app/api/task/index.dspy b/app/api/task/index.dspy new file mode 100644 index 0000000..a1137df --- /dev/null +++ b/app/api/task/index.dspy @@ -0,0 +1,17 @@ +# -*- coding:utf-8 -*- +# GET /api/task?task_id=xxx - 查询任务状态 + +import json +from ahserver.serverenv import ServerEnv + +task_id = params_kw.get('task_id', '') +if not task_id: + return json.dumps({'error': 'task_id is required'}, ensure_ascii=False) + +env = ServerEnv() +longtasks = env.longtasks +if longtasks is None: + return json.dumps({'error': 'service not ready'}, ensure_ascii=False) + +status = await longtasks.get_status(task_id) +return json.dumps(status) diff --git a/app/health.dspy b/app/health.dspy index a6e7e39..b7028a1 100644 --- a/app/health.dspy +++ b/app/health.dspy @@ -1 +1,3 @@ -{"status":"ok","service":"demucs-service","model":"htdemucs"} +import json +result = {"status": "ok", "service": "$svc"} +print(json.dumps(result)) diff --git a/conf/config.json b/conf/config.json index ce0fb7c..6432e5f 100644 --- a/conf/config.json +++ b/conf/config.json @@ -1,7 +1,38 @@ { - "port": 9083, - "queue": "demucs", - "filesroot": "/tmp/demucs-outputs", - "host": "0.0.0.0", - "debug": false -} + "password_key": "DemucsService2026Key", + "databases": {}, + "session_redis": { + "host": "127.0.0.1", + "port": 6379, + "db": 1 + }, + "website": { + "paths": [ + [ + "$[workdir]$/app", + "" + ] + ], + "host": "0.0.0.0", + "port": 9083, + "coding": "utf-8", + "indexes": [ + "index.html", + "index.dspy" + ], + "processors": [ + [ + ".dspy", + "dspy" + ] + ], + "startswiths": [ + { + "leading": "/idfile", + "registerfunction": "idfile" + } + ] + }, + "hot_reload": false, + "filesroot": "/tmp/demucs-outputs" +} \ No newline at end of file diff --git a/start.sh b/start.sh index ae7a025..ee37541 100755 --- a/start.sh +++ b/start.sh @@ -3,5 +3,5 @@ cd /data/ymq/demucs-service export DEMUCS_GPU_ID=5 export CUDA_VISIBLE_DEVICES=5 export PYTHONPATH=/data/ymq/demucs-service -nohup /data/ymq/demucs_venv/bin/python ah.py > nohup.out 2>&1 & +nohup /data/ymq/wan22-service/py3/bin/python ah.py > nohup.out 2>&1 & echo "demucs-service started, PID: $!, GPU: $DEMUCS_GPU_ID"