Spaces:
Paused
Paused
Delete audio_routes.py
Browse files- audio_routes.py +0 -263
audio_routes.py
DELETED
|
@@ -1,263 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
"""Audio API endpoints for Flare
|
| 3 |
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 4 |
-
Provides text-to-speech (TTS) and speech-to-text (STT) endpoints.
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
from fastapi import APIRouter, HTTPException, Response, Body
|
| 8 |
-
from pydantic import BaseModel
|
| 9 |
-
from typing import Optional
|
| 10 |
-
from datetime import datetime
|
| 11 |
-
import sys
|
| 12 |
-
|
| 13 |
-
from logger import log_info, log_error, log_warning, log_debug
|
| 14 |
-
from tts_factory import TTSFactory
|
| 15 |
-
from tts_preprocessor import TTSPreprocessor
|
| 16 |
-
from config_provider import ConfigProvider
|
| 17 |
-
|
| 18 |
-
router = APIRouter(tags=["audio"])
|
| 19 |
-
|
| 20 |
-
# ===================== Models =====================
|
| 21 |
-
class TTSRequest(BaseModel):
|
| 22 |
-
text: str
|
| 23 |
-
voice_id: Optional[str] = None
|
| 24 |
-
language: Optional[str] = "tr-TR"
|
| 25 |
-
|
| 26 |
-
class STTRequest(BaseModel):
|
| 27 |
-
audio_data: str # Base64 encoded audio
|
| 28 |
-
language: Optional[str] = "tr-TR"
|
| 29 |
-
format: Optional[str] = "webm" # webm, wav, mp3
|
| 30 |
-
|
| 31 |
-
# ===================== Helpers =====================
|
| 32 |
-
def log(message: str):
|
| 33 |
-
"""Log helper with timestamp"""
|
| 34 |
-
timestamp = datetime.now().strftime("%H:%M:%S.%f")[:-3]
|
| 35 |
-
print(f"[{timestamp}] {message}")
|
| 36 |
-
sys.stdout.flush()
|
| 37 |
-
|
| 38 |
-
# ===================== TTS Endpoints =====================
|
| 39 |
-
@router.post("/tts/generate")
|
| 40 |
-
async def generate_tts(request: TTSRequest):
|
| 41 |
-
"""Generate TTS audio from text - public endpoint for chat"""
|
| 42 |
-
try:
|
| 43 |
-
# Create TTS provider
|
| 44 |
-
tts_provider = TTSFactory.create_provider()
|
| 45 |
-
|
| 46 |
-
if not tts_provider:
|
| 47 |
-
# Return empty response for no TTS
|
| 48 |
-
log_info("π΅ TTS disabled - returning empty response")
|
| 49 |
-
return Response(
|
| 50 |
-
content=b"",
|
| 51 |
-
media_type="audio/mpeg",
|
| 52 |
-
headers={"X-TTS-Status": "disabled"}
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
log_info(f"π€ TTS request: '{request.text[:50]}...' with provider: {tts_provider.get_provider_name()}")
|
| 56 |
-
|
| 57 |
-
# Preprocess text if needed
|
| 58 |
-
preprocessor = TTSPreprocessor(language=request.language)
|
| 59 |
-
processed_text = preprocessor.preprocess(
|
| 60 |
-
request.text,
|
| 61 |
-
tts_provider.get_preprocessing_flags()
|
| 62 |
-
)
|
| 63 |
-
|
| 64 |
-
log_debug(f"π Preprocessed text: {processed_text[:100]}...")
|
| 65 |
-
|
| 66 |
-
# Generate audio
|
| 67 |
-
audio_data = await tts_provider.synthesize(
|
| 68 |
-
text=processed_text,
|
| 69 |
-
voice_id=request.voice_id
|
| 70 |
-
)
|
| 71 |
-
|
| 72 |
-
log_info(f"β
TTS generated {len(audio_data)} bytes of audio")
|
| 73 |
-
|
| 74 |
-
# Return audio as binary response
|
| 75 |
-
return Response(
|
| 76 |
-
content=audio_data,
|
| 77 |
-
media_type="audio/mpeg",
|
| 78 |
-
headers={
|
| 79 |
-
"Content-Disposition": 'inline; filename="tts_output.mp3"',
|
| 80 |
-
"X-TTS-Provider": tts_provider.get_provider_name(),
|
| 81 |
-
"X-TTS-Language": request.language,
|
| 82 |
-
"Cache-Control": "no-cache"
|
| 83 |
-
}
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
except Exception as e:
|
| 87 |
-
log_error("β TTS generation error", e)
|
| 88 |
-
raise HTTPException(
|
| 89 |
-
status_code=500,
|
| 90 |
-
detail=f"TTS generation failed: {str(e)}"
|
| 91 |
-
)
|
| 92 |
-
|
| 93 |
-
@router.get("/tts/voices")
|
| 94 |
-
async def get_tts_voices():
|
| 95 |
-
"""Get available TTS voices - public endpoint"""
|
| 96 |
-
try:
|
| 97 |
-
tts_provider = TTSFactory.create_provider()
|
| 98 |
-
|
| 99 |
-
if not tts_provider:
|
| 100 |
-
return {
|
| 101 |
-
"voices": [],
|
| 102 |
-
"provider": "none",
|
| 103 |
-
"enabled": False
|
| 104 |
-
}
|
| 105 |
-
|
| 106 |
-
voices = tts_provider.get_supported_voices()
|
| 107 |
-
|
| 108 |
-
# Convert dict to list format
|
| 109 |
-
voice_list = [
|
| 110 |
-
{"id": voice_id, "name": voice_name}
|
| 111 |
-
for voice_id, voice_name in voices.items()
|
| 112 |
-
]
|
| 113 |
-
|
| 114 |
-
return {
|
| 115 |
-
"voices": voice_list,
|
| 116 |
-
"provider": tts_provider.get_provider_name(),
|
| 117 |
-
"enabled": True
|
| 118 |
-
}
|
| 119 |
-
|
| 120 |
-
except Exception as e:
|
| 121 |
-
log_error("β Error getting TTS voices", e)
|
| 122 |
-
return {
|
| 123 |
-
"voices": [],
|
| 124 |
-
"provider": "error",
|
| 125 |
-
"enabled": False,
|
| 126 |
-
"error": str(e)
|
| 127 |
-
}
|
| 128 |
-
|
| 129 |
-
@router.get("/tts/status")
|
| 130 |
-
async def get_tts_status():
|
| 131 |
-
"""Get TTS service status"""
|
| 132 |
-
cfg = ConfigProvider.get()
|
| 133 |
-
|
| 134 |
-
return {
|
| 135 |
-
"enabled": cfg.global_config.tts_provider.name != "no_tts",
|
| 136 |
-
"provider": cfg.global_config.tts_provider.name,
|
| 137 |
-
"provider_config": {
|
| 138 |
-
"name": cfg.global_config.tts_provider.name,
|
| 139 |
-
"has_api_key": bool(cfg.global_config.tts_provider.api_key),
|
| 140 |
-
"endpoint": cfg.global_config.tts_provider.endpoint
|
| 141 |
-
}
|
| 142 |
-
}
|
| 143 |
-
|
| 144 |
-
# ===================== STT Endpoints =====================
|
| 145 |
-
@router.post("/stt/transcribe")
|
| 146 |
-
async def transcribe_audio(request: STTRequest):
|
| 147 |
-
"""Transcribe audio to text"""
|
| 148 |
-
try:
|
| 149 |
-
from stt_factory import STTFactory
|
| 150 |
-
from stt_interface import STTConfig
|
| 151 |
-
import base64
|
| 152 |
-
|
| 153 |
-
# Create STT provider
|
| 154 |
-
stt_provider = STTFactory.create_provider()
|
| 155 |
-
|
| 156 |
-
if not stt_provider or not stt_provider.supports_realtime():
|
| 157 |
-
log_warning("π΅ STT disabled or doesn't support transcription")
|
| 158 |
-
raise HTTPException(
|
| 159 |
-
status_code=503,
|
| 160 |
-
detail="STT service not available"
|
| 161 |
-
)
|
| 162 |
-
|
| 163 |
-
# Get config
|
| 164 |
-
cfg = ConfigProvider.get()
|
| 165 |
-
stt_config = cfg.global_config.stt_provider.settings
|
| 166 |
-
|
| 167 |
-
# Decode audio data
|
| 168 |
-
audio_bytes = base64.b64decode(request.audio_data)
|
| 169 |
-
|
| 170 |
-
# Create STT config
|
| 171 |
-
config = STTConfig(
|
| 172 |
-
language=request.language or stt_config.get("language", "tr-TR"),
|
| 173 |
-
sample_rate=16000,
|
| 174 |
-
encoding=request.format.upper() if request.format else "WEBM_OPUS",
|
| 175 |
-
enable_punctuation=stt_config.get("enable_punctuation", True),
|
| 176 |
-
enable_word_timestamps=False,
|
| 177 |
-
model=stt_config.get("model", "latest_long"),
|
| 178 |
-
use_enhanced=stt_config.get("use_enhanced", True),
|
| 179 |
-
single_utterance=True,
|
| 180 |
-
interim_results=False
|
| 181 |
-
)
|
| 182 |
-
|
| 183 |
-
# Start streaming session
|
| 184 |
-
await stt_provider.start_streaming(config)
|
| 185 |
-
|
| 186 |
-
# Process audio
|
| 187 |
-
transcription = ""
|
| 188 |
-
confidence = 0.0
|
| 189 |
-
|
| 190 |
-
try:
|
| 191 |
-
async for result in stt_provider.stream_audio(audio_bytes):
|
| 192 |
-
if result.is_final:
|
| 193 |
-
transcription = result.text
|
| 194 |
-
confidence = result.confidence
|
| 195 |
-
break
|
| 196 |
-
finally:
|
| 197 |
-
# Stop streaming
|
| 198 |
-
await stt_provider.stop_streaming()
|
| 199 |
-
|
| 200 |
-
log_info(f"β
STT transcription completed: '{transcription[:50]}...'")
|
| 201 |
-
|
| 202 |
-
return {
|
| 203 |
-
"text": transcription,
|
| 204 |
-
"confidence": confidence,
|
| 205 |
-
"language": request.language,
|
| 206 |
-
"provider": stt_provider.get_provider_name()
|
| 207 |
-
}
|
| 208 |
-
|
| 209 |
-
except HTTPException:
|
| 210 |
-
raise
|
| 211 |
-
except Exception as e:
|
| 212 |
-
log_error("β STT transcription error", e)
|
| 213 |
-
raise HTTPException(
|
| 214 |
-
status_code=500,
|
| 215 |
-
detail=f"Transcription failed: {str(e)}"
|
| 216 |
-
)
|
| 217 |
-
|
| 218 |
-
@router.get("/stt/languages")
|
| 219 |
-
async def get_stt_languages():
|
| 220 |
-
"""Get supported STT languages"""
|
| 221 |
-
try:
|
| 222 |
-
from stt_factory import STTFactory
|
| 223 |
-
|
| 224 |
-
stt_provider = STTFactory.create_provider()
|
| 225 |
-
|
| 226 |
-
if not stt_provider:
|
| 227 |
-
return {
|
| 228 |
-
"languages": [],
|
| 229 |
-
"provider": "none",
|
| 230 |
-
"enabled": False
|
| 231 |
-
}
|
| 232 |
-
|
| 233 |
-
languages = stt_provider.get_supported_languages()
|
| 234 |
-
|
| 235 |
-
return {
|
| 236 |
-
"languages": languages,
|
| 237 |
-
"provider": stt_provider.get_provider_name(),
|
| 238 |
-
"enabled": True
|
| 239 |
-
}
|
| 240 |
-
|
| 241 |
-
except Exception as e:
|
| 242 |
-
log_error("β Error getting STT languages", e)
|
| 243 |
-
return {
|
| 244 |
-
"languages": [],
|
| 245 |
-
"provider": "error",
|
| 246 |
-
"enabled": False,
|
| 247 |
-
"error": str(e)
|
| 248 |
-
}
|
| 249 |
-
|
| 250 |
-
@router.get("/stt/status")
|
| 251 |
-
async def get_stt_status():
|
| 252 |
-
"""Get STT service status"""
|
| 253 |
-
cfg = ConfigProvider.get()
|
| 254 |
-
|
| 255 |
-
return {
|
| 256 |
-
"enabled": cfg.global_config.stt_provider.name != "no_stt",
|
| 257 |
-
"provider": cfg.global_config.stt_provider.name,
|
| 258 |
-
"provider_config": {
|
| 259 |
-
"name": cfg.global_config.stt_provider.name,
|
| 260 |
-
"has_api_key": bool(cfg.global_config.stt_provider.api_key),
|
| 261 |
-
"endpoint": cfg.global_config.stt_provider.endpoint
|
| 262 |
-
}
|
| 263 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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