Update app.py
Browse files
app.py
CHANGED
|
@@ -3,46 +3,47 @@ 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,
|
| 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,
|
| 26 |
shutil.copy(src_path, dst_path)
|
| 27 |
-
found.append(
|
| 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="
|
| 46 |
)
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
|
|
|
| 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, _, files in os.walk(BASE_DIR):
|
| 22 |
for file in files:
|
| 23 |
if file in TARGET_FILENAMES:
|
| 24 |
+
subfolder_name = os.path.basename(root)
|
| 25 |
+
new_name = f"{subfolder_name}_{file}"
|
| 26 |
src_path = os.path.join(root, file)
|
| 27 |
+
dst_path = os.path.join(TEMP_DIR, new_name)
|
| 28 |
shutil.copy(src_path, dst_path)
|
| 29 |
+
found.append(new_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# 打包为 zip
|
| 32 |
if found:
|
| 33 |
+
with ZipFile(ZIP_OUTPUT, 'w') as zipf:
|
| 34 |
+
for file in os.listdir(TEMP_DIR):
|
| 35 |
+
zipf.write(os.path.join(TEMP_DIR, file), arcname=file)
|
| 36 |
return ZIP_OUTPUT
|
| 37 |
else:
|
| 38 |
+
return "❌ 未找到目标图像文件。请检查文件名或目录结构是否正确。"
|
| 39 |
|
| 40 |
# Gradio 接口
|
| 41 |
iface = gr.Interface(
|
| 42 |
fn=collect_target_images,
|
| 43 |
inputs=[],
|
| 44 |
+
outputs=gr.File(label="点击下载打包图像"),
|
| 45 |
+
title="NeuroPictor 图像文件打包器",
|
| 46 |
+
description="递归查找10046_0.jpg和59194_0.jpg,并重命名为<子文件夹名>_文件名后打包下载"
|
| 47 |
)
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|