Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,9 @@ from flask import Flask, request, jsonify, Response
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import time
|
| 5 |
-
import random
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
-
@app.route('/')
|
| 10 |
-
def index():
|
| 11 |
-
return "flux2api with siliconflow", 200
|
| 12 |
-
|
| 13 |
@app.route('/v1/chat/completions', methods=['POST'])
|
| 14 |
def handle_request():
|
| 15 |
try:
|
|
@@ -32,24 +27,10 @@ def handle_request():
|
|
| 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':
|
| 53 |
}
|
| 54 |
|
| 55 |
response = requests.post(new_url, headers=headers, json=new_request_body)
|
|
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import time
|
|
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
@app.route('/v1/chat/completions', methods=['POST'])
|
| 9 |
def handle_request():
|
| 10 |
try:
|
|
|
|
| 27 |
"guidance_scale": 1
|
| 28 |
}
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
headers = {
|
| 31 |
'accept': 'application/json',
|
| 32 |
'content-type': 'application/json',
|
| 33 |
+
'Authorization': request.headers.get('Authorization')
|
| 34 |
}
|
| 35 |
|
| 36 |
response = requests.post(new_url, headers=headers, json=new_request_body)
|