Spaces:
Running
Running
WIP
Browse files
app.py
CHANGED
@@ -199,29 +199,41 @@ def download_youtube_audio(url):
|
|
199 |
|
200 |
def check_api_health():
|
201 |
"""Check if the API is healthy before making requests."""
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
|
|
|
|
210 |
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
|
226 |
def transcribe_youtube(url, return_timestamps, generate_subs, chunk_length_s=15, batch_size=8):
|
227 |
"""Transcribe audio from YouTube video using URL endpoint."""
|
|
|
199 |
|
200 |
def check_api_health():
|
201 |
"""Check if the API is healthy before making requests."""
|
202 |
+
max_retries = 5
|
203 |
+
retry_delay = 10 # seconds
|
204 |
+
last_error = None
|
205 |
+
|
206 |
+
for attempt in range(max_retries):
|
207 |
+
try:
|
208 |
+
logger.info(f"Performing API health check (attempt {attempt + 1}/{max_retries})...")
|
209 |
+
response = requests.get(f"{API_URL}/health")
|
210 |
+
response.raise_for_status()
|
211 |
+
health_data = response.json()
|
212 |
|
213 |
+
# Check if service is healthy
|
214 |
+
if health_data.get("status") != "healthy":
|
215 |
+
raise gr.Error("API service is not healthy. Please try again later.")
|
216 |
+
|
217 |
+
# Check resource usage
|
218 |
+
cpu_percent = health_data.get("cpu_percent", 0)
|
219 |
+
memory_percent = health_data.get("memory_percent", 0)
|
220 |
+
|
221 |
+
if cpu_percent > 90 or memory_percent > 90:
|
222 |
+
logger.warning(f"High resource usage detected - CPU: {cpu_percent}%, Memory: {memory_percent}%")
|
223 |
+
|
224 |
+
logger.info("API health check passed successfully")
|
225 |
+
return True
|
226 |
+
|
227 |
+
except requests.exceptions.RequestException as e:
|
228 |
+
last_error = str(e)
|
229 |
+
logger.warning(f"Health check attempt {attempt + 1} failed: {last_error}")
|
230 |
+
if attempt < max_retries - 1:
|
231 |
+
logger.info(f"Waiting {retry_delay} seconds before next attempt...")
|
232 |
+
time.sleep(retry_delay)
|
233 |
+
continue
|
234 |
+
|
235 |
+
logger.error(f"All health check attempts failed. Last error: {last_error}")
|
236 |
+
raise gr.Error(f"Failed to connect to the API service after {max_retries} attempts. Please try again later.")
|
237 |
|
238 |
def transcribe_youtube(url, return_timestamps, generate_subs, chunk_length_s=15, batch_size=8):
|
239 |
"""Transcribe audio from YouTube video using URL endpoint."""
|