Spaces:
Paused
Paused
OraCatQAQ commited on
Commit ·
06794c7
1
Parent(s): a621ff2
增加多次验证
Browse files
app.py
CHANGED
|
@@ -321,7 +321,8 @@ async def stream_openai_response(response, request_id: str, model: str, api_key,
|
|
| 321 |
full_reasoning = "" # 添加思维链内容累积变量
|
| 322 |
conversation_id = None # 会话ID
|
| 323 |
captcha_base64 = None # 验证码图片
|
| 324 |
-
|
|
|
|
| 325 |
|
| 326 |
try:
|
| 327 |
# 使用iter_content替代iter_lines
|
|
@@ -348,13 +349,26 @@ async def stream_openai_response(response, request_id: str, model: str, api_key,
|
|
| 348 |
content = data.get('data', {}).get('content', '')
|
| 349 |
reasoning_content = data.get('data', {}).get('reasoning_content', '')
|
| 350 |
|
| 351 |
-
# 检测是否含有验证码
|
| 352 |
-
if
|
| 353 |
-
|
|
|
|
| 354 |
logger.info("检测到验证码响应")
|
| 355 |
captcha_base64 = extract_captcha_image(content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
|
| 357 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
original_captcha_message = {
|
| 359 |
"id": f"chatcmpl-{request_id}",
|
| 360 |
"object": "chat.completion.chunk",
|
|
@@ -364,7 +378,7 @@ async def stream_openai_response(response, request_id: str, model: str, api_key,
|
|
| 364 |
{
|
| 365 |
"index": 0,
|
| 366 |
"delta": {
|
| 367 |
-
"content":
|
| 368 |
},
|
| 369 |
"finish_reason": None
|
| 370 |
}
|
|
@@ -372,7 +386,7 @@ async def stream_openai_response(response, request_id: str, model: str, api_key,
|
|
| 372 |
}
|
| 373 |
yield f"data: {json.dumps(original_captcha_message)}\n\n"
|
| 374 |
|
| 375 |
-
#
|
| 376 |
captcha_message = {
|
| 377 |
"id": f"chatcmpl-{request_id}",
|
| 378 |
"object": "chat.completion.chunk",
|
|
@@ -389,52 +403,8 @@ async def stream_openai_response(response, request_id: str, model: str, api_key,
|
|
| 389 |
]
|
| 390 |
}
|
| 391 |
yield f"data: {json.dumps(captcha_message)}\n\n"
|
| 392 |
-
|
| 393 |
-
if content:
|
| 394 |
-
full_response += content
|
| 395 |
-
# 如果在本流中检测到了验证码,则不发送实际内容,直到验证码处理完毕
|
| 396 |
-
if not captcha_detected_in_stream:
|
| 397 |
-
chunk_content = {
|
| 398 |
-
"id": f"chatcmpl-{request_id}",
|
| 399 |
-
"object": "chat.completion.chunk",
|
| 400 |
-
"created": timestamp,
|
| 401 |
-
"model": model,
|
| 402 |
-
"choices": [
|
| 403 |
-
{
|
| 404 |
-
"index": 0,
|
| 405 |
-
"delta": {
|
| 406 |
-
"content": content
|
| 407 |
-
},
|
| 408 |
-
"finish_reason": None
|
| 409 |
-
}
|
| 410 |
-
]
|
| 411 |
-
}
|
| 412 |
-
yield f"data: {json.dumps(chunk_content)}\n\n"
|
| 413 |
-
|
| 414 |
-
# 处理思维链内容 (始终发送)
|
| 415 |
-
if reasoning_content:
|
| 416 |
-
full_reasoning += reasoning_content
|
| 417 |
-
reasoning_chunk = {
|
| 418 |
-
"id": f"chatcmpl-{request_id}",
|
| 419 |
-
"object": "chat.completion.chunk",
|
| 420 |
-
"created": timestamp,
|
| 421 |
-
"model": model,
|
| 422 |
-
"choices": [
|
| 423 |
-
{
|
| 424 |
-
"index": 0,
|
| 425 |
-
"delta": {
|
| 426 |
-
"reasoning_content": reasoning_content
|
| 427 |
-
},
|
| 428 |
-
"finish_reason": None
|
| 429 |
-
}
|
| 430 |
-
]
|
| 431 |
-
}
|
| 432 |
-
yield f"data: {json.dumps(reasoning_chunk)}\n\n"
|
| 433 |
|
| 434 |
-
|
| 435 |
-
# 如果在本流检测到验证码且会话结束,处理验证码
|
| 436 |
-
# 确保只在原始流(非递归调用)中处理
|
| 437 |
-
if not is_post_captcha and captcha_detected_in_stream and captcha_base64 and conversation_id:
|
| 438 |
captcha_text = recognize_captcha(captcha_base64)
|
| 439 |
|
| 440 |
if captcha_text:
|
|
@@ -537,12 +507,13 @@ async def stream_openai_response(response, request_id: str, model: str, api_key,
|
|
| 537 |
}
|
| 538 |
yield f"data: {json.dumps(captcha_submitted_message)}\n\n"
|
| 539 |
|
| 540 |
-
#
|
|
|
|
| 541 |
async for chunk_after_captcha in stream_openai_response(
|
| 542 |
-
captcha_response, request_id, model, api_key, token_index, deepsider_model
|
| 543 |
):
|
| 544 |
yield chunk_after_captcha
|
| 545 |
-
return
|
| 546 |
else:
|
| 547 |
# 验证码识别失败的处理
|
| 548 |
error_msg = "\n[验证码识别失败,请重试]"
|
|
@@ -564,9 +535,48 @@ async def stream_openai_response(response, request_id: str, model: str, api_key,
|
|
| 564 |
yield f"data: {json.dumps(error_chunk)}\n\n"
|
| 565 |
yield "data: [DONE]\n\n"
|
| 566 |
return
|
| 567 |
-
|
| 568 |
-
#
|
| 569 |
-
if not
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 570 |
final_chunk = {
|
| 571 |
"id": f"chatcmpl-{request_id}",
|
| 572 |
"object": "chat.completion.chunk",
|
|
|
|
| 321 |
full_reasoning = "" # 添加思维链内容累积变量
|
| 322 |
conversation_id = None # 会话ID
|
| 323 |
captcha_base64 = None # 验证码图片
|
| 324 |
+
captcha_detected = False # 验证码检测标志
|
| 325 |
+
captcha_content = "" # 验证码响应内容
|
| 326 |
|
| 327 |
try:
|
| 328 |
# 使用iter_content替代iter_lines
|
|
|
|
| 349 |
content = data.get('data', {}).get('content', '')
|
| 350 |
reasoning_content = data.get('data', {}).get('reasoning_content', '')
|
| 351 |
|
| 352 |
+
# 检测是否含有验证码
|
| 353 |
+
if "验证码提示" in content and "
|
| 357 |
captcha_base64 = extract_captcha_image(content)
|
| 358 |
+
|
| 359 |
+
# 累积非验证码响应内容
|
| 360 |
+
if not captcha_detected:
|
| 361 |
+
full_response += content
|
| 362 |
+
|
| 363 |
+
# 处理思维链内容
|
| 364 |
+
if reasoning_content:
|
| 365 |
+
full_reasoning += reasoning_content
|
| 366 |
|
| 367 |
+
# 当整个响应结束时处理验证码
|
| 368 |
+
elif data.get('code') == 203:
|
| 369 |
+
# 如果检测到验证码,进行验证码处理
|
| 370 |
+
if captcha_detected and captcha_base64 and conversation_id:
|
| 371 |
+
# 先向客户端发送验证码响应
|
| 372 |
original_captcha_message = {
|
| 373 |
"id": f"chatcmpl-{request_id}",
|
| 374 |
"object": "chat.completion.chunk",
|
|
|
|
| 378 |
{
|
| 379 |
"index": 0,
|
| 380 |
"delta": {
|
| 381 |
+
"content": captcha_content
|
| 382 |
},
|
| 383 |
"finish_reason": None
|
| 384 |
}
|
|
|
|
| 386 |
}
|
| 387 |
yield f"data: {json.dumps(original_captcha_message)}\n\n"
|
| 388 |
|
| 389 |
+
# 显示自动识别提示
|
| 390 |
captcha_message = {
|
| 391 |
"id": f"chatcmpl-{request_id}",
|
| 392 |
"object": "chat.completion.chunk",
|
|
|
|
| 403 |
]
|
| 404 |
}
|
| 405 |
yield f"data: {json.dumps(captcha_message)}\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
|
| 407 |
+
# 识别验证码
|
|
|
|
|
|
|
|
|
|
| 408 |
captcha_text = recognize_captcha(captcha_base64)
|
| 409 |
|
| 410 |
if captcha_text:
|
|
|
|
| 507 |
}
|
| 508 |
yield f"data: {json.dumps(captcha_submitted_message)}\n\n"
|
| 509 |
|
| 510 |
+
# 处理验证码后的响应(可能还有验证码)
|
| 511 |
+
# 创建一个新的stream_openai_response流,但检测是否还有验证码
|
| 512 |
async for chunk_after_captcha in stream_openai_response(
|
| 513 |
+
captcha_response, request_id, model, api_key, token_index, deepsider_model
|
| 514 |
):
|
| 515 |
yield chunk_after_captcha
|
| 516 |
+
return
|
| 517 |
else:
|
| 518 |
# 验证码识别失败的处理
|
| 519 |
error_msg = "\n[验证码识别失败,请重试]"
|
|
|
|
| 535 |
yield f"data: {json.dumps(error_chunk)}\n\n"
|
| 536 |
yield "data: [DONE]\n\n"
|
| 537 |
return
|
| 538 |
+
|
| 539 |
+
# 非验证码响应,直接流式输出到目前为止收集的内容
|
| 540 |
+
if not captcha_detected:
|
| 541 |
+
# 流式输出响应内容
|
| 542 |
+
if full_response:
|
| 543 |
+
content_chunk = {
|
| 544 |
+
"id": f"chatcmpl-{request_id}",
|
| 545 |
+
"object": "chat.completion.chunk",
|
| 546 |
+
"created": timestamp,
|
| 547 |
+
"model": model,
|
| 548 |
+
"choices": [
|
| 549 |
+
{
|
| 550 |
+
"index": 0,
|
| 551 |
+
"delta": {
|
| 552 |
+
"content": full_response
|
| 553 |
+
},
|
| 554 |
+
"finish_reason": None
|
| 555 |
+
}
|
| 556 |
+
]
|
| 557 |
+
}
|
| 558 |
+
yield f"data: {json.dumps(content_chunk)}\n\n"
|
| 559 |
+
|
| 560 |
+
# 流式输出思维链内容(如果有)
|
| 561 |
+
if full_reasoning:
|
| 562 |
+
reasoning_chunk = {
|
| 563 |
+
"id": f"chatcmpl-{request_id}",
|
| 564 |
+
"object": "chat.completion.chunk",
|
| 565 |
+
"created": timestamp,
|
| 566 |
+
"model": model,
|
| 567 |
+
"choices": [
|
| 568 |
+
{
|
| 569 |
+
"index": 0,
|
| 570 |
+
"delta": {
|
| 571 |
+
"reasoning_content": full_reasoning
|
| 572 |
+
},
|
| 573 |
+
"finish_reason": None
|
| 574 |
+
}
|
| 575 |
+
]
|
| 576 |
+
}
|
| 577 |
+
yield f"data: {json.dumps(reasoning_chunk)}\n\n"
|
| 578 |
+
|
| 579 |
+
# 发送完成信号
|
| 580 |
final_chunk = {
|
| 581 |
"id": f"chatcmpl-{request_id}",
|
| 582 |
"object": "chat.completion.chunk",
|