prithivMLmods commited on
Commit
1dfda18
·
verified ·
1 Parent(s): 1bc7cd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -7
app.py CHANGED
@@ -4,6 +4,7 @@ from bs4 import BeautifulSoup
4
  from urllib.parse import urljoin, urlparse
5
  from zipfile import ZipFile
6
  from io import BytesIO
 
7
 
8
  def download_file(url, session):
9
  """Download a file and return its content."""
@@ -11,7 +12,7 @@ def download_file(url, session):
11
  response.raise_for_status()
12
  return response.content
13
 
14
- def save_webpage_as_zip(url, output_zip):
15
  """Save a webpage and its assets as a ZIP file."""
16
  session = requests.Session()
17
  response = session.get(url)
@@ -53,8 +54,9 @@ def save_webpage_as_zip(url, output_zip):
53
  with open(asset_full_path, 'wb') as f:
54
  f.write(content)
55
 
56
- # Create a ZIP file
57
- with ZipFile(output_zip, 'w') as zipf:
 
58
  for root, _, files in os.walk(temp_dir):
59
  for file in files:
60
  file_path = os.path.join(root, file)
@@ -66,7 +68,20 @@ def save_webpage_as_zip(url, output_zip):
66
  os.remove(os.path.join(root, file))
67
  os.rmdir(root)
68
 
69
- # Example usage
70
- url = 'http://example.com'
71
- output_zip = 'webpage.zip'
72
- save_webpage_as_zip(url, output_zip)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  from urllib.parse import urljoin, urlparse
5
  from zipfile import ZipFile
6
  from io import BytesIO
7
+ import gradio as gr
8
 
9
  def download_file(url, session):
10
  """Download a file and return its content."""
 
12
  response.raise_for_status()
13
  return response.content
14
 
15
+ def save_webpage_as_zip(url):
16
  """Save a webpage and its assets as a ZIP file."""
17
  session = requests.Session()
18
  response = session.get(url)
 
54
  with open(asset_full_path, 'wb') as f:
55
  f.write(content)
56
 
57
+ # Create a ZIP file in memory
58
+ zip_buffer = BytesIO()
59
+ with ZipFile(zip_buffer, 'w') as zipf:
60
  for root, _, files in os.walk(temp_dir):
61
  for file in files:
62
  file_path = os.path.join(root, file)
 
68
  os.remove(os.path.join(root, file))
69
  os.rmdir(root)
70
 
71
+ zip_buffer.seek(0)
72
+ return zip_buffer
73
+
74
+ def generate_zip_file(url):
75
+ """Generate ZIP file from a webpage URL."""
76
+ zip_buffer = save_webpage_as_zip(url)
77
+ return zip_buffer, 'webpage.zip'
78
+
79
+ # Gradio Interface
80
+ with gr.Blocks() as demo:
81
+ url_input = gr.Textbox(label="Website URL")
82
+ download_button = gr.Button("Download as ZIP")
83
+ output_file = gr.File(label="Download")
84
+
85
+ download_button.click(fn=generate_zip_file, inputs=url_input, outputs=output_file)
86
+
87
+ demo.launch()