Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ import random
|
|
8 |
import gradio as gr
|
9 |
import shutil
|
10 |
|
11 |
-
#
|
12 |
def get_google_suggestions(query):
|
13 |
url = f"http://suggestqueries.google.com/complete/search?client=firefox&q={query}"
|
14 |
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}
|
@@ -18,7 +18,6 @@ def get_google_suggestions(query):
|
|
18 |
except:
|
19 |
return []
|
20 |
|
21 |
-
# Hàm tải ảnh
|
22 |
def download_images(search_term, num_images_per_term, save_folder, status_callback):
|
23 |
if not os.path.exists(save_folder):
|
24 |
os.makedirs(save_folder)
|
@@ -54,7 +53,6 @@ def download_images(search_term, num_images_per_term, save_folder, status_callba
|
|
54 |
|
55 |
return count
|
56 |
|
57 |
-
# Hàm nén thư mục
|
58 |
def zip_folder(folder_path):
|
59 |
output_zip = os.path.join(os.path.dirname(folder_path), "downloaded_images")
|
60 |
try:
|
@@ -63,7 +61,6 @@ def zip_folder(folder_path):
|
|
63 |
except Exception as e:
|
64 |
return None, f"Lỗi khi nén: {str(e)}"
|
65 |
|
66 |
-
# Hàm chính tải ảnh
|
67 |
def start_download(initial_query, target_images, max_per_term, save_folder, zip_files):
|
68 |
status_log = []
|
69 |
|
@@ -108,79 +105,136 @@ def start_download(initial_query, target_images, max_per_term, save_folder, zip_
|
|
108 |
|
109 |
return "\n".join(status_log), zip_file_path
|
110 |
|
111 |
-
# Giao diện Gradio
|
112 |
def create_interface():
|
113 |
css = """
|
114 |
.container {
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
119 |
}
|
120 |
h1 {
|
121 |
-
color: #
|
122 |
text-align: center;
|
123 |
-
font-family:
|
|
|
124 |
}
|
125 |
-
.input-
|
126 |
-
background:
|
127 |
-
|
128 |
-
|
129 |
-
margin:
|
130 |
}
|
131 |
.status-box {
|
132 |
-
background: #
|
133 |
-
border: 1px solid #
|
134 |
-
border-radius:
|
135 |
-
padding:
|
136 |
-
height:
|
137 |
-
|
|
|
138 |
}
|
139 |
-
.button {
|
140 |
-
background: #
|
141 |
color: white !important;
|
142 |
-
border
|
143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
}
|
145 |
-
.
|
146 |
-
|
|
|
|
|
|
|
147 |
}
|
148 |
"""
|
149 |
-
|
150 |
-
with gr.Blocks(css=css, title="Image Downloader") as demo:
|
151 |
-
gr.Markdown("<h1
|
152 |
-
gr.Markdown("Tải ảnh từ Google Images
|
153 |
|
154 |
with gr.Row(elem_classes="container"):
|
155 |
-
with gr.Column(scale=1):
|
156 |
-
gr.
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
with gr.Column(scale=2):
|
165 |
-
gr.
|
166 |
-
|
167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
def run_download(query, target, max_term, folder, zip_opt):
|
|
|
|
|
|
|
170 |
status, zip_path = start_download(query, int(target), int(max_term), folder, zip_opt)
|
171 |
if zip_path:
|
172 |
return status, gr.File.update(value=zip_path, visible=True)
|
173 |
return status, gr.File.update(visible=False)
|
174 |
-
|
175 |
submit_btn.click(
|
176 |
fn=run_download,
|
177 |
inputs=[initial_query, target_images, max_per_term, save_folder, zip_files],
|
178 |
outputs=[output_status, output_file]
|
179 |
)
|
180 |
-
|
181 |
return demo
|
182 |
|
183 |
-
# Chạy ứng dụng
|
184 |
if __name__ == "__main__":
|
185 |
interface = create_interface()
|
186 |
interface.launch()
|
|
|
8 |
import gradio as gr
|
9 |
import shutil
|
10 |
|
11 |
+
# Các hàm phụ trợ giữ nguyên
|
12 |
def get_google_suggestions(query):
|
13 |
url = f"http://suggestqueries.google.com/complete/search?client=firefox&q={query}"
|
14 |
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}
|
|
|
18 |
except:
|
19 |
return []
|
20 |
|
|
|
21 |
def download_images(search_term, num_images_per_term, save_folder, status_callback):
|
22 |
if not os.path.exists(save_folder):
|
23 |
os.makedirs(save_folder)
|
|
|
53 |
|
54 |
return count
|
55 |
|
|
|
56 |
def zip_folder(folder_path):
|
57 |
output_zip = os.path.join(os.path.dirname(folder_path), "downloaded_images")
|
58 |
try:
|
|
|
61 |
except Exception as e:
|
62 |
return None, f"Lỗi khi nén: {str(e)}"
|
63 |
|
|
|
64 |
def start_download(initial_query, target_images, max_per_term, save_folder, zip_files):
|
65 |
status_log = []
|
66 |
|
|
|
105 |
|
106 |
return "\n".join(status_log), zip_file_path
|
107 |
|
108 |
+
# Giao diện Gradio cải tiến
|
109 |
def create_interface():
|
110 |
css = """
|
111 |
.container {
|
112 |
+
max-width: 1000px;
|
113 |
+
margin: 0 auto;
|
114 |
+
background: #ffffff;
|
115 |
+
padding: 30px;
|
116 |
+
border-radius: 15px;
|
117 |
+
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
|
118 |
}
|
119 |
h1 {
|
120 |
+
color: #1a73e8;
|
121 |
text-align: center;
|
122 |
+
font-family: 'Segoe UI', sans-serif;
|
123 |
+
margin-bottom: 20px;
|
124 |
}
|
125 |
+
.input-group {
|
126 |
+
background: #f8f9fa;
|
127 |
+
padding: 15px;
|
128 |
+
border-radius: 10px;
|
129 |
+
margin-bottom: 15px;
|
130 |
}
|
131 |
.status-box {
|
132 |
+
background: #f1f3f5;
|
133 |
+
border: 1px solid #e9ecef;
|
134 |
+
border-radius: 10px;
|
135 |
+
padding: 15px;
|
136 |
+
height: 350px;
|
137 |
+
font-family: 'Courier New', monospace;
|
138 |
+
font-size: 14px;
|
139 |
}
|
140 |
+
.button-primary {
|
141 |
+
background: #1a73e8 !important;
|
142 |
color: white !important;
|
143 |
+
border: none !important;
|
144 |
+
border-radius: 8px !important;
|
145 |
+
padding: 12px 25px !important;
|
146 |
+
font-weight: bold !important;
|
147 |
+
transition: all 0.3s !important;
|
148 |
+
}
|
149 |
+
.button-primary:hover {
|
150 |
+
background: #1557b0 !important;
|
151 |
+
transform: translateY(-2px) !important;
|
152 |
}
|
153 |
+
.footer {
|
154 |
+
text-align: center;
|
155 |
+
color: #666;
|
156 |
+
font-size: 12px;
|
157 |
+
margin-top: 20px;
|
158 |
}
|
159 |
"""
|
160 |
+
|
161 |
+
with gr.Blocks(css=css, title="Image Downloader Pro") as demo:
|
162 |
+
gr.Markdown("<h1>📷 Image Downloader Pro</h1>")
|
163 |
+
gr.Markdown("<p style='text-align: center; color: #666;'>Tải ảnh từ Google Images dễ dàng với giao diện thân thiện</p>")
|
164 |
|
165 |
with gr.Row(elem_classes="container"):
|
166 |
+
with gr.Column(scale=1, min_width=300):
|
167 |
+
with gr.Group(elem_classes="input-group"):
|
168 |
+
gr.Markdown("#### Cài đặt tải")
|
169 |
+
initial_query = gr.Textbox(
|
170 |
+
label="Từ khóa ban đầu",
|
171 |
+
value="free images",
|
172 |
+
placeholder="Nhập từ khóa tìm kiếm..."
|
173 |
+
)
|
174 |
+
target_images = gr.Slider(
|
175 |
+
label="Số lượng ảnh",
|
176 |
+
minimum=1,
|
177 |
+
maximum=10000,
|
178 |
+
value=100,
|
179 |
+
step=1
|
180 |
+
)
|
181 |
+
max_per_term = gr.Slider(
|
182 |
+
label="Ảnh tối đa mỗi từ khóa",
|
183 |
+
minimum=1,
|
184 |
+
maximum=50,
|
185 |
+
value=20,
|
186 |
+
step=1
|
187 |
+
)
|
188 |
+
save_folder = gr.Textbox(
|
189 |
+
label="Thư mục lưu",
|
190 |
+
value=os.path.join(os.getcwd(), "free_images"),
|
191 |
+
placeholder="Đường dẫn thư mục..."
|
192 |
+
)
|
193 |
+
folder_button = gr.Button("Chọn thư mục", variant="secondary")
|
194 |
+
zip_files = gr.Checkbox(label="Nén thành ZIP sau khi tải", value=True)
|
195 |
+
submit_btn = gr.Button("Bắt đầu tải", elem_classes="button-primary")
|
196 |
|
197 |
with gr.Column(scale=2):
|
198 |
+
with gr.Group(elem_classes="input-group"):
|
199 |
+
gr.Markdown("#### Trạng thái")
|
200 |
+
output_status = gr.Textbox(
|
201 |
+
label="Nhật ký tải",
|
202 |
+
lines=15,
|
203 |
+
interactive=False,
|
204 |
+
elem_classes="status-box"
|
205 |
+
)
|
206 |
+
output_file = gr.File(label="File ZIP (nếu có)", visible=False)
|
207 |
|
208 |
+
gr.Markdown("<p class='footer'>Powered by Gradio & xAI</p>")
|
209 |
+
|
210 |
+
# Xử lý chọn thư mục
|
211 |
+
def update_folder_path():
|
212 |
+
folder = gr.FileExplorer(glob="*/", root_dir=os.getcwd())
|
213 |
+
return folder
|
214 |
+
|
215 |
+
folder_button.click(
|
216 |
+
fn=update_folder_path,
|
217 |
+
outputs=save_folder
|
218 |
+
)
|
219 |
+
|
220 |
+
# Xử lý tải
|
221 |
def run_download(query, target, max_term, folder, zip_opt):
|
222 |
+
if not folder:
|
223 |
+
return "Vui lòng chọn thư mục lưu!", gr.File.update(visible=False)
|
224 |
+
|
225 |
status, zip_path = start_download(query, int(target), int(max_term), folder, zip_opt)
|
226 |
if zip_path:
|
227 |
return status, gr.File.update(value=zip_path, visible=True)
|
228 |
return status, gr.File.update(visible=False)
|
229 |
+
|
230 |
submit_btn.click(
|
231 |
fn=run_download,
|
232 |
inputs=[initial_query, target_images, max_per_term, save_folder, zip_files],
|
233 |
outputs=[output_status, output_file]
|
234 |
)
|
235 |
+
|
236 |
return demo
|
237 |
|
|
|
238 |
if __name__ == "__main__":
|
239 |
interface = create_interface()
|
240 |
interface.launch()
|