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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -26
app.py CHANGED
@@ -1,40 +1,32 @@
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()
 
 
1
  import os
2
+ import gradio as gr
 
 
 
 
3
 
4
+ ROOT_DIR = "/data/images/images"
 
 
5
 
6
+ def get_directory_structure(root_path):
7
+ if not os.path.exists(root_path):
8
+ return f"路径不存在:{root_path}"
9
 
10
+ tree_lines = []
 
 
 
11
 
12
+ for current_dir, subdirs, files in os.walk(root_path):
13
+ level = current_dir.replace(root_path, "").count(os.sep)
14
+ indent = " " * level
15
+ tree_lines.append(f"{indent}📁 {os.path.basename(current_dir) or '/'}")
16
 
17
+ sub_indent = " " * (level + 1)
18
+ for f in files:
19
+ tree_lines.append(f"{sub_indent}📄 {f}")
20
 
21
+ return "\n".join(tree_lines)
22
 
23
  # Gradio 界面
24
  demo = gr.Interface(
25
+ fn=lambda: get_directory_structure(ROOT_DIR),
26
+ inputs=[],
27
  outputs="text",
28
+ title="查看 /data/images/images 的目录结构",
29
+ description="点击按钮以展示该目录下的文件夹和文件结构"
30
  )
31
 
32
  demo.launch()