Spaces:
Running
Running
File size: 4,626 Bytes
c6089ac 56a75e7 6efca19 4280a1e d6fe2d7 f02fba9 d6fe2d7 f02fba9 6efca19 f490c39 d6fe2d7 4280a1e f490c39 4280a1e 56a75e7 f490c39 4280a1e 56a75e7 6efca19 56a75e7 6efca19 56a75e7 6efca19 d6fe2d7 56a75e7 6efca19 56a75e7 6efca19 56a75e7 6efca19 56a75e7 f02fba9 56a75e7 |
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
import os
import tempfile
import requests
from flask import Flask, request, send_file, redirect, url_for, render_template_string
from huggingface_hub import HfApi, upload_file, hf_hub_url
REPO_ID = os.getenv("REPO_ID")
TOKEN = os.getenv("HF_TOKEN")
app = Flask(__name__)
api = HfApi()
HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Drive - {{ repo_id }}</title>
<style>
body {
margin: 0;
font-family: 'Segoe UI', sans-serif;
background-color: #1e1e1e;
color: #ffffff;
}
header {
padding: 16px;
background-color: #202124;
font-size: 20px;
font-weight: bold;
}
.container {
padding: 20px;
}
.folder {
color: #8ab4f8;
cursor: pointer;
margin: 6px 0;
}
.file {
margin-left: 20px;
margin: 4px 0;
}
a {
color: #9cdcfe;
text-decoration: none;
margin-left: 10px;
}
a:hover {
text-decoration: underline;
}
.upload-box {
margin-top: 30px;
background: #2c2c2c;
padding: 20px;
border-radius: 8px;
}
input[type="file"], input[type="text"], input[type="submit"] {
margin-top: 10px;
padding: 8px;
font-size: 14px;
}
</style>
<script>
function toggleFolder(id) {
const el = document.getElementById(id);
if (el.style.display === "none") {
el.style.display = "block";
} else {
el.style.display = "none";
}
}
</script>
</head>
<body>
<header>π Hugging Face Drive β {{ repo_id }}</header>
<div class="container">
{% for folder, contents in folder_tree.items() %}
<div class="folder" onclick="toggleFolder('{{ loop.index }}')">π {{ folder }}</div>
<div id="{{ loop.index }}" style="display:none; padding-left: 20px;">
{% for file in contents %}
<div class="file">
{{ file }}
<a href="{{ url_for('download_file', filename=folder + '/' + file) }}">Download</a>
</div>
{% endfor %}
</div>
{% endfor %}
<div class="upload-box">
<h3>Upload File</h3>
<form method="POST" action="/upload" enctype="multipart/form-data">
Folder path (optional): <input type="text" name="folder" placeholder="e.g. my/folder"><br>
File: <input type="file" name="file"><br>
<input type="submit" value="Upload">
</form>
</div>
</div>
</body>
</html>
"""
def build_folder_tree(file_list):
tree = {}
for path in file_list:
if "/" in path:
folder, file = path.rsplit("/", 1)
else:
folder, file = "", path
tree.setdefault(folder, []).append(file)
return tree
@app.route("/")
def index():
files = api.list_repo_files(repo_id=REPO_ID, repo_type="dataset", token=TOKEN)
folder_tree = build_folder_tree(files)
return render_template_string(HTML_TEMPLATE, repo_id=REPO_ID, folder_tree=folder_tree)
@app.route("/download/<path:filename>")
def download_file(filename):
try:
url = hf_hub_url(REPO_ID, filename, repo_type="dataset")
headers = {"Authorization": f"Bearer {TOKEN}"}
response = requests.get(url, headers=headers)
if response.status_code != 200:
return f"Download failed: {response.status_code}", 500
tmp = tempfile.NamedTemporaryFile(delete=False)
tmp.write(response.content)
tmp.close()
return send_file(tmp.name, as_attachment=True, download_name=os.path.basename(filename))
except Exception as e:
return str(e), 500
@app.route("/upload", methods=["POST"])
def upload():
file = request.files["file"]
folder = request.form.get("folder", "").strip().strip("/")
if file:
filename = file.filename
path_in_repo = f"{folder}/{filename}" if folder else filename
temp_path = os.path.join(tempfile.gettempdir(), filename)
file.save(temp_path)
upload_file(
path_or_fileobj=temp_path,
path_in_repo=path_in_repo,
repo_id=REPO_ID,
repo_type="dataset",
token=TOKEN
)
return redirect(url_for("index"))
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=7860)
|