chatyou commited on
Commit
769040c
·
verified ·
1 Parent(s): 59c1ff7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -13
app.py CHANGED
@@ -1,7 +1,11 @@
1
  import os
2
  import tempfile
3
  from flask import Flask, request, jsonify, send_file
4
- from huggingface_hub import HfApi
 
 
 
 
5
 
6
  app = Flask(__name__)
7
 
@@ -10,12 +14,10 @@ HF_TOKEN = os.environ.get("HF_TOKEN")
10
  if not HF_TOKEN:
11
  raise ValueError("HF_TOKEN environment variable not set. Please add it to Space Secrets.")
12
 
13
- api = HfApi(token=HF_TOKEN)
14
-
15
  # 你的 Bucket ID(格式:用户名/存储桶名)
16
  BUCKET_ID = "nagose/filebed"
17
 
18
- # 嵌入 HTML 前端
19
  HTML_CONTENT = """<!DOCTYPE html>
20
  <html>
21
  <head>
@@ -122,10 +124,11 @@ def upload():
122
  with tempfile.NamedTemporaryFile(delete=False) as tmp:
123
  file.save(tmp.name)
124
  try:
125
- # 使用 upload_bucket_files 上传文件
126
- api.upload_bucket_files(
127
  bucket_id=BUCKET_ID,
128
- files=[(tmp.name, file.filename)]
 
129
  )
130
  except Exception as e:
131
  return jsonify({'error': str(e)}), 500
@@ -137,7 +140,12 @@ def upload():
137
  @app.route('/list')
138
  def list_files():
139
  try:
140
- items = api.list_bucket_tree(BUCKET_ID, recursive=True)
 
 
 
 
 
141
  files = [item.path for item in items if item.type == 'file']
142
  return jsonify(files)
143
  except Exception as e:
@@ -149,9 +157,10 @@ def get_file(filename):
149
  with tempfile.TemporaryDirectory() as tmpdir:
150
  local_path = os.path.join(tmpdir, filename)
151
  # 使用 download_bucket_files 下载文件
152
- api.download_bucket_files(
153
  bucket_id=BUCKET_ID,
154
- files=[(filename, local_path)]
 
155
  )
156
  return send_file(local_path, as_attachment=True, download_name=filename)
157
  except Exception as e:
@@ -160,10 +169,11 @@ def get_file(filename):
160
  @app.route('/delete/<path:filename>', methods=['DELETE'])
161
  def delete_file(filename):
162
  try:
163
- # 使用 delete_bucket_files 删除文件
164
- api.delete_bucket_files(
165
  bucket_id=BUCKET_ID,
166
- paths=[filename]
 
167
  )
168
  return jsonify({'success': True})
169
  except Exception as e:
 
1
  import os
2
  import tempfile
3
  from flask import Flask, request, jsonify, send_file
4
+ from huggingface_hub import (
5
+ batch_bucket_files,
6
+ download_bucket_files,
7
+ list_bucket_tree
8
+ )
9
 
10
  app = Flask(__name__)
11
 
 
14
  if not HF_TOKEN:
15
  raise ValueError("HF_TOKEN environment variable not set. Please add it to Space Secrets.")
16
 
 
 
17
  # 你的 Bucket ID(格式:用户名/存储桶名)
18
  BUCKET_ID = "nagose/filebed"
19
 
20
+ # 嵌入 HTML 前端(完整保留)
21
  HTML_CONTENT = """<!DOCTYPE html>
22
  <html>
23
  <head>
 
124
  with tempfile.NamedTemporaryFile(delete=False) as tmp:
125
  file.save(tmp.name)
126
  try:
127
+ # 使用 batch_bucket_files 上传文件(与文档完全一致)
128
+ batch_bucket_files(
129
  bucket_id=BUCKET_ID,
130
+ add=[(tmp.name, file.filename)],
131
+ token=HF_TOKEN # 显式传递 token 确保认证
132
  )
133
  except Exception as e:
134
  return jsonify({'error': str(e)}), 500
 
140
  @app.route('/list')
141
  def list_files():
142
  try:
143
+ # 使用 list_bucket_tree 获取文件列表
144
+ items = list_bucket_tree(
145
+ bucket_id=BUCKET_ID,
146
+ recursive=True,
147
+ token=HF_TOKEN
148
+ )
149
  files = [item.path for item in items if item.type == 'file']
150
  return jsonify(files)
151
  except Exception as e:
 
157
  with tempfile.TemporaryDirectory() as tmpdir:
158
  local_path = os.path.join(tmpdir, filename)
159
  # 使用 download_bucket_files 下载文件
160
+ download_bucket_files(
161
  bucket_id=BUCKET_ID,
162
+ files=[(filename, local_path)],
163
+ token=HF_TOKEN
164
  )
165
  return send_file(local_path, as_attachment=True, download_name=filename)
166
  except Exception as e:
 
169
  @app.route('/delete/<path:filename>', methods=['DELETE'])
170
  def delete_file(filename):
171
  try:
172
+ # 使用 batch_bucket_files 删除文件(通过 delete 参数)
173
+ batch_bucket_files(
174
  bucket_id=BUCKET_ID,
175
+ delete=[filename],
176
+ token=HF_TOKEN
177
  )
178
  return jsonify({'success': True})
179
  except Exception as e: