Spaces:
Running
Running
WIP
Browse files
app.py
CHANGED
@@ -171,7 +171,7 @@ def download_youtube_audio(url):
|
|
171 |
f"{SIEVE_API_URL}/push",
|
172 |
headers={"X-API-Key": SIEVE_API_KEY, "Content-Type": "application/json"},
|
173 |
json=payload,
|
174 |
-
timeout=
|
175 |
)
|
176 |
response.raise_for_status()
|
177 |
response_data = response.json()
|
@@ -197,10 +197,10 @@ def download_youtube_audio(url):
|
|
197 |
|
198 |
logger.info(f"Received job ID: {job_id}")
|
199 |
|
200 |
-
# Poll for job completion
|
201 |
poll_count = 0
|
202 |
-
max_polls =
|
203 |
-
|
204 |
|
205 |
while True:
|
206 |
poll_count += 1
|
@@ -210,14 +210,16 @@ def download_youtube_audio(url):
|
|
210 |
job_response = requests.get(
|
211 |
f"{SIEVE_API_URL}/jobs/{job_id}",
|
212 |
headers={"X-API-Key": SIEVE_API_KEY},
|
213 |
-
timeout=
|
214 |
)
|
215 |
job_response.raise_for_status()
|
216 |
job_data = job_response.json()
|
217 |
logger.debug(f"Job status response: {job_data}")
|
218 |
|
219 |
status = job_data.get("status")
|
220 |
-
|
|
|
|
|
221 |
|
222 |
if status == "completed":
|
223 |
logger.info("Job completed successfully")
|
@@ -247,18 +249,18 @@ def download_youtube_audio(url):
|
|
247 |
with open(output_path, "wb") as f:
|
248 |
f.write(audio_response.content)
|
249 |
logger.info(f"Successfully saved audio to: {output_path}")
|
250 |
-
|
251 |
-
|
|
|
252 |
|
253 |
elif status == "failed":
|
254 |
error_msg = job_data.get("error", "Unknown error")
|
255 |
logger.error(f"Job failed with error: {error_msg}")
|
256 |
raise gr.Error(f"Job failed: {error_msg}")
|
257 |
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
raise gr.Error("Download timed out. Please try again.")
|
262 |
|
263 |
logger.info("Job still processing, waiting 2 seconds before next poll...")
|
264 |
time.sleep(2)
|
@@ -275,6 +277,8 @@ def download_youtube_audio(url):
|
|
275 |
except Exception as e:
|
276 |
logger.exception(f"Unexpected error during YouTube download: {str(e)}")
|
277 |
raise gr.Error(f"Failed to download YouTube audio: {str(e)}")
|
|
|
|
|
278 |
|
279 |
def transcribe_youtube(url, return_timestamps, generate_subs):
|
280 |
"""Transcribe audio from YouTube video.
|
|
|
171 |
f"{SIEVE_API_URL}/push",
|
172 |
headers={"X-API-Key": SIEVE_API_KEY, "Content-Type": "application/json"},
|
173 |
json=payload,
|
174 |
+
timeout=1800 # Add timeout
|
175 |
)
|
176 |
response.raise_for_status()
|
177 |
response_data = response.json()
|
|
|
197 |
|
198 |
logger.info(f"Received job ID: {job_id}")
|
199 |
|
200 |
+
# Poll for job completion
|
201 |
poll_count = 0
|
202 |
+
max_polls = 180 # Maximum number of polls (6 minutes with 2-second delay)
|
203 |
+
last_status = None
|
204 |
|
205 |
while True:
|
206 |
poll_count += 1
|
|
|
210 |
job_response = requests.get(
|
211 |
f"{SIEVE_API_URL}/jobs/{job_id}",
|
212 |
headers={"X-API-Key": SIEVE_API_KEY},
|
213 |
+
timeout=1800,
|
214 |
)
|
215 |
job_response.raise_for_status()
|
216 |
job_data = job_response.json()
|
217 |
logger.debug(f"Job status response: {job_data}")
|
218 |
|
219 |
status = job_data.get("status")
|
220 |
+
if status != last_status:
|
221 |
+
logger.info(f"Job status changed: {status}")
|
222 |
+
last_status = status
|
223 |
|
224 |
if status == "completed":
|
225 |
logger.info("Job completed successfully")
|
|
|
249 |
with open(output_path, "wb") as f:
|
250 |
f.write(audio_response.content)
|
251 |
logger.info(f"Successfully saved audio to: {output_path}")
|
252 |
+
|
253 |
+
# Break out of the polling loop after successful download
|
254 |
+
break
|
255 |
|
256 |
elif status == "failed":
|
257 |
error_msg = job_data.get("error", "Unknown error")
|
258 |
logger.error(f"Job failed with error: {error_msg}")
|
259 |
raise gr.Error(f"Job failed: {error_msg}")
|
260 |
|
261 |
+
if poll_count >= max_polls:
|
262 |
+
logger.error("Maximum polling attempts reached")
|
263 |
+
raise gr.Error("Download took too long. Please try again or check if the video is accessible.")
|
|
|
264 |
|
265 |
logger.info("Job still processing, waiting 2 seconds before next poll...")
|
266 |
time.sleep(2)
|
|
|
277 |
except Exception as e:
|
278 |
logger.exception(f"Unexpected error during YouTube download: {str(e)}")
|
279 |
raise gr.Error(f"Failed to download YouTube audio: {str(e)}")
|
280 |
+
|
281 |
+
return output_path
|
282 |
|
283 |
def transcribe_youtube(url, return_timestamps, generate_subs):
|
284 |
"""Transcribe audio from YouTube video.
|