MariaKaiser commited on
Commit
64b8e6d
·
verified ·
1 Parent(s): 731b06c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -11
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
- async with httpx.AsyncClient() as client:
112
- response = await client.get(url)
113
- if response.status_code != 200:
114
- raise RuntimeError(f"Failed to fetch file: {response.text}")
115
-
116
- # Save to a temporary file
117
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
118
- temp_file.write(response.content)
119
- temp_file.close()
120
- return temp_file.name
 
 
 
 
 
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