WACATW commited on
Commit
2c22388
·
verified ·
1 Parent(s): dc13d87

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +145 -0
app.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import gradio as gr
4
+ from huggingface_hub import HfApi, upload_file, download_file, list_repo_files, delete_file, snapshot_download
5
+
6
+ # 读取环境变量
7
+ HF_TOKEN = os.getenv("HF_TOKEN")
8
+ DATASET_REPO = os.getenv("DATASET_REPO")
9
+ api = HfApi(token=HF_TOKEN)
10
+
11
+ # 仓库内存储路径
12
+ USER_DB_PATH = "user_db.json"
13
+
14
+ # 加载用户账号数据
15
+ def load_user_db():
16
+ try:
17
+ download_file(repo_id=DATASET_REPO, filename=USER_DB_PATH, local_dir="./tmp", repo_type="dataset")
18
+ with open(f"./tmp/{USER_DB_PATH}", "r", encoding="utf-8") as f:
19
+ return json.load(f)
20
+ except:
21
+ return {}
22
+
23
+ # 保存用户账号
24
+ def save_user_db(db):
25
+ with open(f"./tmp/{USER_DB_PATH}", "w", encoding="utf-8") as f:
26
+ json.dump(db, f, ensure_ascii=False)
27
+ upload_file(path_or_fileobj=f"./tmp/{USER_DB_PATH}", path_in_repo=USER_DB_PATH, repo_id=DATASET_REPO, repo_type="dataset)
28
+
29
+ # 注册
30
+ def register(username, pwd):
31
+ db = load_user_db()
32
+ if username in db:
33
+ return "用户名已存在"
34
+ db[username] = {"pwd": pwd}
35
+ save_user_db(db)
36
+ return "注册成功,请登录"
37
+
38
+ # 登录校验
39
+ def login_check(username, pwd):
40
+ db = load_user_db()
41
+ if username not in db:
42
+ return False, "账号不存在"
43
+ if db[username]["pwd"] != pwd:
44
+ return False, "密码错误"
45
+ return True, "登录成功"
46
+
47
+ # 获取用户文件列表
48
+ def get_user_files(username):
49
+ user_dir = f"users/{username}/"
50
+ all_files = list_repo_files(repo_id=DATASET_REPO, repo_type="dataset")
51
+ user_files = []
52
+ for f in all_files:
53
+ if f.startswith(user_dir):
54
+ file_name = f.replace(user_dir, "")
55
+ user_files.append((file_name, f))
56
+ return user_files
57
+
58
+ # 上传文件到用户独立目录
59
+ def upload_user_file(username, file_obj):
60
+ user_dir = f"users/{username}/"
61
+ file_name = os.path.basename(file_obj.name)
62
+ target_path = user_dir + file_name
63
+ upload_file(path_or_fileobj=file_obj, path_in_repo=target_path, repo_id=DATASET_REPO, repo_type="dataset")
64
+ return f"上传成功:{file_name}"
65
+
66
+ # 下载文件
67
+ def download_user_file(username, file_name):
68
+ user_dir = f"users/{username}/"
69
+ remote_path = user_dir + file_name
70
+ local_save = f"./download_{file_name}"
71
+ download_file(repo_id=DATASET_REPO, filename=remote_path, local_dir="./", repo_type="dataset")
72
+ return local_save
73
+
74
+ # 删除文件
75
+ def del_user_file(username, file_name):
76
+ user_dir = f"users/{username}/"
77
+ remote_path = user_dir + file_name
78
+ delete_file(path_in_repo=remote_path, repo_id=DATASET_REPO, repo_type="dataset")
79
+ return f"已删除:{file_name}"
80
+
81
+ # Gradio界面
82
+ with gr.Blocks(title="HF云端网盘") as demo:
83
+ gr.Markdown("# 云端网盘(账号登录+持久文件存储)")
84
+ username = gr.Textbox(label="用户名")
85
+ pwd = gr.Textbox(label="密码", type="password")
86
+ with gr.Row():
87
+ login_btn = gr.Button("登录")
88
+ reg_btn = gr.Button("注册")
89
+ msg = gr.Textbox(label="提示信息", interactive=False)
90
+
91
+ # 网盘区域(登录后显示)
92
+ with gr.Column(visible=False) as disk_panel:
93
+ gr.Markdown("## 文件管理")
94
+ upload_input = gr.File(label="上传文件", file_count="multiple")
95
+ upload_msg = gr.Textbox(label="上传结果", interactive=False)
96
+ file_drop = gr.Dropdown(label="已有文件", choices=[])
97
+ with gr.Row():
98
+ dl_btn = gr.Button("下载选中文件")
99
+ del_btn = gr.Button("删除选中文件")
100
+ dl_out = gr.File(label="下载文件")
101
+
102
+ # 注册事件
103
+ def reg_action(u, p):
104
+ res = register(u, p)
105
+ return res
106
+ reg_btn.click(reg_action, inputs=[username, pwd], outputs=[msg])
107
+
108
+ # 登录事件
109
+ def login_action(u, p):
110
+ ok, tip = login_check(u, p)
111
+ if ok:
112
+ files = get_user_files(u)
113
+ names = [i[0] for i in files]
114
+ return tip, gr.update(visible=True), gr.update(choices=names)
115
+ else:
116
+ return tip, gr.update(visible=False), gr.update(choices=[])
117
+ login_btn.click(login_action, inputs=[username, pwd], outputs=[msg, disk_panel, file_drop])
118
+
119
+ # 上传
120
+ def upload_action(u, files):
121
+ tips = []
122
+ for f in files:
123
+ t = upload_user_file(u, f)
124
+ tips.append(t)
125
+ new_files = get_user_files(u)
126
+ names = [i[0] for i in new_files]
127
+ return "\n".join(tips), gr.update(choices=names)
128
+ upload_input.change(upload_action, inputs=[username, upload_input], outputs=[upload_msg, file_drop])
129
+
130
+ # 下载
131
+ def dl_action(u, fname):
132
+ path = download_user_file(u, fname)
133
+ return path
134
+ dl_btn.click(dl_action, inputs=[username, file_drop], outputs=[dl_out])
135
+
136
+ # 删除
137
+ def del_action(u, fname):
138
+ tip = del_user_file(u, fname)
139
+ new_files = get_user_files(u)
140
+ names = [i[0] for i in new_files]
141
+ return tip, gr.update(choices=names)
142
+ del_btn.click(del_action, inputs=[username, file_drop], outputs=[upload_msg, file_drop])
143
+
144
+ if __name__ == "__main__":
145
+ demo.launch(server_name="0.0.0.0")