feat: Add endpoint to retrieve TTS history for the current user and improve request model formatting
This commit is contained in:
@@ -11,16 +11,19 @@ from app.core.dependencies import get_current_active_user_flexible
|
|||||||
from app.models.user import User
|
from app.models.user import User
|
||||||
from app.services.tts import TTSService
|
from app.services.tts import TTSService
|
||||||
|
|
||||||
|
|
||||||
router = APIRouter(prefix="/tts", tags=["tts"])
|
router = APIRouter(prefix="/tts", tags=["tts"])
|
||||||
|
|
||||||
|
|
||||||
class TTSGenerateRequest(BaseModel):
|
class TTSGenerateRequest(BaseModel):
|
||||||
"""TTS generation request model."""
|
"""TTS generation request model."""
|
||||||
|
|
||||||
text: str = Field(..., min_length=1, max_length=1000, description="Text to convert to speech")
|
text: str = Field(
|
||||||
|
..., min_length=1, max_length=1000, description="Text to convert to speech"
|
||||||
|
)
|
||||||
provider: str = Field(default="gtts", description="TTS provider to use")
|
provider: str = Field(default="gtts", description="TTS provider to use")
|
||||||
options: dict[str, Any] = Field(default_factory=dict, description="Provider-specific options")
|
options: dict[str, Any] = Field(
|
||||||
|
default_factory=dict, description="Provider-specific options"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class TTSResponse(BaseModel):
|
class TTSResponse(BaseModel):
|
||||||
@@ -51,6 +54,47 @@ async def get_tts_service(
|
|||||||
return TTSService(session)
|
return TTSService(session)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("")
|
||||||
|
async def get_tts_list(
|
||||||
|
current_user: Annotated[User, Depends(get_current_active_user_flexible)],
|
||||||
|
tts_service: Annotated[TTSService, Depends(get_tts_service)],
|
||||||
|
limit: int = 50,
|
||||||
|
offset: int = 0,
|
||||||
|
) -> list[TTSResponse]:
|
||||||
|
"""Get TTS list for the current user."""
|
||||||
|
try:
|
||||||
|
if current_user.id is None:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
|
detail="User ID not available",
|
||||||
|
)
|
||||||
|
|
||||||
|
tts_records = await tts_service.get_user_tts_history(
|
||||||
|
user_id=current_user.id,
|
||||||
|
limit=limit,
|
||||||
|
offset=offset,
|
||||||
|
)
|
||||||
|
|
||||||
|
return [
|
||||||
|
TTSResponse(
|
||||||
|
id=tts.id,
|
||||||
|
text=tts.text,
|
||||||
|
provider=tts.provider,
|
||||||
|
options=tts.options,
|
||||||
|
sound_id=tts.sound_id,
|
||||||
|
user_id=tts.user_id,
|
||||||
|
created_at=tts.created_at.isoformat(),
|
||||||
|
)
|
||||||
|
for tts in tts_records
|
||||||
|
]
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
detail=f"Failed to get TTS history: {e!s}",
|
||||||
|
) from e
|
||||||
|
|
||||||
|
|
||||||
@router.post("")
|
@router.post("")
|
||||||
async def generate_tts(
|
async def generate_tts(
|
||||||
request: TTSGenerateRequest,
|
request: TTSGenerateRequest,
|
||||||
@@ -69,7 +113,7 @@ async def generate_tts(
|
|||||||
text=request.text,
|
text=request.text,
|
||||||
user_id=current_user.id,
|
user_id=current_user.id,
|
||||||
provider=request.provider,
|
provider=request.provider,
|
||||||
**request.options
|
**request.options,
|
||||||
)
|
)
|
||||||
|
|
||||||
tts_record = result["tts"]
|
tts_record = result["tts"]
|
||||||
@@ -84,7 +128,7 @@ async def generate_tts(
|
|||||||
sound_id=tts_record.sound_id,
|
sound_id=tts_record.sound_id,
|
||||||
user_id=tts_record.user_id,
|
user_id=tts_record.user_id,
|
||||||
created_at=tts_record.created_at.isoformat(),
|
created_at=tts_record.created_at.isoformat(),
|
||||||
)
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
@@ -140,47 +184,6 @@ async def get_provider(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get("")
|
|
||||||
async def get_tts_list(
|
|
||||||
current_user: Annotated[User, Depends(get_current_active_user_flexible)],
|
|
||||||
tts_service: Annotated[TTSService, Depends(get_tts_service)],
|
|
||||||
limit: int = 50,
|
|
||||||
offset: int = 0,
|
|
||||||
) -> list[TTSResponse]:
|
|
||||||
"""Get TTS generation history for the current user."""
|
|
||||||
try:
|
|
||||||
if current_user.id is None:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
||||||
detail="User ID not available",
|
|
||||||
)
|
|
||||||
|
|
||||||
tts_records = await tts_service.get_user_tts_history(
|
|
||||||
user_id=current_user.id,
|
|
||||||
limit=limit,
|
|
||||||
offset=offset,
|
|
||||||
)
|
|
||||||
|
|
||||||
return [
|
|
||||||
TTSResponse(
|
|
||||||
id=tts.id,
|
|
||||||
text=tts.text,
|
|
||||||
provider=tts.provider,
|
|
||||||
options=tts.options,
|
|
||||||
sound_id=tts.sound_id,
|
|
||||||
user_id=tts.user_id,
|
|
||||||
created_at=tts.created_at.isoformat(),
|
|
||||||
)
|
|
||||||
for tts in tts_records
|
|
||||||
]
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
||||||
detail=f"Failed to get TTS history: {e!s}",
|
|
||||||
) from e
|
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{tts_id}")
|
@router.delete("/{tts_id}")
|
||||||
async def delete_tts(
|
async def delete_tts(
|
||||||
tts_id: int,
|
tts_id: int,
|
||||||
|
|||||||
Reference in New Issue
Block a user