Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,43 +31,45 @@ async def check_n_load_attach(session: aiohttp.ClientSession, task_id
|
|
| 31 |
try:
|
| 32 |
async with session.get(file_url, timeout=15) as response:
|
| 33 |
if response.status == 200:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
elif "text/x-python" in content_type:
|
| 54 |
-
extension = ".py"
|
| 55 |
-
else:
|
| 56 |
-
extension = ""
|
| 57 |
-
filename = f"{task_id}{extension}"
|
| 58 |
|
| 59 |
-
#
|
|
|
|
| 60 |
local_file_path = os.path.join("downloads", filename)
|
| 61 |
os.makedirs("downloads", exist_ok=True)
|
|
|
|
|
|
|
| 62 |
async with aiofiles.open(local_file_path, "wb") as file:
|
| 63 |
async for chunk in response.content.iter_chunked(8192):
|
| 64 |
await file.write(chunk)
|
| 65 |
print(f"File downloaded successfully: {local_file_path}")
|
| 66 |
return local_file_path
|
|
|
|
|
|
|
|
|
|
| 67 |
except aiohttp.ClientError as e:
|
| 68 |
print(f"Error downloading attachment for task {task_id}: {str(e)}")
|
| 69 |
return None
|
| 70 |
|
|
|
|
| 71 |
async def fetch_questions(session: aiohttp.ClientSession, questions_url: str) -> list:
|
| 72 |
"""Fetch questions asynchronously."""
|
| 73 |
try:
|
|
|
|
| 31 |
try:
|
| 32 |
async with session.get(file_url, timeout=15) as response:
|
| 33 |
if response.status == 200:
|
| 34 |
+
|
| 35 |
+
# Determine file extension from Content-Type
|
| 36 |
+
content_type = str(response.headers.get("Content-Type", "")).lower()
|
| 37 |
+
extension = ""
|
| 38 |
+
if "image/png" in content_type:
|
| 39 |
+
extension = ".png"
|
| 40 |
+
elif "image/jpeg" in content_type:
|
| 41 |
+
extension = ".jpg"
|
| 42 |
+
elif "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" in content_type:
|
| 43 |
+
extension = ".xlsx"
|
| 44 |
+
elif "audio/mpeg" in content_type:
|
| 45 |
+
extension = ".mp3"
|
| 46 |
+
elif "application/pdf" in content_type:
|
| 47 |
+
extension = ".pdf"
|
| 48 |
+
elif "text/x-python" in content_type:
|
| 49 |
+
extension = ".py"
|
| 50 |
+
else:
|
| 51 |
+
print(f"Unsupported content type: {content_type} for task {task_id}")
|
| 52 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# Use task_id as the filename to ensure uniqueness
|
| 55 |
+
filename = f"{task_id}{extension}"
|
| 56 |
local_file_path = os.path.join("downloads", filename)
|
| 57 |
os.makedirs("downloads", exist_ok=True)
|
| 58 |
+
|
| 59 |
+
# Save the file
|
| 60 |
async with aiofiles.open(local_file_path, "wb") as file:
|
| 61 |
async for chunk in response.content.iter_chunked(8192):
|
| 62 |
await file.write(chunk)
|
| 63 |
print(f"File downloaded successfully: {local_file_path}")
|
| 64 |
return local_file_path
|
| 65 |
+
else:
|
| 66 |
+
print(f"Failed to download file for task {task_id}: HTTP {response.status}")
|
| 67 |
+
return None
|
| 68 |
except aiohttp.ClientError as e:
|
| 69 |
print(f"Error downloading attachment for task {task_id}: {str(e)}")
|
| 70 |
return None
|
| 71 |
|
| 72 |
+
|
| 73 |
async def fetch_questions(session: aiohttp.ClientSession, questions_url: str) -> list:
|
| 74 |
"""Fetch questions asynchronously."""
|
| 75 |
try:
|