Spaces:
Sleeping
Sleeping
generate_correct_grammatical_spelling_errors
Browse files
app.py
CHANGED
@@ -510,37 +510,62 @@ def update_supporting_sentences_input(supporting_sentences_radio):
|
|
510 |
|
511 |
def generate_conclusion_sentences(model, max_tokens, sys_content, scenario, eng_level, topic, points, topic_sentence, user_generate_conclusion_sentence_prompt):
|
512 |
"""
|
513 |
-
根据系统提示和用户输入的情境、主题、要点、主题句,调用
|
514 |
"""
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
|
|
|
|
528 |
|
529 |
-
|
530 |
-
"
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
535 |
|
536 |
-
try:
|
537 |
-
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
|
538 |
-
response_content = json.loads(response.choices[0].message.content)
|
539 |
-
json_content = response_content["results"]
|
540 |
-
gr_update = gr.update(choices=[json_content], visible=True)
|
541 |
except Exception as e:
|
542 |
-
print(f"An error occurred while generating conclusion sentences: {e}")
|
543 |
-
|
|
|
|
|
|
|
|
|
|
|
544 |
|
545 |
return gr_update
|
546 |
|
@@ -556,103 +581,181 @@ def generate_paragraph(topic_sentence, supporting_sentences, conclusion_sentence
|
|
556 |
|
557 |
def generate_paragraph_evaluate(model, sys_content, paragraph, user_generate_paragraph_evaluate_prompt):
|
558 |
"""
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
564 |
-
|
|
|
|
|
|
|
|
|
565 |
"""
|
566 |
-
|
567 |
-
|
568 |
-
|
569 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
570 |
|
571 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
572 |
|
573 |
-
|
574 |
-
"
|
575 |
-
|
576 |
-
|
577 |
-
|
578 |
-
|
|
|
579 |
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
|
585 |
-
|
|
|
|
|
|
|
586 |
|
587 |
-
|
588 |
-
|
589 |
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
|
596 |
-
|
597 |
-
]
|
598 |
-
|
599 |
-
|
600 |
-
|
601 |
-
|
602 |
-
|
603 |
-
|
604 |
-
if attempt == max_attempts:
|
605 |
-
raise gr.Error("網路塞車,請重新嘗試一次!")
|
606 |
|
607 |
-
|
|
|
|
|
|
|
608 |
|
609 |
def generate_correct_grammatical_spelling_errors(model, sys_content, eng_level, paragraph, user_correct_grammatical_spelling_errors_prompt):
|
610 |
"""
|
611 |
-
|
612 |
-
"""
|
613 |
-
user_content = f"""
|
614 |
-
level is: {eng_level}
|
615 |
-
paragraph is: {paragraph}
|
616 |
-
---
|
617 |
-
{user_correct_grammatical_spelling_errors_prompt}
|
618 |
"""
|
619 |
-
|
620 |
-
|
621 |
-
|
622 |
-
|
623 |
-
|
624 |
-
|
625 |
-
"
|
626 |
-
|
627 |
-
|
628 |
-
|
629 |
-
|
|
|
630 |
|
631 |
-
|
632 |
-
|
633 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
634 |
try:
|
635 |
-
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
|
636 |
-
content = response.choices[0].message.content
|
637 |
data = json.loads(content)
|
638 |
-
print(f"data: {data}")
|
639 |
-
|
640 |
corrections_list = [
|
641 |
[item['original'], item['correction'], item['explanation']]
|
642 |
for item in data['Corrections and Explanations']
|
643 |
]
|
644 |
headers = ["原文", "建議", "解釋"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
645 |
|
646 |
-
corrections_list_gr_update = gr.update(value=corrections_list, headers=headers, wrap=True, visible=True)
|
647 |
-
reverse_paragraph_gr_update = gr.update(value=data["Revised Paragraph"], visible=False)
|
648 |
-
break
|
649 |
-
except Exception as e:
|
650 |
-
print(f"An error occurred while generating correct grammatical spelling errors: {e}")
|
651 |
-
attempt += 1
|
652 |
-
if attempt == max_attempts:
|
653 |
-
raise gr.Error("網路塞車,請重新嘗試一次!")
|
654 |
-
|
655 |
-
return corrections_list_gr_update, reverse_paragraph_gr_update
|
656 |
|
657 |
def highlight_diff_texts(highlight_list, text):
|
658 |
# Convert DataFrame to JSON string
|
|
|
510 |
|
511 |
def generate_conclusion_sentences(model, max_tokens, sys_content, scenario, eng_level, topic, points, topic_sentence, user_generate_conclusion_sentence_prompt):
|
512 |
"""
|
513 |
+
根据系统提示和用户输入的情境、主题、要点、主题句,调用 LLM API 生成相关的结论句。
|
514 |
"""
|
515 |
+
try:
|
516 |
+
user_content = f"""
|
517 |
+
scenario is: {scenario}
|
518 |
+
english level is: {eng_level}
|
519 |
+
topic is: {topic}
|
520 |
+
points is: {points}
|
521 |
+
topic sentence is: {topic_sentence}
|
522 |
+
---
|
523 |
+
{user_generate_conclusion_sentence_prompt}
|
524 |
+
"""
|
525 |
+
|
526 |
+
messages = [
|
527 |
+
{"role": "system", "content": sys_content},
|
528 |
+
{"role": "user", "content": user_content}
|
529 |
+
]
|
530 |
|
531 |
+
# 根據模型選擇 provider
|
532 |
+
if "gemini" in model.lower():
|
533 |
+
print("====gemini====")
|
534 |
+
provider = GeminiProvider()
|
535 |
+
else:
|
536 |
+
print("====openai====")
|
537 |
+
provider = OpenAIProvider(OPEN_AI_CLIENT)
|
538 |
+
|
539 |
+
# 使用 LLMService 處理請求
|
540 |
+
llm_service = LLMService(provider)
|
541 |
+
content = llm_service.chat(
|
542 |
+
prompt=f"{sys_content}\n{user_content}" if "gemini" in model.lower() else None,
|
543 |
+
messages=messages,
|
544 |
+
model=model,
|
545 |
+
max_tokens=max_tokens,
|
546 |
+
response_format={"type": "json_object"}
|
547 |
+
)
|
548 |
+
|
549 |
+
# 處理回應格式
|
550 |
+
if isinstance(content, str) and "```json" in content:
|
551 |
+
content = content.replace("```json", "").replace("```", "")
|
552 |
+
|
553 |
+
try:
|
554 |
+
response_content = json.loads(content)
|
555 |
+
json_content = response_content["results"]
|
556 |
+
gr_update = gr.update(choices=[json_content], visible=True)
|
557 |
+
except (json.JSONDecodeError, KeyError, ValueError) as e:
|
558 |
+
print(f"Error parsing conclusion sentences: {e}")
|
559 |
+
raise gr.Error("無法解析結論句,請重新嘗試")
|
560 |
|
|
|
|
|
|
|
|
|
|
|
561 |
except Exception as e:
|
562 |
+
print(f"An error occurred while generating conclusion sentences: {str(e)}")
|
563 |
+
error_msg = "網路塞車,請重新嘗試一次!"
|
564 |
+
if "rate limit" in str(e).lower():
|
565 |
+
error_msg = "請求過於頻繁,請稍後再試"
|
566 |
+
elif "invalid_request_error" in str(e).lower():
|
567 |
+
error_msg = "請求格式錯誤,請檢查輸入"
|
568 |
+
raise gr.Error(error_msg)
|
569 |
|
570 |
return gr_update
|
571 |
|
|
|
581 |
|
582 |
def generate_paragraph_evaluate(model, sys_content, paragraph, user_generate_paragraph_evaluate_prompt):
|
583 |
"""
|
584 |
+
根據用戶輸入的段落,調用 LLM API 生成相關的段落分析。
|
585 |
+
|
586 |
+
Args:
|
587 |
+
model (str): 使用的 LLM 模型名稱
|
588 |
+
sys_content (str): 系統提示內容
|
589 |
+
paragraph (str): 要評估的段落
|
590 |
+
user_generate_paragraph_evaluate_prompt (str): 用戶評估提示
|
591 |
+
|
592 |
+
Returns:
|
593 |
+
gr.update: 包含評估結果的表格更新
|
594 |
"""
|
595 |
+
def parse_evaluation_response(content):
|
596 |
+
"""解析 LLM 回應內容"""
|
597 |
+
try:
|
598 |
+
if isinstance(content, str):
|
599 |
+
# 處理可能的 markdown 格式
|
600 |
+
if "```json" in content:
|
601 |
+
json_blocks = content.split("```json")
|
602 |
+
for block in json_blocks:
|
603 |
+
if "```" in block:
|
604 |
+
content = block.split("```")[0].strip()
|
605 |
+
else:
|
606 |
+
content = block.strip()
|
607 |
+
|
608 |
+
# 清理內容確保是有效的 JSON
|
609 |
+
content = content.strip()
|
610 |
+
if content.startswith("```") and content.endswith("```"):
|
611 |
+
content = content[3:-3].strip()
|
612 |
+
|
613 |
+
print(f"Cleaned content: {content}")
|
614 |
+
return json.loads(content)
|
615 |
+
return content
|
616 |
+
except (json.JSONDecodeError, KeyError, ValueError) as e:
|
617 |
+
print(f"Error parsing evaluation response: {e}")
|
618 |
+
raise ValueError("無法解析評估結果")
|
619 |
+
|
620 |
+
def handle_evaluation_error(error):
|
621 |
+
"""處理評估過程中的錯誤"""
|
622 |
+
if "rate limit" in str(error).lower():
|
623 |
+
return "請求過於頻繁,請稍後再試"
|
624 |
+
elif "invalid_request_error" in str(error).lower():
|
625 |
+
return "請求格式錯誤,請檢查輸入"
|
626 |
+
elif isinstance(error, ValueError):
|
627 |
+
return str(error)
|
628 |
+
return "網路塞車,請重新嘗試一次!"
|
629 |
|
630 |
+
try:
|
631 |
+
# 準備請求內容
|
632 |
+
user_content = f"""
|
633 |
+
paragraph is: {paragraph}
|
634 |
+
---
|
635 |
+
{user_generate_paragraph_evaluate_prompt}
|
636 |
+
"""
|
637 |
+
|
638 |
+
messages = [
|
639 |
+
{"role": "system", "content": sys_content},
|
640 |
+
{"role": "user", "content": user_content}
|
641 |
+
]
|
642 |
|
643 |
+
# 根據模型選擇 provider
|
644 |
+
if "gemini" in model.lower():
|
645 |
+
print("====gemini====")
|
646 |
+
provider = GeminiProvider()
|
647 |
+
else:
|
648 |
+
print("====openai====")
|
649 |
+
provider = OpenAIProvider(OPEN_AI_CLIENT)
|
650 |
|
651 |
+
# 使用 LLMService 處理請求
|
652 |
+
llm_service = LLMService(provider)
|
653 |
+
content = llm_service.chat(
|
654 |
+
prompt=f"{sys_content}\n{user_content}" if "gemini" in model.lower() else None,
|
655 |
+
messages=messages,
|
656 |
+
model=model,
|
657 |
+
max_tokens=2000,
|
658 |
+
response_format={"type": "json_object"}
|
659 |
+
)
|
660 |
|
661 |
+
print(f"====generate_paragraph_evaluate====")
|
662 |
+
print(content)
|
663 |
|
664 |
+
# 解析回應內容
|
665 |
+
data = parse_evaluation_response(content)
|
666 |
+
|
667 |
+
# 準備表格數據
|
668 |
+
table_data = [
|
669 |
+
["學測架構|內容(Content)", data['content']['level'], data['content']['explanation']],
|
670 |
+
["學測架構|組織(Organization)", data['organization']['level'], data['organization']['explanation']],
|
671 |
+
["學測架構|文法、句構(Grammar/Sentence Structure)", data['grammar_and_usage']['level'], data['grammar_and_usage']['explanation']],
|
672 |
+
["學測架構|字彙、拼字(Vocabulary/Spelling)", data['vocabulary']['level'], data['vocabulary']['explanation']],
|
673 |
+
["JUTOR 架構|連貫性和連接詞(Coherence and Cohesion)", data['coherence_and_cohesion']['level'], data['coherence_and_cohesion']['explanation']]
|
674 |
+
]
|
675 |
+
headers = ["架構", "評分", "解釋"]
|
676 |
+
|
677 |
+
return gr.update(value=table_data, headers=headers, visible=True)
|
|
|
|
|
678 |
|
679 |
+
except Exception as e:
|
680 |
+
print(f"An error occurred while generating paragraph evaluate: {str(e)}")
|
681 |
+
error_msg = handle_evaluation_error(e)
|
682 |
+
raise gr.Error(error_msg)
|
683 |
|
684 |
def generate_correct_grammatical_spelling_errors(model, sys_content, eng_level, paragraph, user_correct_grammatical_spelling_errors_prompt):
|
685 |
"""
|
686 |
+
根據用戶輸入的段落,生成文法和拼字錯誤修正建議。
|
|
|
|
|
|
|
|
|
|
|
|
|
687 |
"""
|
688 |
+
try:
|
689 |
+
user_content = f"""
|
690 |
+
level is: {eng_level}
|
691 |
+
paragraph is: {paragraph}
|
692 |
+
---
|
693 |
+
{user_correct_grammatical_spelling_errors_prompt}
|
694 |
+
"""
|
695 |
+
|
696 |
+
messages = [
|
697 |
+
{"role": "system", "content": sys_content},
|
698 |
+
{"role": "user", "content": user_content}
|
699 |
+
]
|
700 |
|
701 |
+
# 根據模型選擇 provider
|
702 |
+
if "gemini" in model.lower():
|
703 |
+
print("====gemini====")
|
704 |
+
provider = GeminiProvider()
|
705 |
+
else:
|
706 |
+
print("====openai====")
|
707 |
+
provider = OpenAIProvider(OPEN_AI_CLIENT)
|
708 |
+
|
709 |
+
# 使用 LLMService 處理請求
|
710 |
+
llm_service = LLMService(provider)
|
711 |
+
content = llm_service.chat(
|
712 |
+
prompt=f"{sys_content}\n{user_content}" if "gemini" in model.lower() else None,
|
713 |
+
messages=messages,
|
714 |
+
model=model,
|
715 |
+
max_tokens=1000,
|
716 |
+
response_format={"type": "json_object"}
|
717 |
+
)
|
718 |
+
|
719 |
+
# 處理回應格式
|
720 |
+
if isinstance(content, str) and "```json" in content:
|
721 |
+
content = content.replace("```json", "").replace("```", "")
|
722 |
+
|
723 |
try:
|
|
|
|
|
724 |
data = json.loads(content)
|
|
|
|
|
725 |
corrections_list = [
|
726 |
[item['original'], item['correction'], item['explanation']]
|
727 |
for item in data['Corrections and Explanations']
|
728 |
]
|
729 |
headers = ["原文", "建議", "解釋"]
|
730 |
+
|
731 |
+
corrections_list_gr_update = gr.update(
|
732 |
+
value=corrections_list,
|
733 |
+
headers=headers,
|
734 |
+
wrap=True,
|
735 |
+
visible=True
|
736 |
+
)
|
737 |
+
revised_paragraph_gr_update = gr.update(
|
738 |
+
value=data["Revised Paragraph"],
|
739 |
+
visible=False
|
740 |
+
)
|
741 |
+
|
742 |
+
return corrections_list_gr_update, revised_paragraph_gr_update
|
743 |
+
|
744 |
+
except (json.JSONDecodeError, KeyError, ValueError) as e:
|
745 |
+
print(f"Error parsing corrections: {e}")
|
746 |
+
raise gr.Error("無法解析修正建議,請重新嘗試")
|
747 |
+
|
748 |
+
except Exception as e:
|
749 |
+
print(f"An error occurred while generating corrections: {str(e)}")
|
750 |
+
error_msg = "網路塞車,請重新嘗試一次!"
|
751 |
+
if "rate limit" in str(e).lower():
|
752 |
+
error_msg = "請求過於頻繁,請稍後再試"
|
753 |
+
elif "invalid_request_error" in str(e).lower():
|
754 |
+
error_msg = "請求格式錯誤,請檢查輸入"
|
755 |
+
elif "model_not_found" in str(e).lower():
|
756 |
+
error_msg = "選擇的模型不存在或無法存取"
|
757 |
+
raise gr.Error(error_msg)
|
758 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
759 |
|
760 |
def highlight_diff_texts(highlight_list, text):
|
761 |
# Convert DataFrame to JSON string
|