File size: 1,800 Bytes
42b8bfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from huggingface_hub import HfApi
import os

# 设置 Hugging Face Space 的信息
space_id = "fengmiguoji/dify"  # 您的 Space ID
local_dify_dir = "C:\\Users\\Administrator\\Downloads\\dify-main\\dify-main"  # 您的本地 Dify 项目目录的绝对路径

# 创建 HfApi 客户端
api = HfApi()

def upload_directory_to_space(local_dir, space_id):
    """将本地目录上传到 Hugging Face Space 的根目录."""
    for root, dirs, files in os.walk(local_dir):
        for file in files:
            local_path = os.path.join(root, file)
            # 计算文件在 Space 中的相对路径
            relative_path = os.path.relpath(local_path, local_dify_dir)

            #构造huggingface上的路径
            repo_path = relative_path

            print(f"Uploading {local_path} to {repo_path} in Space {space_id}")
            try:
                api.upload_file(
                    path_or_fileobj=local_path,
                    path_in_repo=repo_path,
                    repo_id=space_id,
                    repo_type="space",
                )
            except Exception as e:
                print(f"Error uploading {local_path}: {e}")

        for dir in dirs:
             local_path = os.path.join(root, dir)
             relative_path = os.path.relpath(local_path, local_dify_dir)
             repo_path = relative_path
             try:
                api.create_folder(
                    repo_id=space_id,
                    folder_path=repo_path,
                    repo_type="space",
                )
             except Exception as e:
                print(f"Error creating folder {repo_path}: {e}")
    print("upload finish")
# 上传 Dify 项目目录
upload_directory_to_space(local_dify_dir, space_id)