Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,26 @@
|
|
|
|
1 |
import json
|
2 |
from datetime import datetime
|
3 |
-
import
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
def
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
async def handle_chat_completions(request):
|
11 |
-
try:
|
12 |
-
body = await 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 |
-
|
|
|
19 |
|
20 |
prompt = messages[-1]['content']
|
21 |
new_url = f"https://api.siliconflow.cn/v1/{model}/text-to-image"
|
@@ -27,20 +32,27 @@ async def handle_chat_completions(request):
|
|
27 |
"guidance_scale": 1
|
28 |
}
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
image_url = response_body['images'][0]['url']
|
39 |
unique_id = int(datetime.now().timestamp() * 1000)
|
40 |
current_timestamp = int(unique_id / 1000)
|
41 |
|
42 |
if stream:
|
43 |
-
# 构造流式响应
|
44 |
response_payload = {
|
45 |
"id": unique_id,
|
46 |
"object": "chat.completion.chunk",
|
@@ -56,12 +68,11 @@ async def handle_chat_completions(request):
|
|
56 |
}
|
57 |
]
|
58 |
}
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
else:
|
64 |
-
# 构造非流响应
|
65 |
response_payload = {
|
66 |
"id": unique_id,
|
67 |
"object": "chat.completion",
|
@@ -84,14 +95,16 @@ async def handle_chat_completions(request):
|
|
84 |
"total_tokens": len(prompt) + len(image_url)
|
85 |
}
|
86 |
}
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
app = web.Application()
|
93 |
-
app.router.add_post('/ai/v1/chat/completions', handle_chat_completions)
|
94 |
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
if __name__ == '__main__':
|
97 |
-
|
|
|
1 |
+
from http.server import HTTPServer, BaseHTTPRequestHandler
|
2 |
import json
|
3 |
from datetime import datetime
|
4 |
+
import urllib.request
|
5 |
+
import ssl
|
6 |
|
7 |
+
class ChatCompletionsHandler(BaseHTTPRequestHandler):
|
8 |
+
def do_POST(self):
|
9 |
+
if self.path != '/ai/v1/chat/completions':
|
10 |
+
self.send_error(404, "Not Found")
|
11 |
+
return
|
12 |
+
|
13 |
+
content_length = int(self.headers['Content-Length'])
|
14 |
+
post_data = self.rfile.read(content_length)
|
15 |
+
body = json.loads(post_data.decode('utf-8'))
|
16 |
|
|
|
|
|
|
|
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 |
+
self.send_error(400, "Bad Request: Missing required fields")
|
23 |
+
return
|
24 |
|
25 |
prompt = messages[-1]['content']
|
26 |
new_url = f"https://api.siliconflow.cn/v1/{model}/text-to-image"
|
|
|
32 |
"guidance_scale": 1
|
33 |
}
|
34 |
|
35 |
+
req = urllib.request.Request(new_url,
|
36 |
+
data=json.dumps(new_request_body).encode('utf-8'),
|
37 |
+
headers={
|
38 |
+
'accept': 'application/json',
|
39 |
+
'content-type': 'application/json',
|
40 |
+
'Authorization': self.headers.get('Authorization')
|
41 |
+
},
|
42 |
+
method='POST')
|
43 |
+
|
44 |
+
ctx = ssl.create_default_context()
|
45 |
+
ctx.check_hostname = False
|
46 |
+
ctx.verify_mode = ssl.CERT_NONE
|
47 |
+
|
48 |
+
with urllib.request.urlopen(req, context=ctx) as response:
|
49 |
+
response_body = json.loads(response.read().decode('utf-8'))
|
50 |
|
51 |
image_url = response_body['images'][0]['url']
|
52 |
unique_id = int(datetime.now().timestamp() * 1000)
|
53 |
current_timestamp = int(unique_id / 1000)
|
54 |
|
55 |
if stream:
|
|
|
56 |
response_payload = {
|
57 |
"id": unique_id,
|
58 |
"object": "chat.completion.chunk",
|
|
|
68 |
}
|
69 |
]
|
70 |
}
|
71 |
+
self.send_response(200)
|
72 |
+
self.send_header('Content-Type', 'text/event-stream')
|
73 |
+
self.end_headers()
|
74 |
+
self.wfile.write(f"data: {json.dumps(response_payload)}\n\n".encode('utf-8'))
|
75 |
else:
|
|
|
76 |
response_payload = {
|
77 |
"id": unique_id,
|
78 |
"object": "chat.completion",
|
|
|
95 |
"total_tokens": len(prompt) + len(image_url)
|
96 |
}
|
97 |
}
|
98 |
+
self.send_response(200)
|
99 |
+
self.send_header('Content-Type', 'application/json')
|
100 |
+
self.end_headers()
|
101 |
+
self.wfile.write(json.dumps(response_payload).encode('utf-8'))
|
|
|
|
|
|
|
102 |
|
103 |
+
def run(server_class=HTTPServer, handler_class=ChatCompletionsHandler, port=8000):
|
104 |
+
server_address = ('', port)
|
105 |
+
httpd = server_class(server_address, handler_class)
|
106 |
+
print(f"Starting server on port {port}")
|
107 |
+
httpd.serve_forever()
|
108 |
|
109 |
if __name__ == '__main__':
|
110 |
+
run()
|