chb2026 commited on
Commit
06fc498
·
verified ·
1 Parent(s): 6a50d34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -16
app.py CHANGED
@@ -9,7 +9,6 @@ from flask import Flask, request, jsonify, redirect, render_template
9
  from huggingface_hub import HfApi, HfFileSystem
10
  from PIL import Image
11
  import uuid
12
- import requests
13
 
14
  app = Flask(__name__)
15
 
@@ -18,6 +17,7 @@ HF_TOKEN = os.environ.get("HF_TOKEN")
18
  DATASET_ID = os.environ.get("DATASET_ID")
19
  MAX_SIZE = int(os.environ.get("MAX_SIZE", 10)) * 1024 * 1024 # 转字节
20
  API_KEY = os.environ.get("API_KEY")
 
21
 
22
  fs = HfFileSystem(token=HF_TOKEN)
23
  hf_api = HfApi(token=HF_TOKEN)
@@ -38,11 +38,8 @@ def generate_filename(data, mime_type=None):
38
  def save_to_dataset(content, filename):
39
  fs.mkdir(f"datasets/{DATASET_ID}/images", exist_ok=True)
40
  fs.write_bytes(f"datasets/{DATASET_ID}/images/{filename}", content)
41
- url = f"https://huggingface.co/datasets/{DATASET_ID}/resolve/main/images/{filename}"
42
-
43
- # 获取重定向后的 URL
44
- response = requests.head(url, allow_redirects=True)
45
- return response.url
46
 
47
  @app.route("/", methods=["GET"])
48
  def index():
@@ -100,16 +97,39 @@ def upload():
100
  image = Image.open(BytesIO(content))
101
  width, height = image.size
102
  mime_type = image.get_format_mimetype()
 
 
103
  except:
104
  width = height = 0
105
  mime_type = None
 
 
106
 
107
  # 生成存储信息
108
  filename = generate_filename(content, mime_type)
109
  file_url = save_to_dataset(content, filename)
110
 
111
- # 构造响应
112
  response_format = request.args.get("format", "json")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  if response_format == "txt":
114
  return file_url
115
  elif response_format == "redirect":
@@ -117,16 +137,11 @@ def upload():
117
  else:
118
  return jsonify({
119
  "status_code": 200,
120
- "success": {"message": "file uploaded", "code": 200},
121
- "image": {
122
- "filename": filename,
123
- "url": file_url,
124
- "size": len(content),
125
- "width": width,
126
- "height": height,
127
- "mime": mime_type,
128
- "size_formatted": f"{len(content)/1024/1024:.1f} MB"
129
  },
 
130
  "status_txt": "OK"
131
  })
132
 
 
9
  from huggingface_hub import HfApi, HfFileSystem
10
  from PIL import Image
11
  import uuid
 
12
 
13
  app = Flask(__name__)
14
 
 
17
  DATASET_ID = os.environ.get("DATASET_ID")
18
  MAX_SIZE = int(os.environ.get("MAX_SIZE", 10)) * 1024 * 1024 # 转字节
19
  API_KEY = os.environ.get("API_KEY")
20
+ SPACE_DOMAIN = os.environ.get("SPACE_DOMAIN", "chb2026-image.hf.space") # Space域名
21
 
22
  fs = HfFileSystem(token=HF_TOKEN)
23
  hf_api = HfApi(token=HF_TOKEN)
 
38
  def save_to_dataset(content, filename):
39
  fs.mkdir(f"datasets/{DATASET_ID}/images", exist_ok=True)
40
  fs.write_bytes(f"datasets/{DATASET_ID}/images/{filename}", content)
41
+ # 返回代理URL
42
+ return f"https://{SPACE_DOMAIN}/proxy/datasets/{DATASET_ID}/resolve/main/images/{filename}"
 
 
 
43
 
44
  @app.route("/", methods=["GET"])
45
  def index():
 
97
  image = Image.open(BytesIO(content))
98
  width, height = image.size
99
  mime_type = image.get_format_mimetype()
100
+ extension = mimetypes.guess_extension(mime_type) or ".jpg"
101
+ is_animated = getattr(image, "is_animated", False)
102
  except:
103
  width = height = 0
104
  mime_type = None
105
+ extension = ".bin"
106
+ is_animated = False
107
 
108
  # 生成存储信息
109
  filename = generate_filename(content, mime_type)
110
  file_url = save_to_dataset(content, filename)
111
 
112
+ # 构造符合PicGo API的响应
113
  response_format = request.args.get("format", "json")
114
+
115
+ image_info = {
116
+ "name": os.path.splitext(filename)[0],
117
+ "extension": extension.lstrip("."),
118
+ "size": len(content),
119
+ "width": width,
120
+ "height": height,
121
+ "date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
122
+ "date_gmt": datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"),
123
+ "title": request.form.get("title", filename),
124
+ "description": request.form.get("description"),
125
+ "nsfw": int(request.form.get("nsfw", "0")),
126
+ "md5": hashlib.md5(content).hexdigest(),
127
+ "mime": mime_type,
128
+ "url": file_url,
129
+ "is_animated": int(is_animated),
130
+ "size_formatted": f"{len(content)/1024/1024:.1f} MB"
131
+ }
132
+
133
  if response_format == "txt":
134
  return file_url
135
  elif response_format == "redirect":
 
137
  else:
138
  return jsonify({
139
  "status_code": 200,
140
+ "success": {
141
+ "message": "file uploaded",
142
+ "code": 200
 
 
 
 
 
 
143
  },
144
+ "image": image_info,
145
  "status_txt": "OK"
146
  })
147