Add API endpoints (status/submit/task)
This commit is contained in:
parent
924b27be11
commit
f44545d818
31
app/api/status/index.dspy
Normal file
31
app/api/status/index.dspy
Normal file
@ -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)
|
||||||
50
app/api/submit/index.dspy
Normal file
50
app/api/submit/index.dspy
Normal file
@ -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)
|
||||||
17
app/api/task/index.dspy
Normal file
17
app/api/task/index.dspy
Normal file
@ -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)
|
||||||
@ -1 +1,3 @@
|
|||||||
{"status":"ok","service":"demucs-service","model":"htdemucs"}
|
import json
|
||||||
|
result = {"status": "ok", "service": "$svc"}
|
||||||
|
print(json.dumps(result))
|
||||||
|
|||||||
@ -1,7 +1,38 @@
|
|||||||
{
|
{
|
||||||
"port": 9083,
|
"password_key": "DemucsService2026Key",
|
||||||
"queue": "demucs",
|
"databases": {},
|
||||||
"filesroot": "/tmp/demucs-outputs",
|
"session_redis": {
|
||||||
|
"host": "127.0.0.1",
|
||||||
|
"port": 6379,
|
||||||
|
"db": 1
|
||||||
|
},
|
||||||
|
"website": {
|
||||||
|
"paths": [
|
||||||
|
[
|
||||||
|
"$[workdir]$/app",
|
||||||
|
""
|
||||||
|
]
|
||||||
|
],
|
||||||
"host": "0.0.0.0",
|
"host": "0.0.0.0",
|
||||||
"debug": false
|
"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"
|
||||||
}
|
}
|
||||||
2
start.sh
2
start.sh
@ -3,5 +3,5 @@ cd /data/ymq/demucs-service
|
|||||||
export DEMUCS_GPU_ID=5
|
export DEMUCS_GPU_ID=5
|
||||||
export CUDA_VISIBLE_DEVICES=5
|
export CUDA_VISIBLE_DEVICES=5
|
||||||
export PYTHONPATH=/data/ymq/demucs-service
|
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"
|
echo "demucs-service started, PID: $!, GPU: $DEMUCS_GPU_ID"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user