Add API endpoints (status/submit/task)
This commit is contained in:
parent
e18aac6595
commit
5f8bb8afda
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 - ASR服务状态
|
||||
|
||||
import subprocess
|
||||
import json
|
||||
|
||||
result = {
|
||||
'service': 'asr-transcription',
|
||||
'model': 'faster-whisper-large-v3-turbo-ct2',
|
||||
'gpu_id': 6,
|
||||
'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)
|
||||
53
app/api/submit/index.dspy
Normal file
53
app/api/submit/index.dspy
Normal file
@ -0,0 +1,53 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
# POST /api/submit - 提交ASR转录任务
|
||||
|
||||
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])
|
||||
language = params_kw.get('language', 'auto')
|
||||
beam_size = params_kw.get('beam_size', 5)
|
||||
|
||||
payload = {
|
||||
'task_type': 'transcribe',
|
||||
'task_id': task_id,
|
||||
'audio_path': audio_path,
|
||||
'language': language,
|
||||
'beam_size': int(beam_size)
|
||||
}
|
||||
|
||||
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,
|
||||
'language': language,
|
||||
'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)',
|
||||
'language': 'string (default auto, or zh/en/ja/ko etc)',
|
||||
'beam_size': 'int (default 5)',
|
||||
'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,5 +1,3 @@
|
||||
{{
|
||||
"status": "ok",
|
||||
"service": "asr-service",
|
||||
"model": "faster-whisper-large-v3-turbo-ct2"
|
||||
}}
|
||||
import json
|
||||
result = {"status": "ok", "service": "$svc"}
|
||||
print(json.dumps(result))
|
||||
|
||||
6
start.sh
6
start.sh
@ -1,7 +1,7 @@
|
||||
#!/bin/bash
|
||||
cd /data/ymq/asr-service
|
||||
export ASR_GPU_ID=5
|
||||
export CUDA_VISIBLE_DEVICES=5
|
||||
export ASR_GPU_ID=6
|
||||
export CUDA_VISIBLE_DEVICES=6
|
||||
export PYTHONPATH=/data/ymq/asr-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 "asr-service started, PID: $!, GPU: $ASR_GPU_ID"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user