Update app.py
Browse files
app.py
CHANGED
@@ -4,12 +4,22 @@ import uuid
|
|
4 |
import json
|
5 |
import time
|
6 |
import os
|
|
|
7 |
|
8 |
_COOKIES = os.environ.get("COOKIES", "")
|
9 |
-
|
|
|
|
|
10 |
|
11 |
app = Flask(__name__)
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
@app.route('/v1/chat/completions', methods=['POST'])
|
14 |
def chat_completions():
|
15 |
print("start")
|
@@ -34,8 +44,6 @@ def chat_completions():
|
|
34 |
"Content-Type": "application/json",
|
35 |
"Cookie": _COOKIES
|
36 |
}
|
37 |
-
print(headers)
|
38 |
-
print(akash_data)
|
39 |
_stream=data.get('stream', True)
|
40 |
# 发送请求到Akash API
|
41 |
response = requests.post(
|
@@ -111,9 +119,25 @@ def chat_completions():
|
|
111 |
}
|
112 |
)
|
113 |
else:
|
114 |
-
|
|
|
|
|
115 |
return Response(
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
status=response.status_code,
|
118 |
headers={
|
119 |
'Cache-Control': 'no-cache',
|
@@ -123,6 +147,7 @@ def chat_completions():
|
|
123 |
)
|
124 |
|
125 |
except Exception as e:
|
|
|
126 |
return jsonify({"error": str(e)}), 500
|
127 |
|
128 |
if __name__ == '__main__':
|
|
|
4 |
import json
|
5 |
import time
|
6 |
import os
|
7 |
+
import re
|
8 |
|
9 |
_COOKIES = os.environ.get("COOKIES", "")
|
10 |
+
#_COOKIES = "cf_clearance=toCFbLwd4RZKgHIv_nY.7B1yXUkIfvh0QwWBzxpLa5w-1740730551-1.2.1.1-yasz4kMVvPBtgGz06uRNFlDE1VXXZ2McAq65AKeidqAagCe4ZQ5desj88RDdSZAZ3CG4tJZrZ7pVxTCUDHnvGy96C6_ocRUQc3.qbbsWyeLwHNCD2OZjtVqkVI5MHiNFarJv8qGd9uOriKAytgttX.vMYbEOstOnMGvY5DnjODPDUWDlOwwNrshAsghJbf8B32X9VokGduYk1FvqqPhs85Eq4zdHv83tGKFSpTidYD7FmQoxwiv77T2kyeBXX.6.4ExZiHE9DszGcWZ9_FO8Ub5U2zod0S9U0oqncQwQVXM; session_token=82673f97181bb5da073f2d8771f5b4cd9208f15e0df4c29c74fd849f729249e0"
|
11 |
+
|
12 |
+
API_KEY = os.getenv("API_KEY", "linux.do")
|
13 |
|
14 |
app = Flask(__name__)
|
15 |
|
16 |
+
@app.before_request
|
17 |
+
def check_api_key():
|
18 |
+
key = request.headers.get("Authorization")
|
19 |
+
if key != "Bearer "+API_KEY:
|
20 |
+
return jsonify({"success": False, "message": "Unauthorized: Invalid API key"}), 403
|
21 |
+
|
22 |
+
|
23 |
@app.route('/v1/chat/completions', methods=['POST'])
|
24 |
def chat_completions():
|
25 |
print("start")
|
|
|
44 |
"Content-Type": "application/json",
|
45 |
"Cookie": _COOKIES
|
46 |
}
|
|
|
|
|
47 |
_stream=data.get('stream', True)
|
48 |
# 发送请求到Akash API
|
49 |
response = requests.post(
|
|
|
119 |
}
|
120 |
)
|
121 |
else:
|
122 |
+
text_matches = re.findall(r'0:"(.*?)"', response.text)
|
123 |
+
parsed_text = "".join(text_matches)
|
124 |
+
|
125 |
return Response(
|
126 |
+
json.dumps({
|
127 |
+
"object": "chat.completion",
|
128 |
+
"created": int(time.time() * 1000),
|
129 |
+
"model": data.get('model', "DeepSeek-R1"),
|
130 |
+
"choices": [
|
131 |
+
{
|
132 |
+
"index": 0,
|
133 |
+
"message": {
|
134 |
+
"role": "user",
|
135 |
+
"content": parsed_text
|
136 |
+
},
|
137 |
+
"finish_reason": "stop"
|
138 |
+
}
|
139 |
+
]
|
140 |
+
},ensure_ascii=False),
|
141 |
status=response.status_code,
|
142 |
headers={
|
143 |
'Cache-Control': 'no-cache',
|
|
|
147 |
)
|
148 |
|
149 |
except Exception as e:
|
150 |
+
print(e)
|
151 |
return jsonify({"error": str(e)}), 500
|
152 |
|
153 |
if __name__ == '__main__':
|