Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,6 @@ import time
|
|
7 |
import random
|
8 |
import gradio as gr
|
9 |
import shutil
|
10 |
-
import threading
|
11 |
|
12 |
# Hàm lấy gợi ý tìm kiếm từ Google
|
13 |
def get_google_suggestions(query):
|
@@ -60,12 +59,12 @@ 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
|
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):
|
@@ -100,36 +99,83 @@ def start_download(initial_query, target_images, max_per_term, save_folder):
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
# Giao diện Gradio
|
110 |
def create_interface():
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
-
with gr.Row():
|
116 |
-
with gr.Column():
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
|
|
|
|
|
|
121 |
|
122 |
-
with gr.Column():
|
123 |
-
|
124 |
-
|
|
|
125 |
|
126 |
-
def run_download(query, target, max_term, folder):
|
127 |
-
|
|
|
|
|
|
|
128 |
|
129 |
submit_btn.click(
|
130 |
fn=run_download,
|
131 |
-
inputs=[initial_query, target_images, max_per_term, save_folder],
|
132 |
-
outputs=
|
133 |
)
|
134 |
|
135 |
return demo
|
|
|
7 |
import random
|
8 |
import gradio as gr
|
9 |
import shutil
|
|
|
10 |
|
11 |
# Hàm lấy gợi ý tìm kiếm từ Google
|
12 |
def get_google_suggestions(query):
|
|
|
59 |
output_zip = os.path.join(os.path.dirname(folder_path), "downloaded_images")
|
60 |
try:
|
61 |
shutil.make_archive(output_zip, 'zip', folder_path)
|
62 |
+
return output_zip + ".zip", "Đã nén thành công"
|
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 |
|
70 |
def status_callback(message):
|
|
|
99 |
time.sleep(random.uniform(2, 5))
|
100 |
|
101 |
status_callback(f"Hoàn tất! Đã tải {total_downloaded} ảnh.")
|
|
|
|
|
|
|
102 |
|
103 |
+
zip_file_path = None
|
104 |
+
if zip_files:
|
105 |
+
status_callback("Đang nén thư mục...")
|
106 |
+
zip_file_path, zip_message = zip_folder(save_folder)
|
107 |
+
status_callback(zip_message)
|
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 |
+
background: linear-gradient(135deg, #e0eafc, #cfdef3);
|
116 |
+
padding: 20px;
|
117 |
+
border-radius: 10px;
|
118 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
|
119 |
+
}
|
120 |
+
h1 {
|
121 |
+
color: #2c3e50;
|
122 |
+
text-align: center;
|
123 |
+
font-family: Arial, sans-serif;
|
124 |
+
}
|
125 |
+
.input-box {
|
126 |
+
background: white;
|
127 |
+
border-radius: 5px;
|
128 |
+
padding: 10px;
|
129 |
+
margin: 5px 0;
|
130 |
+
}
|
131 |
+
.status-box {
|
132 |
+
background: #f9f9f9;
|
133 |
+
border: 1px solid #ddd;
|
134 |
+
border-radius: 5px;
|
135 |
+
padding: 10px;
|
136 |
+
height: 300px;
|
137 |
+
overflow-y: auto;
|
138 |
+
}
|
139 |
+
.button {
|
140 |
+
background: #3498db !important;
|
141 |
+
color: white !important;
|
142 |
+
border-radius: 5px !important;
|
143 |
+
padding: 10px 20px !important;
|
144 |
+
}
|
145 |
+
.button:hover {
|
146 |
+
background: #2980b9 !important;
|
147 |
+
}
|
148 |
+
"""
|
149 |
+
|
150 |
+
with gr.Blocks(css=css, title="Image Downloader") as demo:
|
151 |
+
gr.Markdown("<h1>📸 Image Downloader</h1>")
|
152 |
+
gr.Markdown("Tải ảnh từ Google Images và tùy chọn nén thành file zip.")
|
153 |
|
154 |
+
with gr.Row(elem_classes="container"):
|
155 |
+
with gr.Column(scale=1):
|
156 |
+
gr.Markdown("### Cài đặt", elem_classes="input-box")
|
157 |
+
initial_query = gr.Textbox(label="Từ khóa ban đầu", value="free images", elem_classes="input-box")
|
158 |
+
target_images = gr.Number(label="Số lượng ảnh", value=10000, precision=0, elem_classes="input-box")
|
159 |
+
max_per_term = gr.Number(label="Ảnh tối đa mỗi từ khóa", value=20, precision=0, elem_classes="input-box")
|
160 |
+
save_folder = gr.Textbox(label="Thư mục lưu", value="free_images", elem_classes="input-box")
|
161 |
+
zip_files = gr.Checkbox(label="Nén thành file zip", value=True, elem_classes="input-box")
|
162 |
+
submit_btn = gr.Button("Bắt đầu tải", elem_classes="button")
|
163 |
|
164 |
+
with gr.Column(scale=2):
|
165 |
+
gr.Markdown("### Trạng thái", elem_classes="input-box")
|
166 |
+
output_status = gr.Textbox(label="", lines=15, interactive=False, elem_classes="status-box")
|
167 |
+
output_file = gr.File(label="Tải file zip (nếu có)", visible=False)
|
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
|