testdeep123 commited on
Commit
6efca19
·
verified ·
1 Parent(s): 56a75e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -18
app.py CHANGED
@@ -1,30 +1,30 @@
1
  import os
2
- from flask import Flask, request, send_file, redirect, url_for, render_template_string
3
- from huggingface_hub import HfApi, upload_file
4
  import tempfile
 
 
 
5
 
6
- # Get credentials from environment
7
  REPO_ID = os.getenv("REPO_ID")
8
  TOKEN = os.getenv("HF_TOKEN")
9
 
10
  app = Flask(__name__)
11
  api = HfApi()
12
 
13
- # Basic HTML template with upload form
14
- TEMPLATE = """
15
  <!doctype html>
16
  <title>Hugging Face Dataset Browser</title>
17
  <h2>Files in {{ repo_id }}</h2>
18
  <ul>
19
  {% for file in files %}
20
  <li>
21
- {{ file }}
22
- <a href="{{ url_for('download_file', filename=file) }}">Download</a>
23
  </li>
24
  {% endfor %}
25
  </ul>
26
  <h3>Upload File</h3>
27
  <form method="POST" action="/upload" enctype="multipart/form-data">
 
28
  <input type="file" name="file">
29
  <input type="submit" value="Upload">
30
  </form>
@@ -33,35 +33,45 @@ TEMPLATE = """
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(TEMPLATE, files=files, repo_id=REPO_ID)
37
 
38
  @app.route("/download/<path:filename>")
39
  def download_file(filename):
40
- url = api.hf_hub_url(REPO_ID, filename, repo_type="dataset")
41
- import requests
42
- headers = {"Authorization": f"Bearer {TOKEN}"}
43
- r = requests.get(url, headers=headers)
44
- if r.status_code == 200:
 
45
  tmp = tempfile.NamedTemporaryFile(delete=False)
46
- tmp.write(r.content)
47
  tmp.close()
48
  return send_file(tmp.name, as_attachment=True, download_name=os.path.basename(filename))
49
- else:
50
- return f"Failed to download {filename}", 404
51
 
52
  @app.route("/upload", methods=["POST"])
53
  def upload():
54
  file = request.files["file"]
 
 
55
  if file:
56
- temp_path = os.path.join(tempfile.gettempdir(), file.filename)
 
 
 
 
57
  file.save(temp_path)
 
 
58
  upload_file(
59
  path_or_fileobj=temp_path,
60
- path_in_repo=file.filename,
61
  repo_id=REPO_ID,
62
  repo_type="dataset",
63
  token=TOKEN
64
  )
 
65
  return redirect(url_for("index"))
66
 
67
  if __name__ == "__main__":
 
1
  import os
 
 
2
  import tempfile
3
+ import requests
4
+ from flask import Flask, request, send_file, redirect, url_for, render_template_string
5
+ from huggingface_hub import HfApi, upload_file, hf_hub_url
6
 
 
7
  REPO_ID = os.getenv("REPO_ID")
8
  TOKEN = os.getenv("HF_TOKEN")
9
 
10
  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>
 
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):
40
+ try:
41
+ url = hf_hub_url(REPO_ID, filename, repo_type="dataset")
42
+ headers = {"Authorization": f"Bearer {TOKEN}"}
43
+ response = requests.get(url, headers=headers)
44
+ if response.status_code != 200:
45
+ return f"Download failed: {response.status_code}", 500
46
  tmp = tempfile.NamedTemporaryFile(delete=False)
47
+ tmp.write(response.content)
48
  tmp.close()
49
  return send_file(tmp.name, as_attachment=True, download_name=os.path.basename(filename))
50
+ except Exception as e:
51
+ return str(e), 500
52
 
53
  @app.route("/upload", methods=["POST"])
54
  def upload():
55
  file = request.files["file"]
56
+ folder = request.form.get("folder", "").strip().strip("/")
57
+
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,
70
  repo_id=REPO_ID,
71
  repo_type="dataset",
72
  token=TOKEN
73
  )
74
+
75
  return redirect(url_for("index"))
76
 
77
  if __name__ == "__main__":