chb2026 commited on
Commit
8c745cf
·
verified ·
1 Parent(s): 3ad5cff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -16
app.py CHANGED
@@ -4,7 +4,6 @@ import hashlib
4
  import mimetypes
5
  from datetime import datetime
6
  from io import BytesIO
7
- import requests
8
 
9
  from flask import Flask, request, jsonify, redirect, render_template
10
  from huggingface_hub import HfApi, HfFileSystem
@@ -36,18 +35,9 @@ def generate_filename(data, mime_type=None):
36
  return f"{timestamp}-{unique_id}{ext}"
37
 
38
  def save_to_dataset(content, filename):
39
- path = f"datasets/{DATASET_ID}/resolve/main/images/{filename}"
40
  fs.mkdir(f"datasets/{DATASET_ID}/images", exist_ok=True)
41
  fs.write_bytes(f"datasets/{DATASET_ID}/images/{filename}", content)
42
-
43
- # 获取正确的 URL
44
- repo_id = f"datasets/{DATASET_ID}"
45
- file_path = f"images/{filename}"
46
- url = hf_api.hf_hub_url(repo_id, filename=file_path)
47
-
48
- # 获取实际的下载 URL
49
- response = requests.head(url, allow_redirects=True)
50
- return response.url
51
 
52
  @app.route("/", methods=["GET"])
53
  def index():
@@ -77,15 +67,20 @@ def upload():
77
  # 处理Base64、文件或URL
78
  if isinstance(source, bytes):
79
  content = source
80
- elif source.startswith(("http://", "https://")):
81
- # 需要实现URL下载逻辑(示例代码省略)
82
  return jsonify({"error": "URL download not implemented"}), 501
83
- elif source.startswith("data:"):
84
  header, data = source.split(",", 1)
85
  mime_type = header.split(":")[1].split(";")[0]
86
  content = base64.b64decode(data)
87
- else:
88
  content = base64.b64decode(source)
 
 
 
 
 
 
89
 
90
  # 验证大小限制
91
  if len(content) > MAX_SIZE:
@@ -132,4 +127,3 @@ def upload():
132
 
133
  if __name__ == "__main__":
134
  app.run(host="0.0.0.0", port=7860, debug=True)
135
-
 
4
  import mimetypes
5
  from datetime import datetime
6
  from io import BytesIO
 
7
 
8
  from flask import Flask, request, jsonify, redirect, render_template
9
  from huggingface_hub import HfApi, HfFileSystem
 
35
  return f"{timestamp}-{unique_id}{ext}"
36
 
37
  def save_to_dataset(content, filename):
 
38
  fs.mkdir(f"datasets/{DATASET_ID}/images", exist_ok=True)
39
  fs.write_bytes(f"datasets/{DATASET_ID}/images/{filename}", content)
40
+ return f"https://huggingface.co/datasets/{DATASET_ID}/resolve/main/images/{filename}"
 
 
 
 
 
 
 
 
41
 
42
  @app.route("/", methods=["GET"])
43
  def index():
 
67
  # 处理Base64、文件或URL
68
  if isinstance(source, bytes):
69
  content = source
70
+ elif source and source.startswith(("http://", "https://")):
 
71
  return jsonify({"error": "URL download not implemented"}), 501
72
+ elif source and source.startswith("data:"):
73
  header, data = source.split(",", 1)
74
  mime_type = header.split(":")[1].split(";")[0]
75
  content = base64.b64decode(data)
76
+ elif source:
77
  content = base64.b64decode(source)
78
+ else:
79
+ return jsonify({
80
+ "status_code": 400,
81
+ "error": {"message": "No file provided"},
82
+ "status_txt": "Bad Request"
83
+ }), 400
84
 
85
  # 验证大小限制
86
  if len(content) > MAX_SIZE:
 
127
 
128
  if __name__ == "__main__":
129
  app.run(host="0.0.0.0", port=7860, debug=True)