Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
import os
|
4 |
import re
|
|
|
5 |
|
6 |
def download_file(url):
|
7 |
"""Downloads a file from a URL and returns the local file path."""
|
@@ -18,6 +19,12 @@ def download_file(url):
|
|
18 |
safe_filename = re.sub(r'[^\w\-_\. ]', '_', original_filename)
|
19 |
temp_filename = f"{safe_filename}"
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
with open(temp_filename, 'wb') as f:
|
22 |
for chunk in response.iter_content(chunk_size=8192000):
|
23 |
f.write(chunk)
|
@@ -35,6 +42,7 @@ iface = gr.Interface(
|
|
35 |
outputs=gr.File(),
|
36 |
title="File Downloader",
|
37 |
description="Enter the URL of an image, video, document, etc. to download it.",
|
|
|
38 |
)
|
39 |
|
40 |
iface.launch()
|
|
|
2 |
import requests
|
3 |
import os
|
4 |
import re
|
5 |
+
import mimetypes
|
6 |
|
7 |
def download_file(url):
|
8 |
"""Downloads a file from a URL and returns the local file path."""
|
|
|
19 |
safe_filename = re.sub(r'[^\w\-_\. ]', '_', original_filename)
|
20 |
temp_filename = f"{safe_filename}"
|
21 |
|
22 |
+
# Infer file extension from content type
|
23 |
+
content_type = response.headers['content-type']
|
24 |
+
ext = mimetypes.guess_extension(content_type)
|
25 |
+
if ext and not temp_filename.endswith(ext): # Append extension if not already present
|
26 |
+
temp_filename += ext
|
27 |
+
|
28 |
with open(temp_filename, 'wb') as f:
|
29 |
for chunk in response.iter_content(chunk_size=8192000):
|
30 |
f.write(chunk)
|
|
|
42 |
outputs=gr.File(),
|
43 |
title="File Downloader",
|
44 |
description="Enter the URL of an image, video, document, etc. to download it.",
|
45 |
+
concurrency_limit=None
|
46 |
)
|
47 |
|
48 |
iface.launch()
|