MariaKaiser commited on
Commit
5987205
·
verified ·
1 Parent(s): e27bc6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -11
app.py CHANGED
@@ -105,31 +105,64 @@ import httpx
105
  import tempfile
106
  import asyncio
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  async def download_file_from_url(url: str, retries: int = 3, delay: float = 2.0) -> str | None:
109
  """
110
  Downloads a file from a URL and returns the path to a temporary file.
111
- Retries on failure up to `retries` times, waiting `delay` seconds between attempts.
112
- Returns None if all attempts fail.
113
  """
 
 
 
 
114
  for attempt in range(1, retries + 1):
115
  try:
116
- async with httpx.AsyncClient(timeout=60.0) as client: # increased timeout
117
  response = await client.get(url)
118
- response.raise_for_status() # raises for non-200 status codes
119
 
120
- # Save to a temporary file
121
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
122
  temp_file.write(response.content)
123
  temp_file.close()
124
- print(f"Downloaded {url} successfully on attempt {attempt}")
 
125
  return temp_file.name
126
 
127
  except Exception as e:
128
  print(f"Attempt {attempt} failed for {url}: {e}")
129
  if attempt < retries:
130
- await asyncio.sleep(delay) # wait before retrying
131
 
132
- print(f"All {retries} attempts failed for {url}")
133
  return None
134
 
135
  #-----------------------------------------------------------
@@ -242,9 +275,12 @@ async def concat_story_audio(story: StoryCreationDTO, base_output: str, final_pa
242
  # --- Add SFX for location if available ---
243
  if scene.location.path:
244
  sfx_file = await download_file_from_url(scene.location.path)
245
- sfx_audio = AudioSegment.from_wav(sfx_file)
246
- scene_audio = scene_audio.overlay(sfx_audio)
247
- os.remove(sfx_file)
 
 
 
248
 
249
  # --- Add background music if available ---
250
  if scene.bgMusic and scene.bgMusic.musicPath:
 
105
  import tempfile
106
  import asyncio
107
 
108
+ # async def download_file_from_url(url: str, retries: int = 3, delay: float = 2.0) -> str | None:
109
+ # """
110
+ # Downloads a file from a URL and returns the path to a temporary file.
111
+ # Retries on failure up to `retries` times, waiting `delay` seconds between attempts.
112
+ # Returns None if all attempts fail.
113
+ # """
114
+ # for attempt in range(1, retries + 1):
115
+ # try:
116
+ # async with httpx.AsyncClient(timeout=60.0) as client: # increased timeout
117
+ # response = await client.get(url)
118
+ # response.raise_for_status() # raises for non-200 status codes
119
+
120
+ # # Save to a temporary file
121
+ # temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
122
+ # temp_file.write(response.content)
123
+ # temp_file.close()
124
+ # print(f"Downloaded {url} successfully on attempt {attempt}")
125
+ # return temp_file.name
126
+
127
+ # except Exception as e:
128
+ # print(f"Attempt {attempt} failed for {url}: {e}")
129
+ # if attempt < retries:
130
+ # await asyncio.sleep(delay) # wait before retrying
131
+
132
+ # print(f"All {retries} attempts failed for {url}")
133
+ # return None
134
+
135
+ download_cache = {}
136
+
137
  async def download_file_from_url(url: str, retries: int = 3, delay: float = 2.0) -> str | None:
138
  """
139
  Downloads a file from a URL and returns the path to a temporary file.
140
+ If download fails after `retries` attempts, returns None instead of raising an error.
141
+ Caches successful downloads to avoid repeated requests.
142
  """
143
+ if url in download_cache:
144
+ print(f"{url} is got from cache")
145
+ return download_cache[url]
146
+
147
  for attempt in range(1, retries + 1):
148
  try:
149
+ async with httpx.AsyncClient(timeout=60.0) as client:
150
  response = await client.get(url)
151
+ response.raise_for_status()
152
 
 
153
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
154
  temp_file.write(response.content)
155
  temp_file.close()
156
+
157
+ download_cache[url] = temp_file.name
158
  return temp_file.name
159
 
160
  except Exception as e:
161
  print(f"Attempt {attempt} failed for {url}: {e}")
162
  if attempt < retries:
163
+ await asyncio.sleep(delay)
164
 
165
+ print(f"All {retries} attempts failed for {url}, skipping...")
166
  return None
167
 
168
  #-----------------------------------------------------------
 
275
  # --- Add SFX for location if available ---
276
  if scene.location.path:
277
  sfx_file = await download_file_from_url(scene.location.path)
278
+ if sfx_file:
279
+ sfx_audio = AudioSegment.from_wav(sfx_file)
280
+ scene_audio = scene_audio.overlay(sfx_audio)
281
+ os.remove(sfx_file)
282
+ else:
283
+ print(f"SFX skipped for {scene.location.locationName}")
284
 
285
  # --- Add background music if available ---
286
  if scene.bgMusic and scene.bgMusic.musicPath: