hzruo commited on
Commit
4385e66
·
verified ·
1 Parent(s): 86175fb

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +55 -19
main.py CHANGED
@@ -191,24 +191,60 @@ async def chat_completions(
191
  # 在处理消息时先判断模型类型
192
  if data.get('model') == 'AkashGen' and "<image_generation>" in msg_data:
193
  # 图片生成模型的特殊处理
194
- async def process_and_send():
195
- messages = await process_image_generation(msg_data, session, headers, chat_id)
196
- if messages:
197
- return messages
198
- return None
199
-
200
- # 创建新的事件循环
201
- loop = asyncio.new_event_loop()
202
- asyncio.set_event_loop(loop)
203
- try:
204
- result_messages = loop.run_until_complete(process_and_send())
205
- finally:
206
- loop.close()
207
-
208
- if result_messages:
209
- for message in result_messages:
210
- yield f"data: {json.dumps(message)}\n\n"
211
- continue
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212
 
213
  content_buffer += msg_data
214
 
@@ -230,7 +266,7 @@ async def chat_completions(
230
  "id": f"chatcmpl-{chat_id}",
231
  "object": "chat.completion.chunk",
232
  "created": int(time.time()),
233
- "model": data.get('model'), # 使用请求中指定的模型
234
  "choices": [{
235
  "delta": {},
236
  "index": 0,
 
191
  # 在处理消息时先判断模型类型
192
  if data.get('model') == 'AkashGen' and "<image_generation>" in msg_data:
193
  # 图片生成模型的特殊处理
194
+ match = re.search(r"jobId='([^']+)' prompt='([^']+)' negative='([^']*)'", msg_data)
195
+ if match:
196
+ job_id, prompt, negative = match.groups()
197
+ print(f"Starting image generation process for job_id: {job_id}")
198
+
199
+ # 立即发送思考开始的消息
200
+ start_time = time.time()
201
+ think_msg = "<think>\n"
202
+ think_msg += "🎨 Generating image...\n\n"
203
+ think_msg += f"Prompt: {prompt}\n"
204
+
205
+ # 发送思考开始消息 (使用标准 OpenAI 格式)
206
+ chunk = {
207
+ "id": f"chatcmpl-{chat_id}",
208
+ "object": "chat.completion.chunk",
209
+ "created": int(time.time()),
210
+ "model": data.get('model'),
211
+ "choices": [{
212
+ "delta": {"content": think_msg},
213
+ "index": 0,
214
+ "finish_reason": None
215
+ }]
216
+ }
217
+ yield f"data: {json.dumps(chunk)}\n\n"
218
+
219
+ # 检查图片状态和上传
220
+ result = await upload_to_xinyew(
221
+ await check_image_status(session, job_id, headers),
222
+ job_id
223
+ )
224
+
225
+ # 发送结束消息
226
+ elapsed_time = time.time() - start_time
227
+ end_msg = f"\n🤔 Thinking for {elapsed_time:.1f}s...\n"
228
+ end_msg += "</think>\n\n"
229
+ if result:
230
+ end_msg += f"![Generated Image]({result})"
231
+ else:
232
+ end_msg += "*Image generation or upload failed.*\n"
233
+
234
+ # 发送结束消息 (使用标准 OpenAI 格式)
235
+ chunk = {
236
+ "id": f"chatcmpl-{chat_id}",
237
+ "object": "chat.completion.chunk",
238
+ "created": int(time.time()),
239
+ "model": data.get('model'),
240
+ "choices": [{
241
+ "delta": {"content": end_msg},
242
+ "index": 0,
243
+ "finish_reason": None
244
+ }]
245
+ }
246
+ yield f"data: {json.dumps(chunk)}\n\n"
247
+ continue
248
 
249
  content_buffer += msg_data
250
 
 
266
  "id": f"chatcmpl-{chat_id}",
267
  "object": "chat.completion.chunk",
268
  "created": int(time.time()),
269
+ "model": data.get('model'),
270
  "choices": [{
271
  "delta": {},
272
  "index": 0,