Spaces:
Runtime error

newpan / app.py
WACATW's picture
Update app.py
134fb11 verified
Raw History Blame Contribute Delete
2.66 kB
import streamlit as st
import requests
import json
# 后台Secrets密钥
HF_TOKEN = st.secrets["HF_TOKEN"]
HF_USERNAME = "WACATW"
HF_DATASET = "pan"
HF_API_BASE = f"https://huggingface.co/api/datasets/{HF_USERNAME}/{HF_DATASET}"
st.set_page_config(page_title="个人云盘", layout="wide")
headers = {
"Authorization": f"Bearer {HF_TOKEN}",
"Content-Type": "application/json"
}
# 读取url参数
action = st.query_params.get("action", "")
data_raw = st.query_params.get("data", "{}")
try:
req_data = json.loads(data_raw)
except Exception:
req_data = {}
# 接口路由
if action == "list":
path = req_data.get("path", "")
res = requests.get(f"{HF_API_BASE}/tree/main/{path}", headers=headers)
st.json(res.json())
elif action == "getContent":
path = req_data.get("path", "")
res = requests.get(f"{HF_API_BASE}/resolve/main/{path}", headers=headers)
st.text(res.text)
elif action == "getBlob":
path = req_data.get("path", "")
res = requests.get(f"{HF_API_BASE}/resolve/main/{path}", headers=headers)
st.download_button(label="download", data=res.content, file_name="tmp", key="dl")
elif action == "uploadText":
path = req_data.get("path", "")
content = req_data.get("content", "")
payload = {
"content": content,
"commit_message": "前端云盘上传文本"
}
res = requests.put(f"{HF_API_BASE}/main/{path}", json=payload, headers=headers)
st.json({"success": res.status_code in (200, 201)})
elif action == "deleteSingle":
path = req_data.get("path", "")
res = requests.delete(f"{HF_API_BASE}/main/{path}", headers=headers)
st.json({"success": res.status_code == 200})
elif action == "deleteBatch":
paths = req_data.get("paths", [])
ok = True
for p in paths:
r = requests.delete(f"{HF_API_BASE}/main/{p}", headers=headers)
if r.status_code != 200:
ok = False
st.json({"success": ok})
elif action == "move":
old_path = req_data.get("oldPath")
new_path = req_data.get("newPath")
# 复制
get_res = requests.get(f"{HF_API_BASE}/resolve/main/{old_path}", headers=headers)
put_payload = {
"content": get_res.text,
"commit_message": "重命名文件"
}
requests.put(f"{HF_API_BASE}/main/{new_path}", json=put_payload, headers=headers)
# 删除原文件
requests.delete(f"{HF_API_BASE}/main/{old_path}", headers=headers)
st.json({"success": True})
# 无api参数,渲染网页
if not st.query_params.get("api"):
with open("index.html", "r", encoding="utf-8") as f:
html = f.read()
st.components.v1.html(html, height=950, scrolling=True)