Spaces:
Sleeping
Sleeping
with gr.Tab("中文作文工具") as chinese_idea_tab:
Browse files
app.py
CHANGED
@@ -13,7 +13,7 @@ import json
|
|
13 |
import tempfile
|
14 |
import urllib.parse
|
15 |
import pandas as pd
|
16 |
-
import
|
17 |
|
18 |
from storage_service import GoogleCloudStorage
|
19 |
|
@@ -1087,6 +1087,42 @@ def verify_moderation(text):
|
|
1087 |
|
1088 |
return is_flagged, response_dict
|
1089 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1090 |
def get_chinese_paragraph_evaluate_content(model, sys_content, paragraph, user_generate_paragraph_evaluate_prompt):
|
1091 |
verify_string_length(paragraph)
|
1092 |
verify_moderation(paragraph)
|
@@ -1114,12 +1150,21 @@ def get_chinese_paragraph_evaluate_content(model, sys_content, paragraph, user_g
|
|
1114 |
["主題與內容", data['主題與內容']['level'], data['主題與內容']['explanation']],
|
1115 |
["段落結構", data['段落結構']['level'], data['段落結構']['explanation']],
|
1116 |
["遣詞造句", data['遣詞造句']['level'], data['遣詞造句']['explanation']],
|
1117 |
-
["
|
1118 |
]
|
1119 |
-
|
1120 |
content_table = gr.update(value=table_data, headers=headers, visible=True)
|
1121 |
-
|
1122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1123 |
|
1124 |
def get_chinese_paragraph_refine_evaluate_content(model, sys_content, paragraph_1, paragraph_2, user_refine_paragraph_prompt):
|
1125 |
verify_string_length(paragraph_1)
|
@@ -1151,7 +1196,7 @@ def get_chinese_paragraph_refine_evaluate_content(model, sys_content, paragraph_
|
|
1151 |
["主題與內容", data['主題與內容']['level'], data['主題與內容']['explanation']],
|
1152 |
["段落結構", data['段落結構']['level'], data['段落結構']['explanation']],
|
1153 |
["遣詞造句", data['遣詞造句']['level'], data['遣詞造句']['explanation']],
|
1154 |
-
["
|
1155 |
]
|
1156 |
|
1157 |
gr_update = gr.update(value=table_data, headers=headers, visible=True)
|
@@ -2907,32 +2952,6 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
|
|
2907 |
with gr.Row():
|
2908 |
chinese_assignment_description = gr.Textbox(label="作業說明", interactive=False)
|
2909 |
|
2910 |
-
|
2911 |
-
# =====中文作文工具=====
|
2912 |
-
with gr.Tab("中文作文工具") as chinese_idea_tab:
|
2913 |
-
# 輸入題目、輸出靈感
|
2914 |
-
with gr.Row():
|
2915 |
-
chinese_write_idea_prompt = """
|
2916 |
-
你是一位國文老師,善於引導學生寫作。請根據以下的題目,幫助學生生成靈感:
|
2917 |
-
"""
|
2918 |
-
chinese_write_idea_prompt_input = gr.TextArea(label="System Prompt", value=chinese_write_idea_prompt, visible=False)
|
2919 |
-
with gr.Column():
|
2920 |
-
with gr.Row():
|
2921 |
-
gr.Markdown("# 中文作文工具")
|
2922 |
-
with gr.Row():
|
2923 |
-
chinese_essay_title_input = gr.TextArea(label="輸入題目")
|
2924 |
-
with gr.Column():
|
2925 |
-
with gr.Row():
|
2926 |
-
chinese_essay_generate_button = gr.Button("生成靈感", variant="primary")
|
2927 |
-
with gr.Row():
|
2928 |
-
chinese_essay_idea_output = gr.Markdown(label="生成靈感")
|
2929 |
-
|
2930 |
-
chinese_essay_generate_button.click(
|
2931 |
-
fn=generate_chinese_essay_idea,
|
2932 |
-
inputs=[model, chinese_write_idea_prompt_input, chinese_essay_title_input],
|
2933 |
-
outputs=chinese_essay_idea_output
|
2934 |
-
)
|
2935 |
-
|
2936 |
# =====中文全文批改=====
|
2937 |
with gr.Tab("中文全文批改") as chinese_full_paragraph_tab:
|
2938 |
with gr.Row(visible=False) as chinese_full_paragraph_params:
|
@@ -2940,20 +2959,24 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
|
|
2940 |
default_user_generate_chinese_full_paragraph_evaluate_prompt = """
|
2941 |
# 請嚴格根據 instructions
|
2942 |
|
2943 |
-
#
|
2944 |
-
|
2945 |
-
|
2946 |
-
|
2947 |
-
|
2948 |
-
|
2949 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2950 |
2. 再將評分標準與回饋的內容以JSON格式輸出,並且請使用繁體中文(ZH-TW)來評分段落並輸出,用 ```json ..... ``` 包裹:
|
2951 |
3. please use Chinese language (ZH-TW) to evaluate the paragraph and output use JSON format:
|
2952 |
-
|
2953 |
-
EXAMPLE:
|
2954 |
-
# 整體評分:(🔴?🟡?🟢?)
|
2955 |
-
if 評分與回饋 都是 X,則整體評分給予 🔴
|
2956 |
|
|
|
2957 |
# 綜合回饋
|
2958 |
你的文章...............(寫出一段話,來總結這篇作文的好壞)
|
2959 |
|
@@ -2964,7 +2987,7 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
|
|
2964 |
|
2965 |
遣詞造句:A 你的遣詞造句大致良好,用詞得體且通順。不過可以嘗試加一些更具體的技術詞彙或示例,使文章更專業化。
|
2966 |
|
2967 |
-
|
2968 |
|
2969 |
# 修改範例
|
2970 |
- 原文:內容雖然簡短,但主題明確。
|
@@ -2989,7 +3012,7 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
|
|
2989 |
"level": "C",
|
2990 |
"explanation": "#中文解釋 ZH-TW"
|
2991 |
}},
|
2992 |
-
"
|
2993 |
"level": "C-",
|
2994 |
"explanation": "#中文解釋 ZH-TW"
|
2995 |
}}
|
@@ -3073,7 +3096,7 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
|
|
3073 |
|
3074 |
遣詞造句:A 你的遣詞造句大致良好,用詞得體且通順。不過可以嘗試加一些更具體的技術詞彙或示例,使文章更專業化。
|
3075 |
|
3076 |
-
|
3077 |
|
3078 |
# 修改範例
|
3079 |
- 原文:內容雖然簡短,但主��明確。
|
@@ -3098,7 +3121,7 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
|
|
3098 |
"level": "C",
|
3099 |
"explanation": "#中文解釋 ZH-TW"
|
3100 |
}},
|
3101 |
-
"
|
3102 |
"level": "C-",
|
3103 |
"explanation": "#中文解釋 ZH-TW"
|
3104 |
}}
|
@@ -3269,7 +3292,32 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
|
|
3269 |
chinese_assignment_description_history_log
|
3270 |
]
|
3271 |
)
|
3272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3273 |
demo.load(
|
3274 |
init_params,
|
3275 |
inputs =[],
|
|
|
13 |
import tempfile
|
14 |
import urllib.parse
|
15 |
import pandas as pd
|
16 |
+
import re
|
17 |
|
18 |
from storage_service import GoogleCloudStorage
|
19 |
|
|
|
1087 |
|
1088 |
return is_flagged, response_dict
|
1089 |
|
1090 |
+
def assign_grade(subject_content, structure, diction, spelling_punctuation):
|
1091 |
+
# 定義等級順序
|
1092 |
+
grade_order = ["A+", "A", "A-", "B+", "B", "B-"]
|
1093 |
+
|
1094 |
+
# 確認等級順序
|
1095 |
+
def grade_is_higher_or_equal(grade1, grade2):
|
1096 |
+
return grade_order.index(grade1) <= grade_order.index(grade2)
|
1097 |
+
|
1098 |
+
# 如果任何一個選項為 "X",則直接返回紅燈
|
1099 |
+
if any(grade == "X" for grade in [subject_content, structure, diction, spelling_punctuation]):
|
1100 |
+
return "🔴"
|
1101 |
+
|
1102 |
+
# 🟢 條件
|
1103 |
+
if (subject_content in ["A+", "A", "A-"] and
|
1104 |
+
grade_is_higher_or_equal(structure, "B+") and
|
1105 |
+
grade_is_higher_or_equal(diction, "B+") and
|
1106 |
+
grade_is_higher_or_equal(spelling_punctuation, "B+")):
|
1107 |
+
return "🟢"
|
1108 |
+
|
1109 |
+
# 🟡 條件
|
1110 |
+
elif (subject_content in ["B+", "B"] and
|
1111 |
+
grade_is_higher_or_equal(structure, "B") and
|
1112 |
+
grade_is_higher_or_equal(diction, "B") and
|
1113 |
+
grade_is_higher_or_equal(spelling_punctuation, "B")):
|
1114 |
+
return "🟡"
|
1115 |
+
|
1116 |
+
# 🔴 條件
|
1117 |
+
elif (subject_content == "B-" and
|
1118 |
+
structure == "B-" and
|
1119 |
+
diction == "B-" and
|
1120 |
+
spelling_punctuation == "B-"):
|
1121 |
+
return "🔴"
|
1122 |
+
|
1123 |
+
# 預設為 🟡
|
1124 |
+
return "🟡"
|
1125 |
+
|
1126 |
def get_chinese_paragraph_evaluate_content(model, sys_content, paragraph, user_generate_paragraph_evaluate_prompt):
|
1127 |
verify_string_length(paragraph)
|
1128 |
verify_moderation(paragraph)
|
|
|
1150 |
["主題與內容", data['主題與內容']['level'], data['主題與內容']['explanation']],
|
1151 |
["段落結構", data['段落結構']['level'], data['段落結構']['explanation']],
|
1152 |
["遣詞造句", data['遣詞造句']['level'], data['遣詞造句']['explanation']],
|
1153 |
+
["錯別字", data['錯別字']['level'], data['錯別字']['explanation']]
|
1154 |
]
|
|
|
1155 |
content_table = gr.update(value=table_data, headers=headers, visible=True)
|
1156 |
+
|
1157 |
+
# 綜合評分
|
1158 |
+
grade = assign_grade(data['主題與內容']['level'], data['段落結構']['level'], data['遣詞造句']['level'], data['錯別字']['level'])
|
1159 |
+
grade_content_text = f"# 綜合評分:{grade}"
|
1160 |
+
total_content_text = grade_content_text + "\n" + content_text
|
1161 |
+
|
1162 |
+
# 綜合回饋
|
1163 |
+
feedback_match = re.search(r"綜合回饋(.*?)評分標準與回饋", content_text, re.DOTALL)
|
1164 |
+
feedback_text = feedback_match.group(1).strip() if feedback_match else ""
|
1165 |
+
table_data.append(["綜合評分", grade, feedback_text])
|
1166 |
+
|
1167 |
+
return total_content_text, content_table
|
1168 |
|
1169 |
def get_chinese_paragraph_refine_evaluate_content(model, sys_content, paragraph_1, paragraph_2, user_refine_paragraph_prompt):
|
1170 |
verify_string_length(paragraph_1)
|
|
|
1196 |
["主題與內容", data['主題與內容']['level'], data['主題與內容']['explanation']],
|
1197 |
["段落結構", data['段落結構']['level'], data['段落結構']['explanation']],
|
1198 |
["遣詞造句", data['遣詞造句']['level'], data['遣詞造句']['explanation']],
|
1199 |
+
["錯別字", data['錯別字']['level'], data['錯別字']['explanation']]
|
1200 |
]
|
1201 |
|
1202 |
gr_update = gr.update(value=table_data, headers=headers, visible=True)
|
|
|
2952 |
with gr.Row():
|
2953 |
chinese_assignment_description = gr.Textbox(label="作業說明", interactive=False)
|
2954 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2955 |
# =====中文全文批改=====
|
2956 |
with gr.Tab("中文全文批改") as chinese_full_paragraph_tab:
|
2957 |
with gr.Row(visible=False) as chinese_full_paragraph_params:
|
|
|
2959 |
default_user_generate_chinese_full_paragraph_evaluate_prompt = """
|
2960 |
# 請嚴格根據 instructions
|
2961 |
|
2962 |
+
# Rules:
|
2963 |
+
1. 先檢查是否是合理的作文或是段落,再進行評分
|
2964 |
+
2. 請確保作文或段落的內容完整,並且符合中文語法
|
2965 |
+
3. 如果是一篇亂打的文章請直接給予回饋:「這篇文章內容不完整,無法進行評分。」
|
2966 |
+
4. 如果無法進行評分 評分標準與回饋的內容跟等級,則為 X
|
2967 |
+
5. 評分標準與回饋根據「A+、A、A- 、B+、 B、 B-」等級來評分,最低為 B-
|
2968 |
+
|
2969 |
+
# Restrictions:
|
2970 |
+
1. 不用給整體評分
|
2971 |
+
2. 不用改標點符號
|
2972 |
+
3. 評分標準的分數等級請使用「A+、A、A- 、B+、 B、 B-」等級,不可使用數字或是其他等級,像是「90分、80分、C、D」等等。
|
2973 |
+
|
2974 |
+
# Output format:
|
2975 |
+
1. 先給 綜合回饋、評分標準與回饋、修改範例
|
2976 |
2. 再將評分標準與回饋的內容以JSON格式輸出,並且請使用繁體中文(ZH-TW)來評分段落並輸出,用 ```json ..... ``` 包裹:
|
2977 |
3. please use Chinese language (ZH-TW) to evaluate the paragraph and output use JSON format:
|
|
|
|
|
|
|
|
|
2978 |
|
2979 |
+
EXAMPLE:
|
2980 |
# 綜合回饋
|
2981 |
你的文章...............(寫出一段話,來總結這篇作文的好壞)
|
2982 |
|
|
|
2987 |
|
2988 |
遣詞造句:A 你的遣詞造句大致良好,用詞得體且通順。不過可以嘗試加一些更具體的技術詞彙或示例,使文章更專業化。
|
2989 |
|
2990 |
+
錯別字:A+ 你的文章沒有明顯的錯別字,這點做得很好 (或是你有以下的錯別字: ...... )。
|
2991 |
|
2992 |
# 修改範例
|
2993 |
- 原文:內容雖然簡短,但主題明確。
|
|
|
3012 |
"level": "C",
|
3013 |
"explanation": "#中文解釋 ZH-TW"
|
3014 |
}},
|
3015 |
+
"錯別字": {{
|
3016 |
"level": "C-",
|
3017 |
"explanation": "#中文解釋 ZH-TW"
|
3018 |
}}
|
|
|
3096 |
|
3097 |
遣詞造句:A 你的遣詞造句大致良好,用詞得體且通順。不過可以嘗試加一些更具體的技術詞彙或示例,使文章更專業化。
|
3098 |
|
3099 |
+
錯別字:A+ 你的文章沒有明顯的錯別字,這點做得很好。(或是你有以下的錯別字: ......)。
|
3100 |
|
3101 |
# 修改範例
|
3102 |
- 原文:內容雖然簡短,但主��明確。
|
|
|
3121 |
"level": "C",
|
3122 |
"explanation": "#中文解釋 ZH-TW"
|
3123 |
}},
|
3124 |
+
"錯別字": {{
|
3125 |
"level": "C-",
|
3126 |
"explanation": "#中文解釋 ZH-TW"
|
3127 |
}}
|
|
|
3292 |
chinese_assignment_description_history_log
|
3293 |
]
|
3294 |
)
|
3295 |
+
|
3296 |
+
# =====中文作文工具=====
|
3297 |
+
with gr.Tab("中文作文工具") as chinese_idea_tab:
|
3298 |
+
# 輸入題目、輸出靈感
|
3299 |
+
with gr.Row():
|
3300 |
+
chinese_write_idea_prompt = """
|
3301 |
+
你是一位國文老師,善於引導學生寫作。請根據以下的題目,幫助學生生成靈感:
|
3302 |
+
"""
|
3303 |
+
chinese_write_idea_prompt_input = gr.TextArea(label="System Prompt", value=chinese_write_idea_prompt, visible=False)
|
3304 |
+
with gr.Column():
|
3305 |
+
with gr.Row():
|
3306 |
+
gr.Markdown("# 中文作文工具")
|
3307 |
+
with gr.Row():
|
3308 |
+
chinese_essay_title_input = gr.TextArea(label="輸入題目")
|
3309 |
+
with gr.Column():
|
3310 |
+
with gr.Row():
|
3311 |
+
chinese_essay_generate_button = gr.Button("生成靈感", variant="primary")
|
3312 |
+
with gr.Row():
|
3313 |
+
chinese_essay_idea_output = gr.Markdown(label="生成靈感")
|
3314 |
+
|
3315 |
+
chinese_essay_generate_button.click(
|
3316 |
+
fn=generate_chinese_essay_idea,
|
3317 |
+
inputs=[model, chinese_write_idea_prompt_input, chinese_essay_title_input],
|
3318 |
+
outputs=chinese_essay_idea_output
|
3319 |
+
)
|
3320 |
+
|
3321 |
demo.load(
|
3322 |
init_params,
|
3323 |
inputs =[],
|