MariaKaiser commited on
Commit
ac044bc
·
verified ·
1 Parent(s): ff6cba5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -41
app.py CHANGED
@@ -105,66 +105,66 @@ 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
- 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
  #-----------------------------------------------------------
169
 
170
  #takes the text to be said and path to the prosody audio and path to save the generated audio and returns path to the generated audio
 
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
  #-----------------------------------------------------------
169
 
170
  #takes the text to be said and path to the prosody audio and path to save the generated audio and returns path to the generated audio