Spaces:
Running
Running
import gradio as gr | |
import requests | |
import os | |
def download_file(url): | |
"""Downloads a file from a URL and returns the local file path.""" | |
try: | |
response = requests.get(url, stream=True) | |
response.raise_for_status() # Raise an exception for bad status codes | |
# Generate a unique temporary filename | |
temp_filename = os.path.basename(url) | |
with open(temp_filename, 'wb') as f: | |
for chunk in response.iter_content(chunk_size=8192): | |
f.write(chunk) | |
return temp_filename | |
except requests.exceptions.MissingSchema: | |
return "Error: Invalid URL format. Please include the protocol (e.g., http:// or https://)." | |
except requests.exceptions.ConnectionError: | |
return "Error: Could not connect to the server. Please check your internet connection." | |
except requests.exceptions.RequestException as e: | |
return f"Error downloading file: {e}" | |
iface = gr.Interface( | |
fn=download_file, | |
inputs=gr.Textbox(lines=1, placeholder="Enter URL of the file"), | |
outputs=gr.File(), | |
title="File Downloader", | |
description="Enter the URL of an image, video, document, etc. to download it.", | |
) | |
iface.launch() |