testdeep123 commited on
Commit
f490c39
Β·
verified Β·
1 Parent(s): 6efca19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -23
app.py CHANGED
@@ -11,29 +11,110 @@ app = Flask(__name__)
11
  api = HfApi()
12
 
13
  HTML_TEMPLATE = """
14
- <!doctype html>
15
- <title>Hugging Face Dataset Browser</title>
16
- <h2>Files in {{ repo_id }}</h2>
17
- <ul>
18
- {% for file in files %}
19
- <li>
20
- {{ file }}
21
- β€” <a href="{{ url_for('download_file', filename=file) }}">Download</a>
22
- </li>
23
- {% endfor %}
24
- </ul>
25
- <h3>Upload File</h3>
26
- <form method="POST" action="/upload" enctype="multipart/form-data">
27
- Folder path (optional): <input type="text" name="folder" placeholder="example/folder"><br><br>
28
- <input type="file" name="file">
29
- <input type="submit" value="Upload">
30
- </form>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  """
32
 
 
 
 
 
 
 
 
 
 
 
33
  @app.route("/")
34
  def index():
35
  files = api.list_repo_files(repo_id=REPO_ID, repo_type="dataset", token=TOKEN)
36
- return render_template_string(HTML_TEMPLATE, files=files, repo_id=REPO_ID)
 
37
 
38
  @app.route("/download/<path:filename>")
39
  def download_file(filename):
@@ -58,12 +139,8 @@ def upload():
58
  if file:
59
  filename = file.filename
60
  path_in_repo = f"{folder}/{filename}" if folder else filename
61
-
62
- # Save temporarily
63
  temp_path = os.path.join(tempfile.gettempdir(), filename)
64
  file.save(temp_path)
65
-
66
- # Upload to Hugging Face
67
  upload_file(
68
  path_or_fileobj=temp_path,
69
  path_in_repo=path_in_repo,
@@ -71,7 +148,6 @@ def upload():
71
  repo_type="dataset",
72
  token=TOKEN
73
  )
74
-
75
  return redirect(url_for("index"))
76
 
77
  if __name__ == "__main__":
 
11
  api = HfApi()
12
 
13
  HTML_TEMPLATE = """
14
+ <!DOCTYPE html>
15
+ <html lang="en">
16
+ <head>
17
+ <meta charset="UTF-8">
18
+ <title>Drive - {{ repo_id }}</title>
19
+ <style>
20
+ body {
21
+ margin: 0;
22
+ font-family: 'Segoe UI', sans-serif;
23
+ background-color: #1e1e1e;
24
+ color: #ffffff;
25
+ }
26
+ header {
27
+ padding: 16px;
28
+ background-color: #202124;
29
+ font-size: 20px;
30
+ font-weight: bold;
31
+ }
32
+ .container {
33
+ padding: 20px;
34
+ }
35
+ .folder {
36
+ color: #8ab4f8;
37
+ cursor: pointer;
38
+ margin: 6px 0;
39
+ }
40
+ .file {
41
+ margin-left: 20px;
42
+ margin: 4px 0;
43
+ }
44
+ a {
45
+ color: #9cdcfe;
46
+ text-decoration: none;
47
+ margin-left: 10px;
48
+ }
49
+ a:hover {
50
+ text-decoration: underline;
51
+ }
52
+ .upload-box {
53
+ margin-top: 30px;
54
+ background: #2c2c2c;
55
+ padding: 20px;
56
+ border-radius: 8px;
57
+ }
58
+ input[type="file"], input[type="text"], input[type="submit"] {
59
+ margin-top: 10px;
60
+ padding: 8px;
61
+ font-size: 14px;
62
+ }
63
+ </style>
64
+ <script>
65
+ function toggleFolder(id) {
66
+ const el = document.getElementById(id);
67
+ if (el.style.display === "none") {
68
+ el.style.display = "block";
69
+ } else {
70
+ el.style.display = "none";
71
+ }
72
+ }
73
+ </script>
74
+ </head>
75
+ <body>
76
+ <header>πŸ“ Hugging Face Drive β€” {{ repo_id }}</header>
77
+ <div class="container">
78
+ {% for folder, contents in folder_tree.items() %}
79
+ <div class="folder" onclick="toggleFolder('{{ loop.index }}')">πŸ“‚ {{ folder }}</div>
80
+ <div id="{{ loop.index }}" style="display:none; padding-left: 20px;">
81
+ {% for file in contents %}
82
+ <div class="file">
83
+ {{ file }}
84
+ <a href="{{ url_for('download_file', filename=folder + '/' + file) }}">Download</a>
85
+ </div>
86
+ {% endfor %}
87
+ </div>
88
+ {% endfor %}
89
+
90
+ <div class="upload-box">
91
+ <h3>Upload File</h3>
92
+ <form method="POST" action="/upload" enctype="multipart/form-data">
93
+ Folder path (optional): <input type="text" name="folder" placeholder="e.g. my/folder"><br>
94
+ File: <input type="file" name="file"><br>
95
+ <input type="submit" value="Upload">
96
+ </form>
97
+ </div>
98
+ </div>
99
+ </body>
100
+ </html>
101
  """
102
 
103
+ def build_folder_tree(file_list):
104
+ tree = {}
105
+ for path in file_list:
106
+ if "/" in path:
107
+ folder, file = path.rsplit("/", 1)
108
+ else:
109
+ folder, file = "", path
110
+ tree.setdefault(folder, []).append(file)
111
+ return tree
112
+
113
  @app.route("/")
114
  def index():
115
  files = api.list_repo_files(repo_id=REPO_ID, repo_type="dataset", token=TOKEN)
116
+ folder_tree = build_folder_tree(files)
117
+ return render_template_string(HTML_TEMPLATE, repo_id=REPO_ID, folder_tree=folder_tree)
118
 
119
  @app.route("/download/<path:filename>")
120
  def download_file(filename):
 
139
  if file:
140
  filename = file.filename
141
  path_in_repo = f"{folder}/{filename}" if folder else filename
 
 
142
  temp_path = os.path.join(tempfile.gettempdir(), filename)
143
  file.save(temp_path)
 
 
144
  upload_file(
145
  path_or_fileobj=temp_path,
146
  path_in_repo=path_in_repo,
 
148
  repo_type="dataset",
149
  token=TOKEN
150
  )
 
151
  return redirect(url_for("index"))
152
 
153
  if __name__ == "__main__":