KingNish commited on
Commit
e6218e4
1 Parent(s): d6e49e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -24
app.py CHANGED
@@ -1,33 +1,32 @@
1
  import gradio as gr
2
  import requests
 
3
 
4
- def fetch_file(url):
5
- try:
6
- response = requests.get(url)
7
- response.raise_for_status() # Raise an error for bad status codes
8
- file_name = url.split('/')[-1] # Extract the file name from the URL
9
- return file_name, response.content
10
- except requests.exceptions.RequestException as e:
11
- return None, str(e)
12
 
13
- def gradio_fetch_file(url):
14
- file_name, file_content = fetch_file(url)
15
- if file_name:
16
- return file_name, file_content
17
- else:
18
- return None, file_content
 
 
 
 
 
 
19
 
20
- # Create the Gradio interface
21
  iface = gr.Interface(
22
- fn=gradio_fetch_file,
23
- inputs=gr.Textbox(lines=1, placeholder="Enter the URL of the file..."),
24
- outputs=[
25
- gr.File(label="Downloaded File"),
26
- gr.Textbox(label="Error Message")
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()