Update process_interview.py
Browse files- process_interview.py +14 -8
process_interview.py
CHANGED
|
@@ -50,18 +50,24 @@ PINECONE_KEY = os.getenv("PINECONE_KEY")
|
|
| 50 |
ASSEMBLYAI_KEY = os.getenv("ASSEMBLYAI_KEY")
|
| 51 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 52 |
|
| 53 |
-
def download_audio_from_url(url: str) -> str:
|
| 54 |
-
"""Downloads an audio file from a URL to a temporary local path."""
|
| 55 |
try:
|
| 56 |
temp_dir = tempfile.gettempdir()
|
| 57 |
temp_path = os.path.join(temp_dir, f"{uuid.uuid4()}.tmp_audio")
|
| 58 |
logger.info(f"Downloading audio from {url} to {temp_path}")
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
logger.error(f"Failed to download audio from URL {url}: {e}")
|
| 67 |
raise
|
|
|
|
| 50 |
ASSEMBLYAI_KEY = os.getenv("ASSEMBLYAI_KEY")
|
| 51 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 52 |
|
| 53 |
+
def download_audio_from_url(url: str, retries=3) -> str:
|
| 54 |
+
"""Downloads an audio file from a URL to a temporary local path with retries."""
|
| 55 |
try:
|
| 56 |
temp_dir = tempfile.gettempdir()
|
| 57 |
temp_path = os.path.join(temp_dir, f"{uuid.uuid4()}.tmp_audio")
|
| 58 |
logger.info(f"Downloading audio from {url} to {temp_path}")
|
| 59 |
+
for attempt in range(retries):
|
| 60 |
+
try:
|
| 61 |
+
with requests.get(url, stream=True, timeout=30) as r:
|
| 62 |
+
r.raise_for_status()
|
| 63 |
+
with open(temp_path, 'wb') as f:
|
| 64 |
+
for chunk in r.iter_content(chunk_size=8192):
|
| 65 |
+
f.write(chunk)
|
| 66 |
+
return temp_path
|
| 67 |
+
except (requests.exceptions.ChunkedEncodingError, urllib3.exceptions.ProtocolError) as e:
|
| 68 |
+
logger.warning(f"Attempt {attempt + 1} failed: {e}. Retrying...")
|
| 69 |
+
time.sleep(2 ** attempt) # Exponential backoff
|
| 70 |
+
raise Exception(f"Failed to download audio after {retries} attempts.")
|
| 71 |
except Exception as e:
|
| 72 |
logger.error(f"Failed to download audio from URL {url}: {e}")
|
| 73 |
raise
|