Build lightweight AI agent admin
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
"""
|
||||
语音识别 API
|
||||
"""
|
||||
from fastapi import APIRouter, Depends, File, HTTPException, Query, UploadFile
|
||||
from fastapi.responses import Response
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.database import get_db
|
||||
|
||||
router = APIRouter(prefix="/speech", tags=["AI-语音"])
|
||||
|
||||
|
||||
@router.post("/transcribe", summary="语音转文字")
|
||||
async def transcribe(
|
||||
audio: UploadFile = File(..., description="音频文件"),
|
||||
language: str = Query("zh", description="语言"),
|
||||
provider: str = Query("dashscope", description="提供商"),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
"""
|
||||
语音转文字(ASR)
|
||||
|
||||
将音频文件转换为文字,支持:
|
||||
- dashscope: 阿里云百炼(默认,推荐)
|
||||
- openai: OpenAI Whisper
|
||||
|
||||
支持的音频格式:wav, mp3, webm, pcm, opus
|
||||
"""
|
||||
from ai_platform.services.speech_service import SpeechService
|
||||
|
||||
service = SpeechService(db=db)
|
||||
await service._resolve_dashscope_api_key()
|
||||
audio_content = await audio.read()
|
||||
result = service.transcribe(
|
||||
audio_file=audio_content,
|
||||
language=language,
|
||||
provider=provider,
|
||||
)
|
||||
|
||||
if result["success"]:
|
||||
return {
|
||||
"text": result["text"],
|
||||
"duration": result.get("duration", 0),
|
||||
}
|
||||
else:
|
||||
raise HTTPException(status_code=400, detail=result["error"])
|
||||
|
||||
|
||||
@router.post("/tts", summary="文字转语音")
|
||||
async def text_to_speech(
|
||||
text: str = Query(..., description="要转换的文字"),
|
||||
voice: str = Query("sambert-zhichu-v1", description="声音"),
|
||||
provider: str = Query("dashscope", description="提供商"),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
"""
|
||||
文字转语音(TTS)
|
||||
|
||||
将文字转换为语音,返回音频文件
|
||||
|
||||
DashScope 可用声音:
|
||||
- sambert-zhichu-v1: 知厨(男声)
|
||||
- sambert-zhimiao-emo-v1: 知妙(女声,带情感)
|
||||
- sambert-zhiying-v1: 知莺(女声)
|
||||
|
||||
OpenAI 可用声音:
|
||||
- alloy, echo, fable, onyx, nova, shimmer
|
||||
"""
|
||||
from ai_platform.services.speech_service import SpeechService
|
||||
|
||||
service = SpeechService(db=db)
|
||||
await service._resolve_dashscope_api_key()
|
||||
result = service.text_to_speech(
|
||||
text=text,
|
||||
voice=voice,
|
||||
provider=provider,
|
||||
)
|
||||
|
||||
if result["success"]:
|
||||
return Response(
|
||||
content=result["audio_data"],
|
||||
media_type=result["content_type"],
|
||||
headers={"Content-Disposition": 'attachment; filename="speech.wav"'},
|
||||
)
|
||||
else:
|
||||
raise HTTPException(status_code=400, detail=result["error"])
|
||||
Reference in New Issue
Block a user