Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
for file in files:
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
total_count += count
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
output.append(f" - {relative_path}: {count} 张")
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Gradio 接口
|
| 31 |
-
iface = gr.Interface(
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
| 37 |
if __name__ == "__main__":
|
| 38 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import shutil
|
| 4 |
+
from zipfile import ZipFile
|
| 5 |
|
| 6 |
+
# 根目录和目标文件名
|
| 7 |
+
BASE_DIR = "/data/images/images/NeuroPictor"
|
| 8 |
+
TARGET_FILENAMES = {"10046_0.jpg", "59194_0.jpg"}
|
| 9 |
+
TEMP_DIR = "/tmp/npictor_download"
|
| 10 |
+
ZIP_OUTPUT = "/tmp/npictor_download.zip"
|
| 11 |
|
| 12 |
+
def collect_target_images():
|
| 13 |
+
# 清理临时目录
|
| 14 |
+
if os.path.exists(TEMP_DIR):
|
| 15 |
+
shutil.rmtree(TEMP_DIR)
|
| 16 |
+
os.makedirs(TEMP_DIR, exist_ok=True)
|
| 17 |
|
| 18 |
+
found = []
|
| 19 |
+
|
| 20 |
+
# 遍历子目录查找目标文件
|
| 21 |
+
for root, dirs, files in os.walk(BASE_DIR):
|
| 22 |
for file in files:
|
| 23 |
+
if file in TARGET_FILENAMES:
|
| 24 |
+
src_path = os.path.join(root, file)
|
| 25 |
+
dst_path = os.path.join(TEMP_DIR, file)
|
| 26 |
+
shutil.copy(src_path, dst_path)
|
| 27 |
+
found.append(file)
|
|
|
|
| 28 |
|
| 29 |
+
# 压缩成 zip 文件
|
| 30 |
+
with ZipFile(ZIP_OUTPUT, 'w') as zipf:
|
| 31 |
+
for file in os.listdir(TEMP_DIR):
|
| 32 |
+
zipf.write(os.path.join(TEMP_DIR, file), arcname=file)
|
|
|
|
| 33 |
|
| 34 |
+
if found:
|
| 35 |
+
return ZIP_OUTPUT
|
| 36 |
+
else:
|
| 37 |
+
return "未找到目标图像文件。请确认文件是否存在于子目录中。"
|
| 38 |
|
| 39 |
# Gradio 接口
|
| 40 |
+
iface = gr.Interface(
|
| 41 |
+
fn=collect_target_images,
|
| 42 |
+
inputs=[],
|
| 43 |
+
outputs=gr.File(label="下载压缩包"),
|
| 44 |
+
title="NeuroPictor 图像下载器",
|
| 45 |
+
description="下载四个子文件夹下的 10046_0.jpg 和 59194_0.jpg 文件"
|
| 46 |
+
)
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
| 49 |
iface.launch()
|