rapacious commited on
Commit
ad8845e
·
verified ·
1 Parent(s): 009e35d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -114
app.py CHANGED
@@ -5,8 +5,7 @@ import urllib.request
5
  import json
6
  import time
7
  import random
8
- import tkinter as tk
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 thành file zip
60
- def zip_folder(folder_path, output_zip):
 
61
  try:
62
  shutil.make_archive(output_zip, 'zip', folder_path)
63
- return True
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, save_folder, max_per_term, status_text):
69
- def download_thread():
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
- thread = threading.Thread(target=download_thread)
109
- thread.start()
110
-
111
- # Giao diện GUI
112
- class ImageDownloaderApp:
113
- def __init__(self, root):
114
- self.root = root
115
- self.root.title("Image Downloader")
116
- self.root.geometry("600x500")
117
-
118
- # Nhãn ô nhập từ khóa
119
- tk.Label(root, text="Từ khóa ban đầu:").pack(pady=5)
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
- # Số ảnh tối đa mỗi từ khóa
131
- tk.Label(root, text="Số ảnh tối đa mỗi từ khóa:").pack(pady=5)
132
- self.max_per_term_entry = tk.Entry(root, width=10)
133
- self.max_per_term_entry.insert(0, "20")
134
- self.max_per_term_entry.pack()
135
 
136
- # Thư mục lưu
137
- tk.Label(root, text="Thư mục lưu:").pack(pady=5)
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
- # Nút bắt đầu
145
- self.start_button = tk.Button(root, text="Bắt đầu tải", command=self.start_download)
146
- self.start_button.pack(pady=10)
147
 
148
- # Khu vực hiển thị trạng thái
149
- tk.Label(root, text="Trạng thái:").pack(pady=5)
150
- self.status_text = tk.Text(root, height=15, width=70)
151
- self.status_text.pack()
152
 
153
- def choose_folder(self):
154
- folder = filedialog.askdirectory()
155
- if folder:
156
- self.folder_var.set(folder)
157
 
158
- def update_status(self, message):
159
- self.status_text.insert(tk.END, f"{message}\n")
160
- self.status_text.see(tk.END)
161
- self.root.update_idletasks()
162
 
163
- def start_download(self):
164
- try:
165
- initial_query = self.query_entry.get()
166
- target_images = int(self.target_entry.get())
167
- max_per_term = int(self.max_per_term_entry.get())
168
- save_folder = self.folder_var.get()
169
-
170
- if not initial_query or target_images <= 0 or max_per_term <= 0 or not save_folder:
171
- messagebox.showerror("Lỗi", "Vui lòng nhập đầy đủ và hợp lệ các thông tin!")
172
- return
173
-
174
- self.start_button.config(state="disabled")
175
- self.status_text.delete(1.0, tk.END)
176
- start_download(initial_query, target_images, save_folder, max_per_term, self.update_status)
177
 
178
- except ValueError:
179
- messagebox.showerror("Lỗi", "Số lượng phải là số nguyên!")
180
- finally:
181
- self.start_button.config(state="normal")
 
 
 
 
 
 
 
 
 
 
182
 
183
  # Chạy ứng dụng
184
  if __name__ == "__main__":
185
- root = tk.Tk()
186
- app = ImageDownloaderApp(root)
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()