Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
-
def
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
return file_name, response.content
|
| 10 |
-
except requests.exceptions.RequestException as e:
|
| 11 |
-
return None, str(e)
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
# Create the Gradio interface
|
| 21 |
iface = gr.Interface(
|
| 22 |
-
fn=
|
| 23 |
-
inputs=gr.Textbox(lines=1, placeholder="Enter
|
| 24 |
-
outputs=
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
],
|
| 28 |
-
title="File Fetcher",
|
| 29 |
-
description="Enter the URL of the file to fetch and download it."
|
| 30 |
)
|
| 31 |
|
| 32 |
-
# Launch the Gradio app
|
| 33 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
def download_file(url):
|
| 6 |
+
"""Downloads a file from a URL and returns the local file path."""
|
| 7 |
+
try:
|
| 8 |
+
response = requests.get(url, stream=True)
|
| 9 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Generate a unique temporary filename
|
| 12 |
+
temp_filename = os.path.basename(url)
|
| 13 |
+
with open(temp_filename, 'wb') as f:
|
| 14 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 15 |
+
f.write(chunk)
|
| 16 |
+
return temp_filename
|
| 17 |
+
except requests.exceptions.MissingSchema:
|
| 18 |
+
return "Error: Invalid URL format. Please include the protocol (e.g., http:// or https://)."
|
| 19 |
+
except requests.exceptions.ConnectionError:
|
| 20 |
+
return "Error: Could not connect to the server. Please check your internet connection."
|
| 21 |
+
except requests.exceptions.RequestException as e:
|
| 22 |
+
return f"Error downloading file: {e}"
|
| 23 |
|
|
|
|
| 24 |
iface = gr.Interface(
|
| 25 |
+
fn=download_file,
|
| 26 |
+
inputs=gr.Textbox(lines=1, placeholder="Enter URL of the file"),
|
| 27 |
+
outputs=gr.File(),
|
| 28 |
+
title="File Downloader",
|
| 29 |
+
description="Enter the URL of an image, video, document, etc. to download it.",
|
|
|
|
|
|
|
|
|
|
| 30 |
)
|
| 31 |
|
|
|
|
| 32 |
iface.launch()
|