Spaces:
Sleeping
Sleeping
Merge branch 'main' of https://github.com/trustdemons05/VoiceDifferentiator
Browse files- app/api/routes.py +64 -0
- app/api/schemas.py +23 -0
app/api/routes.py
CHANGED
|
@@ -14,6 +14,7 @@ from pydantic import ValidationError
|
|
| 14 |
|
| 15 |
from .schemas import (
|
| 16 |
DetectRequest,
|
|
|
|
| 17 |
DetectResponse,
|
| 18 |
HealthResponse,
|
| 19 |
ErrorResponse,
|
|
@@ -135,6 +136,69 @@ async def detect_voice(
|
|
| 135 |
pass
|
| 136 |
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
@router.get(
|
| 139 |
"/health",
|
| 140 |
response_model=HealthResponse,
|
|
|
|
| 14 |
|
| 15 |
from .schemas import (
|
| 16 |
DetectRequest,
|
| 17 |
+
ExternalTesterRequest,
|
| 18 |
DetectResponse,
|
| 19 |
HealthResponse,
|
| 20 |
ErrorResponse,
|
|
|
|
| 136 |
pass
|
| 137 |
|
| 138 |
|
| 139 |
+
@router.post(
|
| 140 |
+
"/detect-external",
|
| 141 |
+
response_model=DetectResponse,
|
| 142 |
+
summary="Detect AI-Generated Voice (External Tester Compatible)",
|
| 143 |
+
description="Alternative endpoint compatible with external testing tools"
|
| 144 |
+
)
|
| 145 |
+
async def detect_voice_external(
|
| 146 |
+
request: ExternalTesterRequest
|
| 147 |
+
) -> DetectResponse:
|
| 148 |
+
"""
|
| 149 |
+
Detect if the provided audio is AI-generated or human.
|
| 150 |
+
Compatible with external endpoint testers.
|
| 151 |
+
"""
|
| 152 |
+
temp_path = None
|
| 153 |
+
|
| 154 |
+
try:
|
| 155 |
+
# Convert external tester format to internal format
|
| 156 |
+
audio_bytes = base64.b64decode(request.Audio_Base64_Format)
|
| 157 |
+
|
| 158 |
+
if len(audio_bytes) > 10 * 1024 * 1024:
|
| 159 |
+
raise HTTPException(
|
| 160 |
+
status_code=status.HTTP_400_BAD_REQUEST,
|
| 161 |
+
detail="Audio file too large. Maximum size is 10MB."
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
if len(audio_bytes) < 1000:
|
| 165 |
+
raise HTTPException(
|
| 166 |
+
status_code=status.HTTP_400_BAD_REQUEST,
|
| 167 |
+
detail="Audio file too small. Minimum duration is 1 second."
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
# Write audio to temporary file
|
| 171 |
+
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as f:
|
| 172 |
+
f.write(audio_bytes)
|
| 173 |
+
temp_path = f.name
|
| 174 |
+
|
| 175 |
+
# Get language from request
|
| 176 |
+
language = request.Language.lower()
|
| 177 |
+
|
| 178 |
+
detector = get_detector(language=language)
|
| 179 |
+
|
| 180 |
+
result = detector.detect(temp_path)
|
| 181 |
+
|
| 182 |
+
result["language_detected"] = language
|
| 183 |
+
|
| 184 |
+
return DetectResponse(**result)
|
| 185 |
+
|
| 186 |
+
except HTTPException:
|
| 187 |
+
raise
|
| 188 |
+
except Exception as e:
|
| 189 |
+
raise HTTPException(
|
| 190 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 191 |
+
detail=f"Detection failed: {str(e)}"
|
| 192 |
+
)
|
| 193 |
+
finally:
|
| 194 |
+
# Cleanup temp file
|
| 195 |
+
if temp_path and os.path.exists(temp_path):
|
| 196 |
+
try:
|
| 197 |
+
os.unlink(temp_path)
|
| 198 |
+
except OSError:
|
| 199 |
+
pass
|
| 200 |
+
|
| 201 |
+
|
| 202 |
@router.get(
|
| 203 |
"/health",
|
| 204 |
response_model=HealthResponse,
|
app/api/schemas.py
CHANGED
|
@@ -60,6 +60,29 @@ class DetectRequest(BaseModel):
|
|
| 60 |
}
|
| 61 |
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
# ============== Response Schemas ==============
|
| 64 |
|
| 65 |
class TechnicalDetails(BaseModel):
|
|
|
|
| 60 |
}
|
| 61 |
|
| 62 |
|
| 63 |
+
class ExternalTesterRequest(BaseModel):
|
| 64 |
+
"""Request schema compatible with external endpoint tester"""
|
| 65 |
+
Language: str = Field(
|
| 66 |
+
...,
|
| 67 |
+
description="Language code (en, ta, hi, ml, te)",
|
| 68 |
+
examples=["en"]
|
| 69 |
+
)
|
| 70 |
+
Audio_Format: str = Field(
|
| 71 |
+
default="mp3",
|
| 72 |
+
description="Audio format (mp3, wav, etc.)",
|
| 73 |
+
alias="Audio Format"
|
| 74 |
+
)
|
| 75 |
+
Audio_Base64_Format: str = Field(
|
| 76 |
+
...,
|
| 77 |
+
description="Base64-encoded audio data",
|
| 78 |
+
alias="Audio Base64 Format"
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
class Config:
|
| 82 |
+
populate_by_name = True
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
# ============== Response Schemas ==============
|
| 87 |
|
| 88 |
class TechnicalDetails(BaseModel):
|