Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,8 +7,6 @@ import time
|
|
7 |
import random
|
8 |
import gradio as gr
|
9 |
import shutil
|
10 |
-
import tkinter as tk
|
11 |
-
from tkinter import filedialog
|
12 |
|
13 |
# Các hàm phụ trợ giữ nguyên
|
14 |
def get_google_suggestions(query):
|
@@ -28,7 +26,7 @@ def download_images(search_term, num_images_per_term, save_folder, status_callba
|
|
28 |
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"}
|
29 |
|
30 |
try:
|
31 |
-
response = requests.get(
|
32 |
response.raise_for_status()
|
33 |
except:
|
34 |
status_callback(f"Lỗi truy cập {search_term}")
|
@@ -107,20 +105,6 @@ def start_download(initial_query, target_images, max_per_term, save_folder, zip_
|
|
107 |
|
108 |
return "\n".join(status_log), zip_file_path
|
109 |
|
110 |
-
# Hàm lấy danh sách thư mục con
|
111 |
-
def get_subfolders(root_dir):
|
112 |
-
if not os.path.exists(root_dir):
|
113 |
-
return [root_dir]
|
114 |
-
return [os.path.join(root_dir, d) for d in os.listdir(root_dir) if os.path.isdir(os.path.join(root_dir, d))]
|
115 |
-
|
116 |
-
# Hàm chọn thư mục bằng tkinter
|
117 |
-
def select_folder():
|
118 |
-
root = tk.Tk()
|
119 |
-
root.withdraw()
|
120 |
-
folder = filedialog.askdirectory(initialdir=os.getcwd(), title="Chọn thư mục lưu")
|
121 |
-
root.destroy()
|
122 |
-
return folder if folder else os.path.join(os.getcwd(), "free_images")
|
123 |
-
|
124 |
# Giao diện Gradio
|
125 |
def create_interface():
|
126 |
css = """
|
@@ -190,13 +174,6 @@ def create_interface():
|
|
190 |
font-size: 12px;
|
191 |
margin-top: 20px;
|
192 |
}
|
193 |
-
.folder-explorer {
|
194 |
-
background: #2f3136;
|
195 |
-
padding: 10px;
|
196 |
-
border-radius: 5px;
|
197 |
-
max-height: 150px;
|
198 |
-
overflow-y: auto;
|
199 |
-
}
|
200 |
"""
|
201 |
|
202 |
with gr.Blocks(css=css, title="Image Downloader Pro") as demo:
|
@@ -226,21 +203,11 @@ def create_interface():
|
|
226 |
value=20,
|
227 |
step=1
|
228 |
)
|
229 |
-
|
230 |
-
|
231 |
-
label="Thư mục gốc",
|
232 |
-
value=os.getcwd(),
|
233 |
-
placeholder="Nhập thư mục gốc..."
|
234 |
-
)
|
235 |
-
folder_dropdown = gr.Dropdown(
|
236 |
-
label="Chọn thư mục con",
|
237 |
-
choices=get_subfolders(os.getcwd()),
|
238 |
value=os.path.join(os.getcwd(), "free_images"),
|
239 |
-
|
240 |
)
|
241 |
-
with gr.Row():
|
242 |
-
refresh_button = gr.Button("Làm mới", elem_classes="button-secondary")
|
243 |
-
tk_folder_button = gr.Button("Chọn bằng hộp thoại", elem_classes="button-secondary")
|
244 |
zip_files = gr.Checkbox(label="Nén thành ZIP sau khi tải", value=True)
|
245 |
submit_btn = gr.Button("Bắt đầu tải", elem_classes="button-primary")
|
246 |
|
@@ -257,29 +224,10 @@ def create_interface():
|
|
257 |
|
258 |
gr.Markdown("<p class='footer'>Powered by Gradio & xAI</p>")
|
259 |
|
260 |
-
# Hàm cập nhật danh sách thư mục con
|
261 |
-
def update_folders(root_dir):
|
262 |
-
subfolders = get_subfolders(root_dir)
|
263 |
-
return gr.Dropdown.update(choices=subfolders, value=subfolders[0] if subfolders else root_dir)
|
264 |
-
|
265 |
-
# Sự kiện làm mới danh sách thư mục
|
266 |
-
refresh_button.click(
|
267 |
-
fn=update_folders,
|
268 |
-
inputs=root_folder,
|
269 |
-
outputs=folder_dropdown
|
270 |
-
)
|
271 |
-
|
272 |
-
# Sự kiện chọn thư mục bằng tkinter
|
273 |
-
tk_folder_button.click(
|
274 |
-
fn=select_folder,
|
275 |
-
inputs=[],
|
276 |
-
outputs=folder_dropdown
|
277 |
-
)
|
278 |
-
|
279 |
# Xử lý tải
|
280 |
def run_download(query, target, max_term, folder, zip_opt):
|
281 |
if not folder:
|
282 |
-
return "Vui lòng
|
283 |
|
284 |
status, zip_path = start_download(query, int(target), int(max_term), folder, zip_opt)
|
285 |
if zip_path:
|
@@ -288,7 +236,7 @@ def create_interface():
|
|
288 |
|
289 |
submit_btn.click(
|
290 |
fn=run_download,
|
291 |
-
inputs=[initial_query, target_images, max_per_term,
|
292 |
outputs=[output_status, output_file]
|
293 |
)
|
294 |
|
|
|
7 |
import random
|
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):
|
|
|
26 |
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"}
|
27 |
|
28 |
try:
|
29 |
+
response = requests.get(search_url, headers=headers, timeout=10)
|
30 |
response.raise_for_status()
|
31 |
except:
|
32 |
status_callback(f"Lỗi truy cập {search_term}")
|
|
|
105 |
|
106 |
return "\n".join(status_log), zip_file_path
|
107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
# Giao diện Gradio
|
109 |
def create_interface():
|
110 |
css = """
|
|
|
174 |
font-size: 12px;
|
175 |
margin-top: 20px;
|
176 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
"""
|
178 |
|
179 |
with gr.Blocks(css=css, title="Image Downloader Pro") as demo:
|
|
|
203 |
value=20,
|
204 |
step=1
|
205 |
)
|
206 |
+
save_folder = gr.Textbox(
|
207 |
+
label="Thư mục lưu",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
208 |
value=os.path.join(os.getcwd(), "free_images"),
|
209 |
+
placeholder="Đường dẫn thư mục..."
|
210 |
)
|
|
|
|
|
|
|
211 |
zip_files = gr.Checkbox(label="Nén thành ZIP sau khi tải", value=True)
|
212 |
submit_btn = gr.Button("Bắt đầu tải", elem_classes="button-primary")
|
213 |
|
|
|
224 |
|
225 |
gr.Markdown("<p class='footer'>Powered by Gradio & xAI</p>")
|
226 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
227 |
# Xử lý tải
|
228 |
def run_download(query, target, max_term, folder, zip_opt):
|
229 |
if not folder:
|
230 |
+
return "Vui lòng nhập thư mục lưu!", gr.File.update(visible=False)
|
231 |
|
232 |
status, zip_path = start_download(query, int(target), int(max_term), folder, zip_opt)
|
233 |
if zip_path:
|
|
|
236 |
|
237 |
submit_btn.click(
|
238 |
fn=run_download,
|
239 |
+
inputs=[initial_query, target_images, max_per_term, save_folder, zip_files],
|
240 |
outputs=[output_status, output_file]
|
241 |
)
|
242 |
|