Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
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('/v1/chat/completions', methods=['POST'])
|
10 |
+
def handle_request():
|
11 |
+
try:
|
12 |
+
body = request.json
|
13 |
+
model = body.get('model')
|
14 |
+
messages = body.get('messages')
|
15 |
+
stream = body.get('stream', False)
|
16 |
+
|
17 |
+
if not model or not messages or len(messages) == 0:
|
18 |
+
return jsonify({"error": "Bad Request: Missing required fields"}), 400
|
19 |
+
|
20 |
+
prompt = messages[-1]['content']
|
21 |
+
new_url = f'https://api.siliconflow.cn/v1/{model}/text-to-image'
|
22 |
+
|
23 |
+
new_request_body = {
|
24 |
+
"prompt": prompt,
|
25 |
+
"image_size": "1024x1024",
|
26 |
+
"batch_size": 1,
|
27 |
+
"num_inference_steps": 4,
|
28 |
+
"guidance_scale": 1
|
29 |
+
}
|
30 |
+
|
31 |
+
# 从传入的 Authorization 头中随机选择一个 token
|
32 |
+
authorization_header = request.headers.get('Authorization')
|
33 |
+
if authorization_header:
|
34 |
+
# 去掉 "Bearer " 前缀并分割 token
|
35 |
+
tokens = authorization_header.replace("Bearer ", "").split(',')
|
36 |
+
if len(tokens) > 1:
|
37 |
+
selected_token = random.choice(tokens).strip()
|
38 |
+
else:
|
39 |
+
selected_token = tokens[0].strip()
|
40 |
+
# 重新格式化为 "Bearer 随机选择的token"
|
41 |
+
selected_token = f"Bearer {selected_token}"
|
42 |
+
else:
|
43 |
+
return jsonify({"error": "Unauthorized: Missing Authorization header"}), 401
|
44 |
+
|
45 |
+
headers = {
|
46 |
+
'accept': 'application/json',
|
47 |
+
'content-type': 'application/json',
|
48 |
+
'Authorization': selected_token
|
49 |
+
}
|
50 |
+
|
51 |
+
response = requests.post(new_url, headers=headers, json=new_request_body)
|
52 |
+
response_body = response.json()
|
53 |
+
|
54 |
+
image_url = response_body['images'][0]['url']
|
55 |
+
unique_id = int(time.time() * 1000)
|
56 |
+
current_timestamp = unique_id // 1000
|
57 |
+
|
58 |
+
if stream:
|
59 |
+
response_payload = {
|
60 |
+
"id": unique_id,
|
61 |
+
"object": "chat.completion.chunk",
|
62 |
+
"created": current_timestamp,
|
63 |
+
"model": model,
|
64 |
+
"choices": [
|
65 |
+
{
|
66 |
+
"index": 0,
|
67 |
+
"delta": {
|
68 |
+
"content": f"![]({image_url})"
|
69 |
+
},
|
70 |
+
"finish_reason": "stop"
|
71 |
+
}
|
72 |
+
]
|
73 |
+
}
|
74 |
+
data_string = json.dumps(response_payload)
|
75 |
+
return Response(f"data: {data_string}\n\n", content_type='text/event-stream')
|
76 |
+
else:
|
77 |
+
response_payload = {
|
78 |
+
"id": unique_id,
|
79 |
+
"object": "chat.completion",
|
80 |
+
"created": current_timestamp,
|
81 |
+
"model": model,
|
82 |
+
"choices": [
|
83 |
+
{
|
84 |
+
"index": 0,
|
85 |
+
"message": {
|
86 |
+
"role": "assistant",
|
87 |
+
"content": f"![]({image_url})"
|
88 |
+
},
|
89 |
+
"logprobs": None,
|
90 |
+
"finish_reason": "length"
|
91 |
+
}
|
92 |
+
],
|
93 |
+
"usage": {
|
94 |
+
"prompt_tokens": len(prompt),
|
95 |
+
"completion_tokens": len(image_url),
|
96 |
+
"total_tokens": len(prompt) + len(image_url)
|
97 |
+
}
|
98 |
+
}
|
99 |
+
data_string = json.dumps(response_payload)
|
100 |
+
return Response(f"{data_string}\n\n", content_type='text/event-stream')
|
101 |
+
|
102 |
+
except Exception as e:
|
103 |
+
return jsonify({"error": f"Internal Server Error: {str(e)}"}), 500
|
104 |
+
|
105 |
+
if __name__ == '__main__':
|
106 |
+
app.run(host='0.0.0.0', port=8000)
|