-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.py
130 lines (98 loc) · 3.55 KB
/
app.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import os
import gradio as gr
import huggingface_hub as hh
from huggingface_hub import hf_hub_download
from bypy import ByPy
def format_size(bytes, precision=2):
"""
Convert a file size in bytes to a human-readable format like KB, MB, GB, etc.
Huggingface use 1000 not 1024
"""
units = ["B", "KB", "MB", "GB", "TB", "PB"]
size = float(bytes)
index = 0
while size >= 1000 and index < len(units) - 1:
index += 1
size /= 1000
return f"{size:.{precision}f} {units[index]}"
def list_repo_files_info(repo_id,token=None):
data_ls = []
for file in list(hh.list_files_info(repo_id)):
data_ls.append([file.path,format_size(file.size)])
files = [file[0] for file in data_ls]
filenames = gr.Dropdown(choices = files, label="选择文件",allow_custom_value=True,multiselect=True)
return data_ls, filenames
def download_file(repo_id,filenames):
print(filenames)
repo_name = repo_id.replace("/","---")
for filename in filenames:
print(filename)
out = hh.hf_hub_download(repo_id=repo_id,filename=filename,local_dir=f"/data/download/{repo_name}",local_dir_use_symlinks=False,force_download =True)
out_path = f"/data/download/{repo_name}"
return out_path
def upload_by_file(source_path):
bp = ByPy()
dest_path = source_path.split("/")[-1] # 文件名
out = bp.upload(
source_path,
dest_path
)
return out
def upload_by_path(source_path,compress=True):
bp = ByPy()
# 创建目录
dest_path = source_path.split("/")[-1]
# 压缩
if compress:
print("分卷压缩....")
tar_path =f"{os.getcwd()}/tar/{dest_path}"
print(tar_path)
os.system(f"mkdir -p {tar_path}")
os.system(f"zip -s 4000M {tar_path}/{dest_path}.zip {source_path}/*")
source_path = tar_path
print("上传百度云....")
bp.mkdir(dest_path)
# 同步目录
out = bp.syncup(
source_path,
dest_path,
deleteremote=True
)
return out
def app():
with gr.Blocks(
css="""#chatbot {
font-size: 14px;
min-height: 300px;
}"""
) as demo:
gr.Markdown("# Huggingface Download 😀")
with gr.Row():
with gr.Column():
repo = gr.Textbox(placeholder="输入 repo_id")
list_bt = gr.Button("获取文件清单", variant="primary")
filenames = gr.Dropdown(label="选择文件",allow_custom_value=False,multiselect=True)
submit_bt = gr.Button("下载", variant="primary")
gr.Markdown("下载路径:")
output = gr.Markdown(label="对应路径")
with gr.Column():
output_df = gr.Dataframe(headers=['文件名', '文件大小'])
list_bt.click(list_repo_files_info, inputs=repo, outputs=[output_df,filenames])
submit_bt.click(download_file, [repo, filenames], outputs=output)
gr.Markdown("---")
with gr.Row():
bp = ByPy()
key = bp.list()
with gr.Column():
gr.Markdown("# 上传百度云 🐳")
with gr.Row():
with gr.Column():
submit_by = gr.Button("上传", variant="primary")
result = gr.Markdown(label="结果路径")
with gr.Column():
pass
submit_by.click(upload_by_path,output,outputs= result)
return demo
if __name__ == "__main__":
demo = app()
demo.launch(share=True)