Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,40 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import shutil
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
shutil.
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 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 |
-
|
| 50 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import shutil
|
| 4 |
+
import tempfile
|
| 5 |
+
|
| 6 |
+
# 目标保存路径
|
| 7 |
+
SAVE_DIR = "/data/images/images"
|
| 8 |
+
|
| 9 |
+
def upload_folder(folder_zip):
|
| 10 |
+
if folder_zip is None:
|
| 11 |
+
return "请上传一个文件夹(打包为 zip 文件)"
|
| 12 |
+
|
| 13 |
+
# 创建一个临时目录解压上传内容
|
| 14 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 15 |
+
shutil.unpack_archive(folder_zip.name, tmpdir)
|
| 16 |
+
|
| 17 |
+
# 遍历所有文件并保存到目标路径
|
| 18 |
+
for root, dirs, files in os.walk(tmpdir):
|
| 19 |
+
for file in files:
|
| 20 |
+
src_file_path = os.path.join(root, file)
|
| 21 |
+
|
| 22 |
+
# 保持相对路径结构
|
| 23 |
+
rel_path = os.path.relpath(src_file_path, tmpdir)
|
| 24 |
+
dst_file_path = os.path.join(SAVE_DIR, rel_path)
|
| 25 |
+
|
| 26 |
+
os.makedirs(os.path.dirname(dst_file_path), exist_ok=True)
|
| 27 |
+
shutil.copy2(src_file_path, dst_file_path)
|
| 28 |
+
|
| 29 |
+
return f"上传完成,文件已保存到 {SAVE_DIR}"
|
| 30 |
+
|
| 31 |
+
# Gradio 界面
|
| 32 |
+
demo = gr.Interface(
|
| 33 |
+
fn=upload_folder,
|
| 34 |
+
inputs=gr.File(label="上传打包的文件夹(.zip 格式)", file_types=[".zip"]),
|
| 35 |
+
outputs="text",
|
| 36 |
+
title="上传文件夹内容到服务器",
|
| 37 |
+
description="上传一个包含图像或其他文件的 zip 文件,内容将被保存到 `/data/images/images` 目录下,保留原始子目录结构。"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
+
demo.launch()
|
|
|