Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,8 +5,7 @@ import urllib.request
|
|
| 5 |
import json
|
| 6 |
import time
|
| 7 |
import random
|
| 8 |
-
import
|
| 9 |
-
from tkinter import ttk, messagebox, filedialog
|
| 10 |
import shutil
|
| 11 |
import threading
|
| 12 |
|
|
@@ -56,132 +55,86 @@ def download_images(search_term, num_images_per_term, save_folder, status_callba
|
|
| 56 |
|
| 57 |
return count
|
| 58 |
|
| 59 |
-
# Hàm nén thư mục
|
| 60 |
-
def zip_folder(folder_path
|
|
|
|
| 61 |
try:
|
| 62 |
shutil.make_archive(output_zip, 'zip', folder_path)
|
| 63 |
-
return
|
| 64 |
except Exception as e:
|
| 65 |
-
return str(e)
|
| 66 |
|
| 67 |
# Hàm chính tải ảnh
|
| 68 |
-
def start_download(initial_query, target_images,
|
| 69 |
-
|
| 70 |
-
total_downloaded = 0
|
| 71 |
-
current_query = initial_query
|
| 72 |
-
used_queries = set()
|
| 73 |
-
|
| 74 |
-
while total_downloaded < target_images:
|
| 75 |
-
suggestions = get_google_suggestions(current_query)
|
| 76 |
-
if not suggestions:
|
| 77 |
-
status_text(f"Hết gợi ý, dừng lại.")
|
| 78 |
-
break
|
| 79 |
-
|
| 80 |
-
available_suggestions = [s for s in suggestions if s not in used_queries]
|
| 81 |
-
if not available_suggestions:
|
| 82 |
-
status_text("Hết gợi ý mới, dừng lại.")
|
| 83 |
-
break
|
| 84 |
-
|
| 85 |
-
current_query = random.choice(available_suggestions)
|
| 86 |
-
used_queries.add(current_query)
|
| 87 |
-
|
| 88 |
-
remaining = target_images - total_downloaded
|
| 89 |
-
images_to_download = min(max_per_term, remaining)
|
| 90 |
-
|
| 91 |
-
status_text(f"Tìm kiếm: {current_query}")
|
| 92 |
-
downloaded = download_images(current_query, images_to_download, save_folder, status_text)
|
| 93 |
-
total_downloaded += downloaded
|
| 94 |
-
|
| 95 |
-
status_text(f"Tổng: {total_downloaded}/{target_images}")
|
| 96 |
-
time.sleep(random.uniform(2, 5))
|
| 97 |
-
|
| 98 |
-
status_text(f"Hoàn tất! Đã tải {total_downloaded} ảnh.")
|
| 99 |
-
|
| 100 |
-
# Nén thư mục
|
| 101 |
-
status_text("Đang nén thư mục...")
|
| 102 |
-
zip_result = zip_folder(save_folder, os.path.join(os.path.dirname(save_folder), "downloaded_images"))
|
| 103 |
-
if zip_result is True:
|
| 104 |
-
status_text("Đã nén thành công: downloaded_images.zip")
|
| 105 |
-
else:
|
| 106 |
-
status_text(f"Lỗi khi nén: {zip_result}")
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
self.query_entry = tk.Entry(root, width=40)
|
| 121 |
-
self.query_entry.insert(0, "free images")
|
| 122 |
-
self.query_entry.pack()
|
| 123 |
-
|
| 124 |
-
# Số lượng ảnh mục tiêu
|
| 125 |
-
tk.Label(root, text="Số lượng ảnh cần tải:").pack(pady=5)
|
| 126 |
-
self.target_entry = tk.Entry(root, width=10)
|
| 127 |
-
self.target_entry.insert(0, "10000")
|
| 128 |
-
self.target_entry.pack()
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
self.max_per_term_entry.pack()
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
self.folder_var = tk.StringVar(value="free_images")
|
| 139 |
-
folder_frame = tk.Frame(root)
|
| 140 |
-
folder_frame.pack()
|
| 141 |
-
tk.Entry(folder_frame, textvariable=self.folder_var, width=30).pack(side=tk.LEFT)
|
| 142 |
-
tk.Button(folder_frame, text="Chọn", command=self.choose_folder).pack(side=tk.LEFT, padx=5)
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
self.start_button.pack(pady=10)
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
self.status_text.pack()
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
if folder:
|
| 156 |
-
self.folder_var.set(folder)
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
# Chạy ứng dụng
|
| 184 |
if __name__ == "__main__":
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
root.mainloop()
|
|
|
|
| 5 |
import json
|
| 6 |
import time
|
| 7 |
import random
|
| 8 |
+
import gradio as gr
|
|
|
|
| 9 |
import shutil
|
| 10 |
import threading
|
| 11 |
|
|
|
|
| 55 |
|
| 56 |
return count
|
| 57 |
|
| 58 |
+
# Hàm nén thư mục
|
| 59 |
+
def zip_folder(folder_path):
|
| 60 |
+
output_zip = os.path.join(os.path.dirname(folder_path), "downloaded_images")
|
| 61 |
try:
|
| 62 |
shutil.make_archive(output_zip, 'zip', folder_path)
|
| 63 |
+
return f"Đã nén thành công: {output_zip}.zip"
|
| 64 |
except Exception as e:
|
| 65 |
+
return f"Lỗi khi nén: {str(e)}"
|
| 66 |
|
| 67 |
# Hàm chính tải ảnh
|
| 68 |
+
def start_download(initial_query, target_images, max_per_term, save_folder):
|
| 69 |
+
status_log = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
def status_callback(message):
|
| 72 |
+
status_log.append(message)
|
| 73 |
+
|
| 74 |
+
total_downloaded = 0
|
| 75 |
+
current_query = initial_query
|
| 76 |
+
used_queries = set()
|
| 77 |
+
|
| 78 |
+
while total_downloaded < target_images:
|
| 79 |
+
suggestions = get_google_suggestions(current_query)
|
| 80 |
+
if not suggestions:
|
| 81 |
+
status_callback("Hết gợi ý, dừng lại.")
|
| 82 |
+
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
available_suggestions = [s for s in suggestions if s not in used_queries]
|
| 85 |
+
if not available_suggestions:
|
| 86 |
+
status_callback("Hết gợi ý mới, dừng lại.")
|
| 87 |
+
break
|
|
|
|
| 88 |
|
| 89 |
+
current_query = random.choice(available_suggestions)
|
| 90 |
+
used_queries.add(current_query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
remaining = target_images - total_downloaded
|
| 93 |
+
images_to_download = min(max_per_term, remaining)
|
|
|
|
| 94 |
|
| 95 |
+
status_callback(f"Tìm kiếm: {current_query}")
|
| 96 |
+
downloaded = download_images(current_query, images_to_download, save_folder, status_callback)
|
| 97 |
+
total_downloaded += downloaded
|
|
|
|
| 98 |
|
| 99 |
+
status_callback(f"Tổng: {total_downloaded}/{target_images}")
|
| 100 |
+
time.sleep(random.uniform(2, 5))
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
status_callback(f"Hoàn tất! Đã tải {total_downloaded} ảnh.")
|
| 103 |
+
status_callback("Đang nén thư mục...")
|
| 104 |
+
zip_result = zip_folder(save_folder)
|
| 105 |
+
status_callback(zip_result)
|
| 106 |
|
| 107 |
+
return "\n".join(status_log)
|
| 108 |
+
|
| 109 |
+
# Giao diện Gradio
|
| 110 |
+
def create_interface():
|
| 111 |
+
with gr.Blocks(title="Image Downloader") as demo:
|
| 112 |
+
gr.Markdown("# Image Downloader")
|
| 113 |
+
gr.Markdown("Tải ảnh từ Google Images và nén thành file zip.")
|
| 114 |
+
|
| 115 |
+
with gr.Row():
|
| 116 |
+
with gr.Column():
|
| 117 |
+
initial_query = gr.Textbox(label="Từ khóa ban đầu", value="free images")
|
| 118 |
+
target_images = gr.Number(label="Số lượng ảnh cần tải", value=10000, precision=0)
|
| 119 |
+
max_per_term = gr.Number(label="Số ảnh tối đa mỗi từ khóa", value=20, precision=0)
|
| 120 |
+
save_folder = gr.Textbox(label="Thư mục lưu", value="free_images")
|
| 121 |
|
| 122 |
+
with gr.Column():
|
| 123 |
+
output = gr.Textbox(label="Trạng thái", lines=20, interactive=False)
|
| 124 |
+
submit_btn = gr.Button("Bắt đầu tải")
|
| 125 |
+
|
| 126 |
+
def run_download(query, target, max_term, folder):
|
| 127 |
+
return start_download(query, int(target), int(max_term), folder)
|
| 128 |
+
|
| 129 |
+
submit_btn.click(
|
| 130 |
+
fn=run_download,
|
| 131 |
+
inputs=[initial_query, target_images, max_per_term, save_folder],
|
| 132 |
+
outputs=output
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
return demo
|
| 136 |
|
| 137 |
# Chạy ứng dụng
|
| 138 |
if __name__ == "__main__":
|
| 139 |
+
interface = create_interface()
|
| 140 |
+
interface.launch()
|
|
|