smgc commited on
Commit
f8d271e
1 Parent(s): fab1a10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -21
app.py CHANGED
@@ -6,16 +6,6 @@ import random
6
 
7
  app = Flask(__name__)
8
 
9
- def get_random_token(auth_header):
10
- if not auth_header:
11
- return None
12
- if auth_header.startswith('Bearer '):
13
- auth_header = auth_header[7:]
14
- tokens = [token.strip() for token in auth_header.split(',') if token.strip()]
15
- if not tokens:
16
- return None
17
- return f"Bearer {random.choice(tokens)}"
18
-
19
  @app.route('/')
20
  def index():
21
  return "text-to-image with siliconflow", 200
@@ -27,11 +17,13 @@ def handle_request():
27
  model = body.get('model')
28
  messages = body.get('messages')
29
  stream = body.get('stream', False)
 
30
  if not model or not messages or len(messages) == 0:
31
  return jsonify({"error": "Bad Request: Missing required fields"}), 400
32
-
33
  prompt = messages[-1]['content']
34
  new_url = f'https://api.siliconflow.cn/v1/{model}/text-to-image'
 
35
  new_request_body = {
36
  "prompt": prompt,
37
  "image_size": "1024x1024",
@@ -39,24 +31,34 @@ def handle_request():
39
  "num_inference_steps": 4,
40
  "guidance_scale": 1
41
  }
42
-
43
- # 获取随机token
44
- random_token = get_random_token(request.headers.get('Authorization'))
45
- if not random_token:
46
- return jsonify({"error": "Unauthorized: Invalid or missing Authorization header"}), 401
47
-
 
 
 
 
 
 
 
 
 
48
  headers = {
49
  'accept': 'application/json',
50
  'content-type': 'application/json',
51
- 'Authorization': random_token
52
  }
53
-
54
  response = requests.post(new_url, headers=headers, json=new_request_body)
55
  response_body = response.json()
 
56
  image_url = response_body['images'][0]['url']
57
  unique_id = int(time.time() * 1000)
58
  current_timestamp = unique_id // 1000
59
-
60
  if stream:
61
  response_payload = {
62
  "id": unique_id,
@@ -100,8 +102,9 @@ def handle_request():
100
  }
101
  data_string = json.dumps(response_payload)
102
  return Response(f"{data_string}\n\n", content_type='text/event-stream')
 
103
  except Exception as e:
104
  return jsonify({"error": f"Internal Server Error: {str(e)}"}), 500
105
 
106
  if __name__ == '__main__':
107
- app.run(host='0.0.0.0', port=8000)
 
6
 
7
  app = Flask(__name__)
8
 
 
 
 
 
 
 
 
 
 
 
9
  @app.route('/')
10
  def index():
11
  return "text-to-image with siliconflow", 200
 
17
  model = body.get('model')
18
  messages = body.get('messages')
19
  stream = body.get('stream', False)
20
+
21
  if not model or not messages or len(messages) == 0:
22
  return jsonify({"error": "Bad Request: Missing required fields"}), 400
23
+
24
  prompt = messages[-1]['content']
25
  new_url = f'https://api.siliconflow.cn/v1/{model}/text-to-image'
26
+
27
  new_request_body = {
28
  "prompt": prompt,
29
  "image_size": "1024x1024",
 
31
  "num_inference_steps": 4,
32
  "guidance_scale": 1
33
  }
34
+
35
+ # 从传入的 Authorization 头中随机选择一个 token
36
+ authorization_header = request.headers.get('Authorization')
37
+ if authorization_header:
38
+ # 去掉 "Bearer " 前缀并分割 token
39
+ tokens = authorization_header.replace("Bearer ", "").split(',')
40
+ if len(tokens) > 1:
41
+ selected_token = random.choice(tokens).strip()
42
+ else:
43
+ selected_token = tokens[0].strip()
44
+ # 重新格式化为 "Bearer 随机选择的token"
45
+ selected_token = f"Bearer {selected_token}"
46
+ else:
47
+ return jsonify({"error": "Unauthorized: Missing Authorization header"}), 401
48
+
49
  headers = {
50
  'accept': 'application/json',
51
  'content-type': 'application/json',
52
+ 'Authorization': selected_token
53
  }
54
+
55
  response = requests.post(new_url, headers=headers, json=new_request_body)
56
  response_body = response.json()
57
+
58
  image_url = response_body['images'][0]['url']
59
  unique_id = int(time.time() * 1000)
60
  current_timestamp = unique_id // 1000
61
+
62
  if stream:
63
  response_payload = {
64
  "id": unique_id,
 
102
  }
103
  data_string = json.dumps(response_payload)
104
  return Response(f"{data_string}\n\n", content_type='text/event-stream')
105
+
106
  except Exception as e:
107
  return jsonify({"error": f"Internal Server Error: {str(e)}"}), 500
108
 
109
  if __name__ == '__main__':
110
+ app.run(host='0.0.0.0', port=8000)