muhtasham commited on
Commit
0e36c34
·
1 Parent(s): a231b66
Files changed (1) hide show
  1. app.py +34 -22
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
- try:
203
- response = requests.get(f"{API_URL}/health")
204
- response.raise_for_status()
205
- health_data = response.json()
206
-
207
- # Check if service is healthy
208
- if health_data.get("status") != "healthy":
209
- raise gr.Error("API service is not healthy. Please try again later.")
 
 
210
 
211
- # Check resource usage
212
- cpu_percent = health_data.get("cpu_percent", 0)
213
- memory_percent = health_data.get("memory_percent", 0)
214
-
215
- if cpu_percent > 90 or memory_percent > 90:
216
- logger.warning(f"High resource usage detected - CPU: {cpu_percent}%, Memory: {memory_percent}%")
217
-
218
- logger.info("API health check passed successfully")
219
-
220
- return True
221
-
222
- except requests.exceptions.RequestException as e:
223
- logger.error(f"Health check failed: {str(e)}")
224
- raise gr.Error("Failed to connect to the API service. Please try again later.")
 
 
 
 
 
 
 
 
 
 
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."""