Implement role detection service with pattern matching
- Created RoleDetector class with colon and bracket pattern extraction - Implemented role deduplication logic - Added fallback to default 'Narrator' role when no patterns found - Created /api/tts/roles endpoint using RoleDetector - Added comprehensive test suite for role detection - All tests passing (7/7) Requirements: 2.1, 2.2, 2.3, 2.4, 2.5, 2.6
This commit is contained in:
@@ -9,6 +9,9 @@ from fastapi.responses import JSONResponse
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
# Import services
|
||||
from services.role_detector import RoleDetector
|
||||
|
||||
# Configure logging
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
@@ -91,9 +94,33 @@ async def general_exception_handler(request: Request, exc: Exception):
|
||||
}
|
||||
)
|
||||
|
||||
# Initialize services
|
||||
role_detector = RoleDetector()
|
||||
|
||||
# API Endpoints
|
||||
@app.get("/api/health")
|
||||
async def health_check():
|
||||
"""Health check endpoint."""
|
||||
logger.info("Health check requested")
|
||||
return {"status": "ok"}
|
||||
|
||||
@app.post("/api/tts/roles", response_model=RolesResponse)
|
||||
async def detect_roles(request: RoleDetectionRequest):
|
||||
"""
|
||||
Analyzes text and returns detected speaker roles.
|
||||
|
||||
Supports two pattern types:
|
||||
- Colon pattern: "RoleName:" at line start
|
||||
- Bracket pattern: "[RoleName]" preceding dialogue
|
||||
|
||||
Returns default "Narrator" role if no patterns found.
|
||||
"""
|
||||
logger.info(f"Role detection requested for text of length {len(request.text)}")
|
||||
|
||||
try:
|
||||
roles = role_detector.detect_roles(request.text)
|
||||
logger.info(f"Detected {len(roles)} roles: {[r['label'] for r in roles]}")
|
||||
return RolesResponse(roles=roles)
|
||||
except Exception as e:
|
||||
logger.error(f"Error during role detection: {str(e)}", exc_info=True)
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user