Spaces:
Sleeping
Sleeping
chinese_full_paragraph_evaluate_output_text_history = content_json["chinese_full_paragraph_evaluate_output_text"] if "chinese_full_paragraph_evaluate_output_text" in content_json else ""
Browse files
app.py
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
-
|
2 |
-
from docx import Document
|
3 |
import gradio as gr
|
4 |
from openai import OpenAI
|
5 |
|
|
|
|
|
6 |
import random
|
7 |
import uuid
|
8 |
import json
|
@@ -12,12 +13,10 @@ import json
|
|
12 |
import tempfile
|
13 |
import urllib.parse
|
14 |
import pandas as pd
|
15 |
-
|
16 |
|
17 |
from storage_service import GoogleCloudStorage
|
18 |
|
19 |
-
|
20 |
-
|
21 |
is_env_local = os.getenv("IS_ENV_LOCAL", "false") == "true"
|
22 |
print(f"is_env_local: {is_env_local}")
|
23 |
|
@@ -28,11 +27,16 @@ if is_env_local:
|
|
28 |
IS_ENV_PROD = "False"
|
29 |
OPEN_AI_KEY = config["OPEN_AI_KEY"]
|
30 |
GCS_KEY = json.dumps(config["GOOGLE_APPLICATION_CREDENTIALS_JSON"])
|
|
|
|
|
31 |
else:
|
32 |
OPEN_AI_KEY = os.getenv("OPEN_AI_KEY")
|
33 |
GCS_KEY = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
|
|
|
|
|
34 |
|
35 |
OPEN_AI_CLIENT = OpenAI(api_key=OPEN_AI_KEY)
|
|
|
36 |
|
37 |
# 设置 Google Cloud Storage 客户端
|
38 |
GCS_SERVICE = GoogleCloudStorage(GCS_KEY)
|
@@ -845,46 +849,46 @@ def get_past_exam_practice_log_session_content(file_name):
|
|
845 |
past_exam_save_output
|
846 |
|
847 |
# === Chinese ===
|
848 |
-
def generate_chinese_evaluation_table(model, sys_content, user_prompt, text):
|
849 |
-
|
850 |
-
|
851 |
-
|
852 |
-
|
853 |
-
|
854 |
-
|
855 |
-
|
856 |
-
|
857 |
-
|
858 |
-
|
859 |
-
|
860 |
-
|
861 |
-
|
862 |
-
|
863 |
-
|
864 |
-
|
865 |
-
|
866 |
-
|
867 |
-
|
868 |
-
|
869 |
-
|
870 |
-
|
871 |
-
|
872 |
-
|
873 |
-
|
874 |
-
|
875 |
-
|
876 |
-
|
877 |
-
|
878 |
-
|
879 |
-
|
880 |
-
|
881 |
-
|
882 |
-
|
883 |
-
|
884 |
|
885 |
-
|
886 |
|
887 |
-
|
888 |
|
889 |
def load_exam_data():
|
890 |
with open("exams.json", "r") as file:
|
@@ -902,6 +906,7 @@ def generate_chinese_paragraph_practice_history(
|
|
902 |
session_timestamp,
|
903 |
request_origin,
|
904 |
chinese_full_paragraph_input,
|
|
|
905 |
chinese_full_paragraph_evaluate_output,
|
906 |
chinese_full_paragraph_correct_grammatical_spelling_errors_input,
|
907 |
chinese_full_paragraph_correct_grammatical_spelling_errors_output_table,
|
@@ -918,6 +923,7 @@ def generate_chinese_paragraph_practice_history(
|
|
918 |
"session_timestamp": session_timestamp,
|
919 |
"request_origin": request_origin,
|
920 |
"full_paragraph_input": chinese_full_paragraph_input,
|
|
|
921 |
"full_paragraph_evaluate_output": chinese_full_paragraph_evaluate_output.to_dict(orient='records'),
|
922 |
"full_paragraph_correct_grammatical_spelling_errors_input": chinese_full_paragraph_correct_grammatical_spelling_errors_input,
|
923 |
"full_paragraph_correct_grammatical_spelling_errors_output_table": chinese_full_paragraph_correct_grammatical_spelling_errors_output_table.to_dict(orient='records'),
|
@@ -929,6 +935,7 @@ def generate_chinese_paragraph_practice_history(
|
|
929 |
GCS_SERVICE.upload_json_string("jutor_logs", file_name, json.dumps(content))
|
930 |
|
931 |
return chinese_full_paragraph_input, \
|
|
|
932 |
chinese_full_paragraph_evaluate_output, \
|
933 |
chinese_full_paragraph_correct_grammatical_spelling_errors_input, \
|
934 |
chinese_full_paragraph_correct_grammatical_spelling_errors_output_table, \
|
@@ -937,14 +944,13 @@ def generate_chinese_paragraph_practice_history(
|
|
937 |
chinese_full_paragraph_refine_output, \
|
938 |
chinese_full_paragraph_save_output
|
939 |
|
940 |
-
|
941 |
-
|
942 |
def get_chinese_paragraph_practice_log_session_content(file_name):
|
943 |
if file_name:
|
944 |
content = GCS_SERVICE.download_as_string("jutor_logs", file_name)
|
945 |
print(f"content: {content}")
|
946 |
content_json = json.loads(content)
|
947 |
chinese_full_paragraph_input_history = content_json["full_paragraph_input"]
|
|
|
948 |
chinese_full_paragraph_evaluate_output_history = pd.DataFrame(content_json["full_paragraph_evaluate_output"])
|
949 |
chinese_full_paragraph_correct_grammatical_spelling_errors_input_history = content_json["full_paragraph_correct_grammatical_spelling_errors_input"]
|
950 |
chinese_full_paragraph_correct_grammatical_spelling_errors_output_table_history = pd.DataFrame(content_json["full_paragraph_correct_grammatical_spelling_errors_output_table"])
|
@@ -954,6 +960,7 @@ def get_chinese_paragraph_practice_log_session_content(file_name):
|
|
954 |
chinese_full_paragraph_save_output_history = content_json["full_paragraph_save_output"]
|
955 |
else:
|
956 |
chinese_full_paragraph_input_history = ""
|
|
|
957 |
chinese_full_paragraph_evaluate_output_history = pd.DataFrame()
|
958 |
chinese_full_paragraph_correct_grammatical_spelling_errors_input_history = ""
|
959 |
chinese_full_paragraph_correct_grammatical_spelling_errors_output_table_history = pd.DataFrame()
|
@@ -963,6 +970,7 @@ def get_chinese_paragraph_practice_log_session_content(file_name):
|
|
963 |
chinese_full_paragraph_save_output_history = ""
|
964 |
|
965 |
return chinese_full_paragraph_input_history, \
|
|
|
966 |
chinese_full_paragraph_evaluate_output_history, \
|
967 |
chinese_full_paragraph_correct_grammatical_spelling_errors_input_history, \
|
968 |
chinese_full_paragraph_correct_grammatical_spelling_errors_output_table_history, \
|
@@ -971,6 +979,131 @@ def get_chinese_paragraph_practice_log_session_content(file_name):
|
|
971 |
chinese_full_paragraph_refine_output_history, \
|
972 |
chinese_full_paragraph_save_output_history
|
973 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
974 |
# 小工具
|
975 |
def show_elements():
|
976 |
return gr.update(visible=True)
|
@@ -2674,26 +2807,50 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
|
|
2674 |
|
2675 |
6. 批改回饋的最後請引用原文給予至少三個詞語或是句型改寫的範例,列出原文和修改的版本,修改的案例應該和前面四個面向的批改內容前後呼應。
|
2676 |
|
|
|
|
|
2677 |
please use Chinese language (ZH-TW) to evaluate the paragraph and output use JSON format:
|
2678 |
EXAMPLE:
|
2679 |
-
|
2680 |
-
|
2681 |
-
|
2682 |
-
|
2683 |
-
|
2684 |
-
|
2685 |
-
|
2686 |
-
|
2687 |
-
|
2688 |
-
|
2689 |
-
|
2690 |
-
|
2691 |
-
|
2692 |
-
|
2693 |
-
|
2694 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2695 |
}}
|
2696 |
}}
|
|
|
2697 |
|
2698 |
Restrictions:
|
2699 |
- ALL the content should be in Traditional Chinese (zh-TW), it's very important.
|
@@ -2708,10 +2865,10 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
|
|
2708 |
with gr.Row():
|
2709 |
chinese_full_paragraph_evaluate_button = gr.Button("段落全文分析", variant="primary")
|
2710 |
with gr.Row():
|
2711 |
-
|
|
|
|
|
2712 |
|
2713 |
-
|
2714 |
-
|
2715 |
# JUTOR 段落批改與整體建議
|
2716 |
with gr.Row():
|
2717 |
gr.Markdown("# JUTOR 段落批改與整體建議")
|
@@ -2757,6 +2914,7 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
|
|
2757 |
gr.Markdown("<span style='color:#4e80ee'>輸入段落全文</span>")
|
2758 |
chinese_full_paragraph_input_history = gr.Markdown()
|
2759 |
gr.Markdown("<span style='color:#4e80ee'>段落全文分析</span>")
|
|
|
2760 |
chinese_full_paragraph_evaluate_output_history = gr.Dataframe(wrap=True, column_widths=[35, 15, 50], interactive=False)
|
2761 |
|
2762 |
gr.Markdown("<span style='color:#4e80ee'>修訂文法與拼字錯誤 輸入</span>")
|
@@ -2781,6 +2939,7 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
|
|
2781 |
session_timestamp,
|
2782 |
request_origin,
|
2783 |
chinese_full_paragraph_input,
|
|
|
2784 |
chinese_full_paragraph_evaluate_output,
|
2785 |
chinese_full_paragraph_correct_grammatical_spelling_errors_input,
|
2786 |
chinese_full_paragraph_correct_grammatical_spelling_errors_output_table,
|
@@ -2791,6 +2950,7 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
|
|
2791 |
],
|
2792 |
outputs=[
|
2793 |
chinese_full_paragraph_input_history,
|
|
|
2794 |
chinese_full_paragraph_evaluate_output_history,
|
2795 |
chinese_full_paragraph_correct_grammatical_spelling_errors_input_history,
|
2796 |
chinese_full_paragraph_correct_grammatical_spelling_errors_output_table_history,
|
@@ -2805,10 +2965,19 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
|
|
2805 |
outputs=[chinese_grapragh_practice_history_accordion]
|
2806 |
)
|
2807 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2808 |
chinese_full_paragraph_evaluate_button.click(
|
2809 |
-
fn=
|
2810 |
-
inputs=[model, chinese_full_paragraph_sys_content_input,
|
2811 |
-
outputs=chinese_full_paragraph_evaluate_output
|
2812 |
).then(
|
2813 |
fn=update_paragraph_correct_grammatical_spelling_errors_input,
|
2814 |
inputs=[chinese_full_paragraph_input],
|
@@ -2859,6 +3028,7 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
|
|
2859 |
gr.Markdown("<span style='color:#4e80ee'>輸入段落全文</span>")
|
2860 |
chinese_full_paragraph_input_history_log = gr.Markdown()
|
2861 |
gr.Markdown("<span style='color:#4e80ee'>段落全文分析</span>")
|
|
|
2862 |
chinese_full_paragraph_evaluate_output_history_log = gr.Dataframe(wrap=True, column_widths=[35, 15, 50], interactive=False)
|
2863 |
|
2864 |
gr.Markdown("<span style='color:#4e80ee'>修訂文法與拼字錯誤 輸入</span>")
|
@@ -2887,6 +3057,7 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
|
|
2887 |
inputs=[chinese_paragraph_practice_logs_session_list],
|
2888 |
outputs=[
|
2889 |
chinese_full_paragraph_input_history_log,
|
|
|
2890 |
chinese_full_paragraph_evaluate_output_history_log,
|
2891 |
chinese_full_paragraph_correct_grammatical_spelling_errors_input_history_log,
|
2892 |
chinese_full_paragraph_correct_grammatical_spelling_errors_output_table_history_log,
|
|
|
1 |
+
|
|
|
2 |
import gradio as gr
|
3 |
from openai import OpenAI
|
4 |
|
5 |
+
import os
|
6 |
+
from docx import Document
|
7 |
import random
|
8 |
import uuid
|
9 |
import json
|
|
|
13 |
import tempfile
|
14 |
import urllib.parse
|
15 |
import pandas as pd
|
16 |
+
import time
|
17 |
|
18 |
from storage_service import GoogleCloudStorage
|
19 |
|
|
|
|
|
20 |
is_env_local = os.getenv("IS_ENV_LOCAL", "false") == "true"
|
21 |
print(f"is_env_local: {is_env_local}")
|
22 |
|
|
|
27 |
IS_ENV_PROD = "False"
|
28 |
OPEN_AI_KEY = config["OPEN_AI_KEY"]
|
29 |
GCS_KEY = json.dumps(config["GOOGLE_APPLICATION_CREDENTIALS_JSON"])
|
30 |
+
CUTOR_OPEN_AI_KEY = config["CUTOR_OPEN_AI_KEY"]
|
31 |
+
CUTOR_OPEN_AI_ASSISTANT_ID = config["CUTOR_OPEN_AI_ASSISTANT_ID"]
|
32 |
else:
|
33 |
OPEN_AI_KEY = os.getenv("OPEN_AI_KEY")
|
34 |
GCS_KEY = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
|
35 |
+
CUTOR_OPEN_AI_KEY = os.getenv("CUTOR_OPEN_AI_KEY")
|
36 |
+
CUTOR_OPEN_AI_ASSISTANT_ID = os.getenv("CUTOR_ACUTOR_OPEN_AI_ASSISTANT_IDSSISTANT_ID")
|
37 |
|
38 |
OPEN_AI_CLIENT = OpenAI(api_key=OPEN_AI_KEY)
|
39 |
+
CUTOR_OPEN_AI_CLIENT = OpenAI(api_key=CUTOR_OPEN_AI_KEY)
|
40 |
|
41 |
# 设置 Google Cloud Storage 客户端
|
42 |
GCS_SERVICE = GoogleCloudStorage(GCS_KEY)
|
|
|
849 |
past_exam_save_output
|
850 |
|
851 |
# === Chinese ===
|
852 |
+
# def generate_chinese_evaluation_table(model, sys_content, user_prompt, text):
|
853 |
+
# # https://www.ceec.edu.tw/files/file_pool/1/0j052575870800204600/1216%E5%9C%8B%E6%96%87%E4%BD%9C%E6%96%87%E5%88%86%E9%A0%85%E5%BC%8F%E8%A9%95%E5%88%86%E6%8C%87%E6%A8%99.pdf
|
854 |
+
|
855 |
+
# user_content = f"""
|
856 |
+
# 本篇作文:{text}
|
857 |
+
# ---
|
858 |
+
# {user_prompt}
|
859 |
+
# """
|
860 |
+
# messages = [
|
861 |
+
# {"role": "system", "content": sys_content},
|
862 |
+
# {"role": "user", "content": user_content}
|
863 |
+
# ]
|
864 |
+
|
865 |
+
# response_format = { "type": "json_object" }
|
866 |
+
|
867 |
+
# request_payload = {
|
868 |
+
# "model": model,
|
869 |
+
# "messages": messages,
|
870 |
+
# "max_tokens": 2000,
|
871 |
+
# "response_format": response_format
|
872 |
+
# }
|
873 |
+
|
874 |
+
# response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
|
875 |
+
# content = response.choices[0].message.content
|
876 |
+
|
877 |
+
# print(f"====generate_chinese_evaluation_table====")
|
878 |
+
# print(content)
|
879 |
+
|
880 |
+
# data = json.loads(content)["results"]
|
881 |
+
# headers = ["架構", "評分", "解釋"]
|
882 |
+
# table_data = [
|
883 |
+
# ["主題與內容", data['主題與內容']['level'], data['主題與內容']['explanation']],
|
884 |
+
# ["段落結構", data['段落結構']['level'], data['段落結構']['explanation']],
|
885 |
+
# ["遣詞造句", data['遣詞造句']['level'], data['遣詞造句']['explanation']],
|
886 |
+
# ["錯別字與標點符號", data['錯別字與標點符號']['level'], data['錯別字與標點符號']['explanation']]
|
887 |
+
# ]
|
888 |
|
889 |
+
# gr_update = gr.update(value=table_data, headers=headers)
|
890 |
|
891 |
+
# return gr_update
|
892 |
|
893 |
def load_exam_data():
|
894 |
with open("exams.json", "r") as file:
|
|
|
906 |
session_timestamp,
|
907 |
request_origin,
|
908 |
chinese_full_paragraph_input,
|
909 |
+
chinese_full_paragraph_evaluate_output_text,
|
910 |
chinese_full_paragraph_evaluate_output,
|
911 |
chinese_full_paragraph_correct_grammatical_spelling_errors_input,
|
912 |
chinese_full_paragraph_correct_grammatical_spelling_errors_output_table,
|
|
|
923 |
"session_timestamp": session_timestamp,
|
924 |
"request_origin": request_origin,
|
925 |
"full_paragraph_input": chinese_full_paragraph_input,
|
926 |
+
"chinese_full_paragraph_evaluate_output_text": chinese_full_paragraph_evaluate_output_text,
|
927 |
"full_paragraph_evaluate_output": chinese_full_paragraph_evaluate_output.to_dict(orient='records'),
|
928 |
"full_paragraph_correct_grammatical_spelling_errors_input": chinese_full_paragraph_correct_grammatical_spelling_errors_input,
|
929 |
"full_paragraph_correct_grammatical_spelling_errors_output_table": chinese_full_paragraph_correct_grammatical_spelling_errors_output_table.to_dict(orient='records'),
|
|
|
935 |
GCS_SERVICE.upload_json_string("jutor_logs", file_name, json.dumps(content))
|
936 |
|
937 |
return chinese_full_paragraph_input, \
|
938 |
+
chinese_full_paragraph_evaluate_output_text, \
|
939 |
chinese_full_paragraph_evaluate_output, \
|
940 |
chinese_full_paragraph_correct_grammatical_spelling_errors_input, \
|
941 |
chinese_full_paragraph_correct_grammatical_spelling_errors_output_table, \
|
|
|
944 |
chinese_full_paragraph_refine_output, \
|
945 |
chinese_full_paragraph_save_output
|
946 |
|
|
|
|
|
947 |
def get_chinese_paragraph_practice_log_session_content(file_name):
|
948 |
if file_name:
|
949 |
content = GCS_SERVICE.download_as_string("jutor_logs", file_name)
|
950 |
print(f"content: {content}")
|
951 |
content_json = json.loads(content)
|
952 |
chinese_full_paragraph_input_history = content_json["full_paragraph_input"]
|
953 |
+
chinese_full_paragraph_evaluate_output_text_history = content_json["chinese_full_paragraph_evaluate_output_text"] if "chinese_full_paragraph_evaluate_output_text" in content_json else ""
|
954 |
chinese_full_paragraph_evaluate_output_history = pd.DataFrame(content_json["full_paragraph_evaluate_output"])
|
955 |
chinese_full_paragraph_correct_grammatical_spelling_errors_input_history = content_json["full_paragraph_correct_grammatical_spelling_errors_input"]
|
956 |
chinese_full_paragraph_correct_grammatical_spelling_errors_output_table_history = pd.DataFrame(content_json["full_paragraph_correct_grammatical_spelling_errors_output_table"])
|
|
|
960 |
chinese_full_paragraph_save_output_history = content_json["full_paragraph_save_output"]
|
961 |
else:
|
962 |
chinese_full_paragraph_input_history = ""
|
963 |
+
chinese_full_paragraph_evaluate_output_text_history = ""
|
964 |
chinese_full_paragraph_evaluate_output_history = pd.DataFrame()
|
965 |
chinese_full_paragraph_correct_grammatical_spelling_errors_input_history = ""
|
966 |
chinese_full_paragraph_correct_grammatical_spelling_errors_output_table_history = pd.DataFrame()
|
|
|
970 |
chinese_full_paragraph_save_output_history = ""
|
971 |
|
972 |
return chinese_full_paragraph_input_history, \
|
973 |
+
chinese_full_paragraph_evaluate_output_text_history, \
|
974 |
chinese_full_paragraph_evaluate_output_history, \
|
975 |
chinese_full_paragraph_correct_grammatical_spelling_errors_input_history, \
|
976 |
chinese_full_paragraph_correct_grammatical_spelling_errors_output_table_history, \
|
|
|
979 |
chinese_full_paragraph_refine_output_history, \
|
980 |
chinese_full_paragraph_save_output_history
|
981 |
|
982 |
+
|
983 |
+
# === OpenAI Assistant ===
|
984 |
+
def get_chinese_paragraph_evaluate_content(model, sys_content, paragraph, user_generate_paragraph_evaluate_prompt):
|
985 |
+
user_content = f"""
|
986 |
+
sys_content: {sys_content}
|
987 |
+
---
|
988 |
+
paragraph is: {paragraph}
|
989 |
+
---
|
990 |
+
{user_generate_paragraph_evaluate_prompt}
|
991 |
+
"""
|
992 |
+
content = generate_content_by_open_ai_assistant(user_content, thread_id=None, model_name=model)
|
993 |
+
print(f"====generate_paragraph_evaluate====")
|
994 |
+
print(content)
|
995 |
+
|
996 |
+
content_list = content.split("```json")
|
997 |
+
content_text = content_list[0]
|
998 |
+
content_json = content_list[1].split("```")[0].replace("```", "")
|
999 |
+
|
1000 |
+
data = json.loads(content_json)["results"]
|
1001 |
+
headers = ["架構", "評分", "解釋"]
|
1002 |
+
table_data = [
|
1003 |
+
["主題與內容", data['主題與內容']['level'], data['主題與內容']['explanation']],
|
1004 |
+
["段落結構", data['段落結構']['level'], data['段落結構']['explanation']],
|
1005 |
+
["遣詞造句", data['遣詞造句']['level'], data['遣詞造句']['explanation']],
|
1006 |
+
["錯別字與標點符號", data['錯別字與標點符號']['level'], data['錯別字與標點符號']['explanation']]
|
1007 |
+
]
|
1008 |
+
|
1009 |
+
gr_update = gr.update(value=table_data, headers=headers, visible=True)
|
1010 |
+
|
1011 |
+
return content_text, gr_update
|
1012 |
+
|
1013 |
+
|
1014 |
+
def generate_content_by_open_ai_assistant(user_content, thread_id=None, model_name=None):
|
1015 |
+
client = CUTOR_OPEN_AI_CLIENT
|
1016 |
+
assistant_id = CUTOR_OPEN_AI_ASSISTANT_ID
|
1017 |
+
|
1018 |
+
try:
|
1019 |
+
if not thread_id:
|
1020 |
+
thread = client.beta.threads.create()
|
1021 |
+
thread_id = thread.id
|
1022 |
+
else:
|
1023 |
+
thread = client.beta.threads.retrieve(thread_id)
|
1024 |
+
|
1025 |
+
print(f"Thread ID: {thread.id}")
|
1026 |
+
|
1027 |
+
# if metadata:
|
1028 |
+
# client.beta.threads.update(thread_id=thread.id, metadata=metadata)
|
1029 |
+
|
1030 |
+
# Send the user message to the thread
|
1031 |
+
print("==============Send the user message to the thread====================")
|
1032 |
+
client.beta.threads.messages.create(thread_id=thread.id, role="user", content=user_content)
|
1033 |
+
|
1034 |
+
# Run the assistant
|
1035 |
+
print("==============Run the assistant====================")
|
1036 |
+
run = client.beta.threads.runs.create_and_poll(thread_id=thread.id, assistant_id=assistant_id)
|
1037 |
+
|
1038 |
+
# Wait for the response
|
1039 |
+
# run_status = poll_run_status(run.id, thread.id, timeout=30)
|
1040 |
+
# print(f"Run status: {run_status}")
|
1041 |
+
|
1042 |
+
if run.status == "completed":
|
1043 |
+
print("==============completed====================")
|
1044 |
+
print(f"Thread ID: {thread.id}")
|
1045 |
+
messages = client.beta.threads.messages.list(thread_id=thread.id)
|
1046 |
+
print(f"Messages: {messages}")
|
1047 |
+
response = messages
|
1048 |
+
response_text = messages.data[0].content[0].text.value
|
1049 |
+
print(f"Response: {response_text}")
|
1050 |
+
except Exception as e:
|
1051 |
+
print(f"Error: {e}")
|
1052 |
+
response_text = " 出現錯誤,請稍後再試!"
|
1053 |
+
|
1054 |
+
return response_text
|
1055 |
+
|
1056 |
+
def poll_run_status(run_id, thread_id, timeout=600, poll_interval=5):
|
1057 |
+
"""
|
1058 |
+
Polls the status of a Run and handles different statuses appropriately.
|
1059 |
+
|
1060 |
+
:param run_id: The ID of the Run to poll.
|
1061 |
+
:param thread_id: The ID of the Thread associated with the Run.
|
1062 |
+
:param timeout: Maximum time to wait for the Run to complete, in seconds.
|
1063 |
+
:param poll_interval: Time to wait between each poll, in seconds.
|
1064 |
+
"""
|
1065 |
+
client = OPEN_AI_CLIENT
|
1066 |
+
start_time = time.time()
|
1067 |
+
while time.time() - start_time < timeout:
|
1068 |
+
run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run_id)
|
1069 |
+
|
1070 |
+
if run.status in ["completed", "cancelled", "failed"]:
|
1071 |
+
print(f"Run completed with status: {run.status}")
|
1072 |
+
break
|
1073 |
+
elif run.status == "requires_action":
|
1074 |
+
print("Run requires action. Performing required action...")
|
1075 |
+
# Here, you would perform the required action, e.g., running functions
|
1076 |
+
# and then submitting the outputs. This is simplified for this example.
|
1077 |
+
# After performing the required action, you'd complete the action:
|
1078 |
+
# OPEN_AI_CLIENT.beta.threads.runs.complete_required_action(...)
|
1079 |
+
elif run.status == "expired":
|
1080 |
+
print("Run expired. Exiting...")
|
1081 |
+
break
|
1082 |
+
else:
|
1083 |
+
print(f"Run status is {run.status}. Waiting for updates...")
|
1084 |
+
|
1085 |
+
time.sleep(poll_interval)
|
1086 |
+
else:
|
1087 |
+
print("Timeout reached. Run did not complete in the expected time.")
|
1088 |
+
|
1089 |
+
# Once the Run is completed, handle the result accordingly
|
1090 |
+
if run.status == "completed":
|
1091 |
+
# Retrieve and handle messages or run steps as needed
|
1092 |
+
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
1093 |
+
for message in messages.data:
|
1094 |
+
if message.role == "assistant":
|
1095 |
+
print(f"Assistant response: {message.content}")
|
1096 |
+
elif run.status in ["cancelled", "failed"]:
|
1097 |
+
# Handle cancellation or failure
|
1098 |
+
print(f"Run ended with status: {run.status}")
|
1099 |
+
elif run.status == "expired":
|
1100 |
+
# Handle expired run
|
1101 |
+
print("Run expired without completion.")
|
1102 |
+
|
1103 |
+
return run.status
|
1104 |
+
|
1105 |
+
|
1106 |
+
|
1107 |
# 小工具
|
1108 |
def show_elements():
|
1109 |
return gr.update(visible=True)
|
|
|
2807 |
|
2808 |
6. 批改回饋的最後請引用原文給予至少三個詞語或是句型改寫的範例,列出原文和修改的版本,修改的案例應該和前面四個面向的批改內容前後呼應。
|
2809 |
|
2810 |
+
7. 將評分標準與回饋的內容以JSON格式輸出,並且請使用繁體中文(ZH-TW)來評分段落並輸出,用 ```json ..... ``` 包裹:
|
2811 |
+
|
2812 |
please use Chinese language (ZH-TW) to evaluate the paragraph and output use JSON format:
|
2813 |
EXAMPLE:
|
2814 |
+
很可惜,你的作文有點短,你可以參考下面的建議,讓自己的字數不少於200字。
|
2815 |
+
|
2816 |
+
# 評分標準與回饋
|
2817 |
+
主題與內容:B+ 你的主題很明確,講述了CSS在渲染空間上的問題及解決方案,這是一個重要而實用的話題。然而,內容相對較少,缺乏足夠的細節與實例來支撐你的觀點。建議你可以添加一些具體情境或例子,讓讀者更容易理解CSS的應用情況,例如提到常見的渲染問題以及具體的解決方法。
|
2818 |
+
|
2819 |
+
段落結構:B 你的段落結構基本清晰,但目前只有一段,這使得整體文章顯得有些單薄。建議你可以將內容分成幾個小段落,每個段落著重於不同的要點,例如一段說明問題,另一段探討解決方案,這樣整體更具條理性。
|
2820 |
+
|
2821 |
+
遣詞造句:A 你的遣詞造句大致良好,用詞得體且通順。不過可以嘗試加一些更具體的技術詞彙或示例,使文章更專業化。
|
2822 |
+
|
2823 |
+
錯別字與標點符號:A+ 你的文章沒有明顯的錯別字,並且標點符號的使用也相當正確,這點做得很好。
|
2824 |
+
|
2825 |
+
# 修改範例
|
2826 |
+
原文:內容雖然簡短,但主題明確。 修改:雖然內容相對簡短,但主題表達得非常明確。
|
2827 |
+
|
2828 |
+
原文:缺乏實例和具體情境來支持內容。 修改:目前缺少具體的實例及情境來支持文章的內容與主張。
|
2829 |
+
|
2830 |
+
原文:可以進一步擴展。 修改:可以進一步擴展來豐富內容,讓讀者更有共鳴。
|
2831 |
+
|
2832 |
+
```json
|
2833 |
+
{{
|
2834 |
+
"results": {{
|
2835 |
+
"主題與內容": {{
|
2836 |
+
"level": "A+",
|
2837 |
+
"explanation": "#中文解釋 ZH-TW"
|
2838 |
+
}},
|
2839 |
+
"段落結構": {{
|
2840 |
+
"level": "B+",
|
2841 |
+
"explanation": "#中文解釋 ZH-TW"
|
2842 |
+
}},
|
2843 |
+
"遣詞造句": {{
|
2844 |
+
"level": "C",
|
2845 |
+
"explanation": "#中文解釋 ZH-TW"
|
2846 |
+
}},
|
2847 |
+
"錯別字與標點符號": {{
|
2848 |
+
"level": "C-",
|
2849 |
+
"explanation": "#中文解釋 ZH-TW"
|
2850 |
+
}}
|
2851 |
}}
|
2852 |
}}
|
2853 |
+
```
|
2854 |
|
2855 |
Restrictions:
|
2856 |
- ALL the content should be in Traditional Chinese (zh-TW), it's very important.
|
|
|
2865 |
with gr.Row():
|
2866 |
chinese_full_paragraph_evaluate_button = gr.Button("段落全文分析", variant="primary")
|
2867 |
with gr.Row():
|
2868 |
+
chinese_full_paragraph_evaluate_output_text = gr.Markdown(label="段落全文分析")
|
2869 |
+
with gr.Row():
|
2870 |
+
chinese_full_paragraph_evaluate_output = gr.Dataframe(label="段落全文分析表格", wrap=True, column_widths=[20, 15, 65], interactive=False)
|
2871 |
|
|
|
|
|
2872 |
# JUTOR 段落批改與整體建議
|
2873 |
with gr.Row():
|
2874 |
gr.Markdown("# JUTOR 段落批改與整體建議")
|
|
|
2914 |
gr.Markdown("<span style='color:#4e80ee'>輸入段落全文</span>")
|
2915 |
chinese_full_paragraph_input_history = gr.Markdown()
|
2916 |
gr.Markdown("<span style='color:#4e80ee'>段落全文分析</span>")
|
2917 |
+
chinese_full_paragraph_evaluate_output_text_history = gr.Markdown()
|
2918 |
chinese_full_paragraph_evaluate_output_history = gr.Dataframe(wrap=True, column_widths=[35, 15, 50], interactive=False)
|
2919 |
|
2920 |
gr.Markdown("<span style='color:#4e80ee'>修訂文法與拼字錯誤 輸入</span>")
|
|
|
2939 |
session_timestamp,
|
2940 |
request_origin,
|
2941 |
chinese_full_paragraph_input,
|
2942 |
+
chinese_full_paragraph_evaluate_output_text,
|
2943 |
chinese_full_paragraph_evaluate_output,
|
2944 |
chinese_full_paragraph_correct_grammatical_spelling_errors_input,
|
2945 |
chinese_full_paragraph_correct_grammatical_spelling_errors_output_table,
|
|
|
2950 |
],
|
2951 |
outputs=[
|
2952 |
chinese_full_paragraph_input_history,
|
2953 |
+
chinese_full_paragraph_evaluate_output_text_history,
|
2954 |
chinese_full_paragraph_evaluate_output_history,
|
2955 |
chinese_full_paragraph_correct_grammatical_spelling_errors_input_history,
|
2956 |
chinese_full_paragraph_correct_grammatical_spelling_errors_output_table_history,
|
|
|
2965 |
outputs=[chinese_grapragh_practice_history_accordion]
|
2966 |
)
|
2967 |
|
2968 |
+
# chinese_full_paragraph_evaluate_button.click(
|
2969 |
+
# fn=generate_chinese_evaluation_table,
|
2970 |
+
# inputs=[model, chinese_full_paragraph_sys_content_input, user_generate_chinese_full_paragraph_evaluate_prompt, chinese_full_paragraph_input],
|
2971 |
+
# outputs=chinese_full_paragraph_evaluate_output
|
2972 |
+
# ).then(
|
2973 |
+
# fn=update_paragraph_correct_grammatical_spelling_errors_input,
|
2974 |
+
# inputs=[chinese_full_paragraph_input],
|
2975 |
+
# outputs=chinese_full_paragraph_correct_grammatical_spelling_errors_input
|
2976 |
+
# )
|
2977 |
chinese_full_paragraph_evaluate_button.click(
|
2978 |
+
fn=get_chinese_paragraph_evaluate_content,
|
2979 |
+
inputs=[model, chinese_full_paragraph_sys_content_input, chinese_full_paragraph_input, user_generate_chinese_full_paragraph_evaluate_prompt],
|
2980 |
+
outputs=[chinese_full_paragraph_evaluate_output_text, chinese_full_paragraph_evaluate_output]
|
2981 |
).then(
|
2982 |
fn=update_paragraph_correct_grammatical_spelling_errors_input,
|
2983 |
inputs=[chinese_full_paragraph_input],
|
|
|
3028 |
gr.Markdown("<span style='color:#4e80ee'>輸入段落全文</span>")
|
3029 |
chinese_full_paragraph_input_history_log = gr.Markdown()
|
3030 |
gr.Markdown("<span style='color:#4e80ee'>段落全文分析</span>")
|
3031 |
+
chinese_full_paragraph_evaluate_output_text_history_log = gr.Markdown()
|
3032 |
chinese_full_paragraph_evaluate_output_history_log = gr.Dataframe(wrap=True, column_widths=[35, 15, 50], interactive=False)
|
3033 |
|
3034 |
gr.Markdown("<span style='color:#4e80ee'>修訂文法與拼字錯誤 輸入</span>")
|
|
|
3057 |
inputs=[chinese_paragraph_practice_logs_session_list],
|
3058 |
outputs=[
|
3059 |
chinese_full_paragraph_input_history_log,
|
3060 |
+
chinese_full_paragraph_evaluate_output_text_history_log,
|
3061 |
chinese_full_paragraph_evaluate_output_history_log,
|
3062 |
chinese_full_paragraph_correct_grammatical_spelling_errors_input_history_log,
|
3063 |
chinese_full_paragraph_correct_grammatical_spelling_errors_output_table_history_log,
|