Upload 2 files
Browse files- app.py +165 -157
- requirements.txt +3 -3
app.py
CHANGED
@@ -1,157 +1,165 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
import
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, Response, json
|
2 |
+
import requests
|
3 |
+
from uuid import uuid4
|
4 |
+
import time
|
5 |
+
import os
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
AUTH_TOKEN = os.getenv('AUTH_TOKEN')
|
9 |
+
|
10 |
+
MODEL_MAPPING = {
|
11 |
+
"deepseek": "deepseek/deepseek-chat",
|
12 |
+
"gpt-4o-mini": "openai/gpt-4o-mini",
|
13 |
+
"gemini-flash-1.5": "google/gemini-flash-1.5",
|
14 |
+
"deepseek-reasoner": "deepseek-reasoner",
|
15 |
+
"minimax-01": "minimax/minimax-01"
|
16 |
+
}
|
17 |
+
|
18 |
+
def make_heck_request(question, session_id, messages, actual_model):
|
19 |
+
previous_question = previous_answer = None
|
20 |
+
if len(messages) >= 2:
|
21 |
+
for i in range(len(messages)-2, -1, -1):
|
22 |
+
if messages[i]["role"] == "user":
|
23 |
+
previous_question = messages[i]["content"]
|
24 |
+
if i+1 < len(messages) and messages[i+1]["role"] == "assistant":
|
25 |
+
previous_answer = messages[i+1]["content"]
|
26 |
+
break
|
27 |
+
|
28 |
+
payload = {
|
29 |
+
"model": actual_model,
|
30 |
+
"question": question,
|
31 |
+
"language": "Chinese",
|
32 |
+
"sessionId": session_id,
|
33 |
+
"previousQuestion": previous_question,
|
34 |
+
"previousAnswer": previous_answer
|
35 |
+
}
|
36 |
+
|
37 |
+
headers = {
|
38 |
+
"Content-Type": "application/json",
|
39 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
|
40 |
+
}
|
41 |
+
|
42 |
+
return requests.post(
|
43 |
+
"https://gateway.aiapilab.com/api/ha/v1/chat",
|
44 |
+
json=payload,
|
45 |
+
headers=headers,
|
46 |
+
stream=True
|
47 |
+
)
|
48 |
+
|
49 |
+
def stream_response(question, session_id, messages, request_model, actual_model):
|
50 |
+
resp = make_heck_request(question, session_id, messages, actual_model)
|
51 |
+
is_answering = False
|
52 |
+
|
53 |
+
for line in resp.iter_lines():
|
54 |
+
if line:
|
55 |
+
line = line.decode('utf-8')
|
56 |
+
if not line.startswith('data: '):
|
57 |
+
continue
|
58 |
+
|
59 |
+
content = line[6:].strip()
|
60 |
+
|
61 |
+
if content == "[ANSWER_START]":
|
62 |
+
is_answering = True
|
63 |
+
chunk = {
|
64 |
+
"id": session_id,
|
65 |
+
"object": "chat.completion.chunk",
|
66 |
+
"created": int(time.time()),
|
67 |
+
"model": request_model,
|
68 |
+
"choices": [{
|
69 |
+
"index": 0,
|
70 |
+
"delta": {"role": "assistant"},
|
71 |
+
}]
|
72 |
+
}
|
73 |
+
yield f"data: {json.dumps(chunk, ensure_ascii=False)}\n\n"
|
74 |
+
continue
|
75 |
+
|
76 |
+
if content == "[ANSWER_DONE]":
|
77 |
+
chunk = {
|
78 |
+
"id": session_id,
|
79 |
+
"object": "chat.completion.chunk",
|
80 |
+
"created": int(time.time()),
|
81 |
+
"model": request_model,
|
82 |
+
"choices": [{
|
83 |
+
"index": 0,
|
84 |
+
"delta": {},
|
85 |
+
"finish_reason": "stop"
|
86 |
+
}]
|
87 |
+
}
|
88 |
+
yield f"data: {json.dumps(chunk, ensure_ascii=False)}\n\n"
|
89 |
+
break
|
90 |
+
|
91 |
+
if is_answering and content and not content.startswith("[RELATE_Q"):
|
92 |
+
chunk = {
|
93 |
+
"id": session_id,
|
94 |
+
"object": "chat.completion.chunk",
|
95 |
+
"created": int(time.time()),
|
96 |
+
"model": request_model,
|
97 |
+
"choices": [{
|
98 |
+
"index": 0,
|
99 |
+
"delta": {"content": content},
|
100 |
+
}]
|
101 |
+
}
|
102 |
+
yield f"data: {json.dumps(chunk, ensure_ascii=False)}\n\n"
|
103 |
+
|
104 |
+
def normal_response(question, session_id, messages, request_model, actual_model):
|
105 |
+
resp = make_heck_request(question, session_id, messages, actual_model)
|
106 |
+
full_content = []
|
107 |
+
is_answering = False
|
108 |
+
|
109 |
+
for line in resp.iter_lines():
|
110 |
+
if line:
|
111 |
+
line = line.decode('utf-8')
|
112 |
+
if line.startswith('data: '):
|
113 |
+
content = line[6:].strip()
|
114 |
+
if content == "[ANSWER_START]":
|
115 |
+
is_answering = True
|
116 |
+
elif content == "[ANSWER_DONE]":
|
117 |
+
break
|
118 |
+
elif is_answering:
|
119 |
+
full_content.append(content)
|
120 |
+
|
121 |
+
response = {
|
122 |
+
"id": session_id,
|
123 |
+
"object": "chat.completion",
|
124 |
+
"created": int(time.time()),
|
125 |
+
"model": request_model,
|
126 |
+
"choices": [{
|
127 |
+
"index": 0,
|
128 |
+
"message": {
|
129 |
+
"role": "assistant",
|
130 |
+
"content": "".join(full_content)
|
131 |
+
},
|
132 |
+
"finish_reason": "stop"
|
133 |
+
}]
|
134 |
+
}
|
135 |
+
return response
|
136 |
+
|
137 |
+
@app.route("/hf/v1/chat/completions", methods=["POST"])
|
138 |
+
def chat_completions():
|
139 |
+
# 验证Token
|
140 |
+
if AUTH_TOKEN:
|
141 |
+
auth_header = request.headers.get('Authorization')
|
142 |
+
if not auth_header or auth_header != f"Bearer {AUTH_TOKEN}":
|
143 |
+
return {"error": "Unauthorized"}, 401
|
144 |
+
|
145 |
+
data = request.json
|
146 |
+
model = MODEL_MAPPING.get(data["model"])
|
147 |
+
if not model:
|
148 |
+
return {"error": "Unsupported Model"}, 400
|
149 |
+
|
150 |
+
question = next((msg["content"] for msg in reversed(data["messages"])
|
151 |
+
if msg["role"] == "user"), None)
|
152 |
+
session_id = str(uuid4())
|
153 |
+
|
154 |
+
if data.get("stream"):
|
155 |
+
return Response(
|
156 |
+
stream_response(question, session_id, data["messages"],
|
157 |
+
data["model"], model),
|
158 |
+
mimetype="text/event-stream"
|
159 |
+
)
|
160 |
+
else:
|
161 |
+
return normal_response(question, session_id, data["messages"],
|
162 |
+
data["model"], model)
|
163 |
+
|
164 |
+
if __name__ == "__main__":
|
165 |
+
app.run(host='0.0.0.0', port=7860)
|
requirements.txt
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
-
|
2 |
-
requests
|
3 |
-
|
|
|
1 |
+
flask
|
2 |
+
requests
|
3 |
+
uuid
|