Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -104,20 +104,26 @@ class StoryCreationDTO(BaseModel):
|
|
| 104 |
import httpx
|
| 105 |
import tempfile
|
| 106 |
|
| 107 |
-
async def download_file_from_url(url: str) -> str:
|
| 108 |
"""
|
| 109 |
Downloads a file from a URL and returns the path to a temporary file.
|
|
|
|
| 110 |
"""
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
#-----------------------------------------------------------
|
| 123 |
|
|
|
|
| 104 |
import httpx
|
| 105 |
import tempfile
|
| 106 |
|
| 107 |
+
async def download_file_from_url(url: str) -> str | None:
|
| 108 |
"""
|
| 109 |
Downloads a file from a URL and returns the path to a temporary file.
|
| 110 |
+
Returns None if the download fails.
|
| 111 |
"""
|
| 112 |
+
try:
|
| 113 |
+
async with httpx.AsyncClient() as client:
|
| 114 |
+
response = await client.get(url, timeout=30.0) # optional timeout
|
| 115 |
+
response.raise_for_status() # raises for non-200 status codes
|
| 116 |
+
|
| 117 |
+
# Save to a temporary file
|
| 118 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 119 |
+
temp_file.write(response.content)
|
| 120 |
+
temp_file.close()
|
| 121 |
+
return temp_file.name
|
| 122 |
+
|
| 123 |
+
except Exception as e:
|
| 124 |
+
# Catch any exception (network, timeout, invalid response)
|
| 125 |
+
print(f"Failed to download {url}: {e}")
|
| 126 |
+
return None
|
| 127 |
|
| 128 |
#-----------------------------------------------------------
|
| 129 |
|