YanmHa commited on
Commit
a9b20f4
·
verified ·
1 Parent(s): 9022abf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -45
app.py CHANGED
@@ -1,50 +1,40 @@
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, _, 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__":
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()