Update app.py
Browse files
app.py
CHANGED
@@ -146,32 +146,83 @@ def chat_completions():
|
|
146 |
}
|
147 |
)
|
148 |
else:
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
"
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
except Exception as e:
|
177 |
print(e)
|
|
|
146 |
}
|
147 |
)
|
148 |
else:
|
149 |
+
if data.get('model', "DeepSeek-R1") != "AkashGen":
|
150 |
+
text_matches = re.findall(r'0:"(.*?)"', response.text)
|
151 |
+
parsed_text = "".join(text_matches)
|
152 |
+
|
153 |
+
return Response(
|
154 |
+
json.dumps({
|
155 |
+
"object": "chat.completion",
|
156 |
+
"created": int(time.time() * 1000),
|
157 |
+
"model": data.get('model', "DeepSeek-R1"),
|
158 |
+
"choices": [
|
159 |
+
{
|
160 |
+
"index": 0,
|
161 |
+
"message": {
|
162 |
+
"role": "user",
|
163 |
+
"content": parsed_text
|
164 |
+
},
|
165 |
+
"finish_reason": "stop"
|
166 |
+
}
|
167 |
+
]
|
168 |
+
},ensure_ascii=False),
|
169 |
+
status=response.status_code,
|
170 |
+
headers={
|
171 |
+
'Cache-Control': 'no-cache',
|
172 |
+
'Connection': 'keep-alive',
|
173 |
+
'Content-Type': 'application/json'
|
174 |
+
}
|
175 |
+
)
|
176 |
+
else:
|
177 |
+
match = re.search(r"jobId='([^']+)'", response.text)
|
178 |
+
job_id = None
|
179 |
+
if match:
|
180 |
+
job_id = match.group(1)
|
181 |
+
print("jobId:", job_id)
|
182 |
+
while True:
|
183 |
+
try:
|
184 |
+
_img_response = requests.get(
|
185 |
+
'https://chat.akash.network/api/image-status?ids='+job_id,
|
186 |
+
headers=headers
|
187 |
+
)
|
188 |
+
_data = _img_response.json()
|
189 |
+
# 检查响应状态码是否为200
|
190 |
+
if _data[0]["status"] == "completed":
|
191 |
+
print("图片生成完成:", job_id)
|
192 |
+
return Response(
|
193 |
+
json.dumps({
|
194 |
+
"object": "chat.completion",
|
195 |
+
"created": int(time.time() * 1000),
|
196 |
+
"model": data.get('model', "DeepSeek-R1"),
|
197 |
+
"choices": [
|
198 |
+
{
|
199 |
+
"index": 0,
|
200 |
+
"message": {
|
201 |
+
"role": "user",
|
202 |
+
"content": _data[0]["result"]
|
203 |
+
},
|
204 |
+
"finish_reason": "stop"
|
205 |
+
}
|
206 |
+
]
|
207 |
+
},ensure_ascii=False),
|
208 |
+
status=response.status_code,
|
209 |
+
headers={
|
210 |
+
'Cache-Control': 'no-cache',
|
211 |
+
'Connection': 'keep-alive',
|
212 |
+
'Content-Type': 'application/json'
|
213 |
+
}
|
214 |
+
)
|
215 |
+
else:
|
216 |
+
print("图片生成中:", job_id)
|
217 |
+
except Exception as e:
|
218 |
+
print("请求过程中出现异常:", e)
|
219 |
+
|
220 |
+
# 每隔5秒请求一次
|
221 |
+
time.sleep(5)
|
222 |
+
else:
|
223 |
+
return jsonify({"error": "当前官方服务异常"}), 500
|
224 |
+
|
225 |
+
|
226 |
|
227 |
except Exception as e:
|
228 |
print(e)
|