Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -133,8 +133,6 @@ import asyncio
|
|
| 133 |
# return None
|
| 134 |
|
| 135 |
|
| 136 |
-
download_cache = {}
|
| 137 |
-
|
| 138 |
async def download_scene_files(scene: SceneDto):
|
| 139 |
tasks = []
|
| 140 |
|
|
@@ -154,37 +152,83 @@ async def download_scene_files(scene: SceneDto):
|
|
| 154 |
downloaded_files = await asyncio.gather(*tasks)
|
| 155 |
return downloaded_files
|
| 156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
async def download_file_from_url(url: str, retries: int = 3, delay: float = 2.0) -> str | None:
|
| 158 |
"""
|
| 159 |
-
Downloads a file from a URL and
|
| 160 |
-
|
| 161 |
-
Caches successful downloads to avoid repeated requests.
|
| 162 |
"""
|
| 163 |
if url in download_cache:
|
| 164 |
-
#print(f"{url} is got from cache")
|
| 165 |
return download_cache[url]
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
for attempt in range(1, retries + 1):
|
| 168 |
try:
|
| 169 |
async with httpx.AsyncClient(timeout=60.0) as client:
|
| 170 |
response = await client.get(url)
|
| 171 |
response.raise_for_status()
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
download_cache[url] = temp_file.name
|
| 180 |
-
return temp_file.name
|
| 181 |
|
| 182 |
except Exception as e:
|
| 183 |
-
#print(f"Attempt {attempt} failed for {url}: {e}")
|
| 184 |
if attempt < retries:
|
| 185 |
await asyncio.sleep(delay)
|
| 186 |
|
| 187 |
-
#print(f"All {retries} attempts failed for {url}, skipping...")
|
| 188 |
return None
|
| 189 |
|
| 190 |
#-----------------------------------------------------------
|
|
@@ -643,10 +687,10 @@ async def get_results(task_id: str):
|
|
| 643 |
if result and all(k in result for k in ("fileName", "duration", "audioPath")):
|
| 644 |
#clearing cache
|
| 645 |
print(f"all fields are available {result}")
|
| 646 |
-
for file_path in download_cache.values():
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
download_cache.clear()
|
| 650 |
|
| 651 |
return {"status": "completed", **result}
|
| 652 |
else:
|
|
|
|
| 133 |
# return None
|
| 134 |
|
| 135 |
|
|
|
|
|
|
|
| 136 |
async def download_scene_files(scene: SceneDto):
|
| 137 |
tasks = []
|
| 138 |
|
|
|
|
| 152 |
downloaded_files = await asyncio.gather(*tasks)
|
| 153 |
return downloaded_files
|
| 154 |
|
| 155 |
+
# async def download_file_from_url(url: str, retries: int = 3, delay: float = 2.0) -> str | None:
|
| 156 |
+
# """
|
| 157 |
+
# Downloads a file from a URL and returns the path to a temporary file.
|
| 158 |
+
# If download fails after `retries` attempts, returns None instead of raising an error.
|
| 159 |
+
# Caches successful downloads to avoid repeated requests.
|
| 160 |
+
# """
|
| 161 |
+
# if url in download_cache:
|
| 162 |
+
# #print(f"{url} is got from cache")
|
| 163 |
+
# return download_cache[url]
|
| 164 |
+
|
| 165 |
+
# for attempt in range(1, retries + 1):
|
| 166 |
+
# try:
|
| 167 |
+
# async with httpx.AsyncClient(timeout=60.0) as client:
|
| 168 |
+
# response = await client.get(url)
|
| 169 |
+
# response.raise_for_status()
|
| 170 |
+
|
| 171 |
+
# temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 172 |
+
# temp_file.write(response.content)
|
| 173 |
+
# temp_file.close()
|
| 174 |
+
|
| 175 |
+
# #print(f"{url} is downloaded and saved in cache")
|
| 176 |
+
|
| 177 |
+
# download_cache[url] = temp_file.name
|
| 178 |
+
# return temp_file.name
|
| 179 |
+
|
| 180 |
+
# except Exception as e:
|
| 181 |
+
# #print(f"Attempt {attempt} failed for {url}: {e}")
|
| 182 |
+
# if attempt < retries:
|
| 183 |
+
# await asyncio.sleep(delay)
|
| 184 |
+
|
| 185 |
+
# #print(f"All {retries} attempts failed for {url}, skipping...")
|
| 186 |
+
# return None
|
| 187 |
+
|
| 188 |
+
import os
|
| 189 |
+
import httpx
|
| 190 |
+
import asyncio
|
| 191 |
+
|
| 192 |
+
CACHE_DIR = "audio_cache"
|
| 193 |
+
os.makedirs(CACHE_DIR, exist_ok=True) # create if not exists folder stores permanently on disk
|
| 194 |
+
|
| 195 |
+
download_cache = {} # in-memory map: url -> local file
|
| 196 |
+
|
| 197 |
async def download_file_from_url(url: str, retries: int = 3, delay: float = 2.0) -> str | None:
|
| 198 |
"""
|
| 199 |
+
Downloads a file from a URL and stores it in a permanent cache folder.
|
| 200 |
+
Returns the local file path. Reuses already downloaded files.
|
|
|
|
| 201 |
"""
|
| 202 |
if url in download_cache:
|
|
|
|
| 203 |
return download_cache[url]
|
| 204 |
+
|
| 205 |
+
# determine local file path in cache folder
|
| 206 |
+
filename = url.split("/")[-1] # simple filename from URL
|
| 207 |
+
local_path = os.path.join(CACHE_DIR, filename)
|
| 208 |
+
|
| 209 |
+
# check if file already exists on disk
|
| 210 |
+
if os.path.exists(local_path):
|
| 211 |
+
download_cache[url] = local_path
|
| 212 |
+
return local_path
|
| 213 |
+
|
| 214 |
+
# download if not cached
|
| 215 |
for attempt in range(1, retries + 1):
|
| 216 |
try:
|
| 217 |
async with httpx.AsyncClient(timeout=60.0) as client:
|
| 218 |
response = await client.get(url)
|
| 219 |
response.raise_for_status()
|
| 220 |
|
| 221 |
+
# save to permanent cache folder
|
| 222 |
+
with open(local_path, "wb") as f:
|
| 223 |
+
f.write(response.content)
|
| 224 |
+
|
| 225 |
+
download_cache[url] = local_path
|
| 226 |
+
return local_path
|
|
|
|
|
|
|
| 227 |
|
| 228 |
except Exception as e:
|
|
|
|
| 229 |
if attempt < retries:
|
| 230 |
await asyncio.sleep(delay)
|
| 231 |
|
|
|
|
| 232 |
return None
|
| 233 |
|
| 234 |
#-----------------------------------------------------------
|
|
|
|
| 687 |
if result and all(k in result for k in ("fileName", "duration", "audioPath")):
|
| 688 |
#clearing cache
|
| 689 |
print(f"all fields are available {result}")
|
| 690 |
+
# for file_path in download_cache.values():
|
| 691 |
+
# if os.path.exists(file_path):
|
| 692 |
+
# os.remove(file_path)
|
| 693 |
+
# download_cache.clear()
|
| 694 |
|
| 695 |
return {"status": "completed", **result}
|
| 696 |
else:
|