Afrinetwork7
commited on
Commit
•
be921fa
1
Parent(s):
5d16050
Update app.py
Browse files
app.py
CHANGED
@@ -92,8 +92,16 @@ async def transcribe_audio(request: AudioRequest):
|
|
92 |
|
93 |
@app.post("/synthesize")
|
94 |
async def synthesize_speech(request: TTSRequest):
|
|
|
95 |
try:
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
logger.info("Calling synthesize function")
|
99 |
result, filtered_text = synthesize(request.text, request.language, request.speed)
|
@@ -104,7 +112,7 @@ async def synthesize_speech(request: TTSRequest):
|
|
104 |
raise ValueError("Synthesis failed to produce audio")
|
105 |
|
106 |
sample_rate, audio = result
|
107 |
-
logger.info(f"Synthesis result: sample_rate={sample_rate}, audio_shape={audio.shape}, audio_dtype={audio.dtype}")
|
108 |
|
109 |
logger.info("Converting audio to numpy array")
|
110 |
audio = np.array(audio, dtype=np.float32)
|
@@ -114,7 +122,8 @@ async def synthesize_speech(request: TTSRequest):
|
|
114 |
max_value = np.max(np.abs(audio))
|
115 |
if max_value == 0:
|
116 |
logger.warning("Audio array is all zeros")
|
117 |
-
|
|
|
118 |
logger.info(f"Normalized audio range: [{audio.min()}, {audio.max()}]")
|
119 |
|
120 |
logger.info("Converting to int16")
|
@@ -136,16 +145,26 @@ async def synthesize_speech(request: TTSRequest):
|
|
136 |
logger.info("FileResponse prepared successfully")
|
137 |
|
138 |
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
except Exception as e:
|
140 |
-
logger.error(f"
|
141 |
error_details = {
|
142 |
"error": str(e),
|
|
|
143 |
"traceback": traceback.format_exc()
|
144 |
}
|
145 |
return JSONResponse(
|
146 |
status_code=500,
|
147 |
-
content={"message": "An error occurred during speech synthesis", "details": error_details}
|
148 |
)
|
|
|
|
|
149 |
|
150 |
@app.post("/identify")
|
151 |
async def identify_language(request: AudioRequest):
|
|
|
92 |
|
93 |
@app.post("/synthesize")
|
94 |
async def synthesize_speech(request: TTSRequest):
|
95 |
+
logger.info(f"Synthesize request received: text='{request.text}', language='{request.language}', speed={request.speed}")
|
96 |
try:
|
97 |
+
# Input validation
|
98 |
+
logger.info("Validating input parameters")
|
99 |
+
if not request.text:
|
100 |
+
raise ValueError("Text cannot be empty")
|
101 |
+
if request.language not in TTS_LANGUAGES:
|
102 |
+
raise ValueError(f"Unsupported language: {request.language}")
|
103 |
+
if not 0.5 <= request.speed <= 2.0:
|
104 |
+
raise ValueError(f"Speed must be between 0.5 and 2.0, got {request.speed}")
|
105 |
|
106 |
logger.info("Calling synthesize function")
|
107 |
result, filtered_text = synthesize(request.text, request.language, request.speed)
|
|
|
112 |
raise ValueError("Synthesis failed to produce audio")
|
113 |
|
114 |
sample_rate, audio = result
|
115 |
+
logger.info(f"Synthesis result: sample_rate={sample_rate}, audio_shape={audio.shape if isinstance(audio, np.ndarray) else 'not numpy array'}, audio_dtype={audio.dtype if isinstance(audio, np.ndarray) else type(audio)}")
|
116 |
|
117 |
logger.info("Converting audio to numpy array")
|
118 |
audio = np.array(audio, dtype=np.float32)
|
|
|
122 |
max_value = np.max(np.abs(audio))
|
123 |
if max_value == 0:
|
124 |
logger.warning("Audio array is all zeros")
|
125 |
+
raise ValueError("Generated audio is silent (all zeros)")
|
126 |
+
audio = audio / max_value
|
127 |
logger.info(f"Normalized audio range: [{audio.min()}, {audio.max()}]")
|
128 |
|
129 |
logger.info("Converting to int16")
|
|
|
145 |
logger.info("FileResponse prepared successfully")
|
146 |
|
147 |
return response
|
148 |
+
|
149 |
+
except ValueError as ve:
|
150 |
+
logger.error(f"ValueError in synthesize_speech: {str(ve)}", exc_info=True)
|
151 |
+
return JSONResponse(
|
152 |
+
status_code=400,
|
153 |
+
content={"message": "Invalid input", "details": str(ve)}
|
154 |
+
)
|
155 |
except Exception as e:
|
156 |
+
logger.error(f"Unexpected error in synthesize_speech: {str(e)}", exc_info=True)
|
157 |
error_details = {
|
158 |
"error": str(e),
|
159 |
+
"type": type(e).__name__,
|
160 |
"traceback": traceback.format_exc()
|
161 |
}
|
162 |
return JSONResponse(
|
163 |
status_code=500,
|
164 |
+
content={"message": "An unexpected error occurred during speech synthesis", "details": error_details}
|
165 |
)
|
166 |
+
finally:
|
167 |
+
logger.info("Synthesize request completed")
|
168 |
|
169 |
@app.post("/identify")
|
170 |
async def identify_language(request: AudioRequest):
|