youngtsai commited on
Commit
7018f10
·
1 Parent(s): dc0bf4b

check_chinese_essay_feedback

Browse files
Files changed (1) hide show
  1. app.py +98 -0
app.py CHANGED
@@ -1102,6 +1102,10 @@ def verify_string_length(text):
1102
  if len(text) > 2000:
1103
  raise gr.Error("輸入的文字長度過長,請重新輸入!")
1104
 
 
 
 
 
1105
  def verify_moderation(text):
1106
  response = OPEN_AI_MODERATION_CLIENT.moderations.create(input=text)
1107
  response_dict = response.model_dump()
@@ -1315,6 +1319,73 @@ def generate_chinese_essay_idea(model, user_prompt, chinese_essay_title_input):
1315
 
1316
  return content
1317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1318
  # Download doc
1319
  def create_word(content):
1320
  unique_filename = str(uuid.uuid4())
@@ -3351,6 +3422,33 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
3351
  outputs=chinese_essay_idea_output
3352
  )
3353
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3354
  with gr.Row(visible=False) as assignment_group:
3355
  with gr.Column():
3356
  with gr.Row():
 
1102
  if len(text) > 2000:
1103
  raise gr.Error("輸入的文字長度過長,請重新輸入!")
1104
 
1105
+ def verify_string_length_short(text):
1106
+ if len(text) < 100:
1107
+ raise gr.Error("輸入的文字長度過短,請重新輸入!")
1108
+
1109
  def verify_moderation(text):
1110
  response = OPEN_AI_MODERATION_CLIENT.moderations.create(input=text)
1111
  response_dict = response.model_dump()
 
1319
 
1320
  return content
1321
 
1322
+ def check_chinese_essay_feedback(feedback_check_prompt, chinese_essay_from_student_input, chinese_essay_feedback_check_input):
1323
+ verify_moderation(chinese_essay_from_student_input)
1324
+ verify_moderation(chinese_essay_feedback_check_input)
1325
+
1326
+ verify_string_length_short(chinese_essay_from_student_input)
1327
+ verify_string_length_short(chinese_essay_feedback_check_input)
1328
+
1329
+ # 檢查回饋是否符合規範
1330
+ sys_content = f"""
1331
+ 你是一位專業的中文寫作老師, 正在檢查自己要給學生的作文回饋。
1332
+ 請根據規範:
1333
+ {feedback_check_prompt}
1334
+ 來檢查回饋是否符合規範。
1335
+ """
1336
+ user_content = f"""
1337
+ 這是學生的原文: {chinese_essay_from_student_input}
1338
+ 這是老師的批改回饋: {chinese_essay_feedback_check_input}
1339
+ 請根據規範:
1340
+ {feedback_check_prompt}
1341
+ 來檢查老師的批改回饋是否符合規範。
1342
+ 符合的話則在該項目前面給予 ✅,給予為什麼給過的理由
1343
+ 不符合的話則在該項目前面給予 ❌,給予為什麼給不過的理由
1344
+
1345
+ 並在最後給出🟢🔴🟡 評分,🔴代表不合格,🟡代表需要修改,🟢代表合格。
1346
+ 再提供修改建議。
1347
+
1348
+ Example:
1349
+ ---
1350
+ # 規範檢查:
1351
+ - ✅ 1. 本篇佳句對該篇文章的正向肯定或佳句摘選 > 10 字
1352
+ - 原因:對文章的正向肯定或佳句摘選。
1353
+ - ✅ 2. 潤飾句子、刪冗詞贅字與修正錯別字、標點符號(提取文中使用得當的詞彙/提供不適合的詞綴替代字) > 5 字
1354
+ - 原因:提供了刪除冗詞及修正錯別字的內容。
1355
+ - ❌ 3. 檢視文章結構、段落安排是否完整、具有連貫性 > 10 字
1356
+ - 原因:未見有關文章結構與段落安排的評語。
1357
+ - ❌ 4. 評語:予以鼓勵,指出可精進方向 > 100 字
1358
+ - 原因:評語「寫得很好,還可以更好」遠不足 100 字,且未有具體指出可精進之處。
1359
+
1360
+ # 檢測結果:(🟢🔴🟡)
1361
+
1362
+ # 修改建議:
1363
+ - ...
1364
+ - ...
1365
+ """
1366
+
1367
+ messages = [
1368
+ {"role": "system", "content": sys_content},
1369
+ {"role": "user", "content": user_content}
1370
+ ]
1371
+
1372
+ request_payload = {
1373
+ "model": "gpt-4o",
1374
+ "messages": messages,
1375
+ "max_tokens": 2000,
1376
+ }
1377
+
1378
+ try:
1379
+ response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
1380
+ content = response.choices[0].message.content.strip()
1381
+ except Exception as e:
1382
+ print(f"檢查中文作文回饋時發生錯誤: {e}")
1383
+ raise gr.Error("網路塞車,請稍後重試!")
1384
+
1385
+ return content
1386
+
1387
+
1388
+
1389
  # Download doc
1390
  def create_word(content):
1391
  unique_filename = str(uuid.uuid4())
 
3422
  outputs=chinese_essay_idea_output
3423
  )
3424
 
3425
+ # 批改回饋檢測
3426
+ with gr.Tab("批改回饋檢測") as chinese_essay_feedback_check_tab:
3427
+ with gr.Row():
3428
+ gr.Markdown("# 批改回饋檢測")
3429
+ with gr.Row():
3430
+ with gr.Accordion("批改回饋檢測規範", open=False):
3431
+ feedback_check_prompt = gr.Markdown("""
3432
+ 1. 本篇佳句對該篇文章的正向肯定或佳句摘選 > 10 字
3433
+ 2. 潤飾句子、刪冗詞贅字與修正錯別字、標點符號(提取文中使用得當的詞彙/提供不適合的詞綴替代字) > 5 字
3434
+ 3. 檢視文章結構、段落安排是否完整、具有連貫性 > 10 字
3435
+ 4. 評語:予以鼓勵,指出可精進方向 > 100 字
3436
+ 5. 以上字數計算都不包含標點符號與空格
3437
+ """)
3438
+ with gr.Row():
3439
+ with gr.Column():
3440
+ chinese_essay_from_student_input = gr.TextArea(label="學生作文原文")
3441
+ chinese_essay_feedback_check_input = gr.TextArea(label="批改回饋")
3442
+ with gr.Column():
3443
+ chinese_essay_feedback_check_button = gr.Button("檢測", variant="primary")
3444
+ chinese_essay_feedback_check_output = gr.Markdown(label="檢測結果")
3445
+
3446
+ chinese_essay_feedback_check_button.click(
3447
+ fn=check_chinese_essay_feedback,
3448
+ inputs=[feedback_check_prompt, chinese_essay_from_student_input, chinese_essay_feedback_check_input],
3449
+ outputs=[chinese_essay_feedback_check_output]
3450
+ )
3451
+
3452
  with gr.Row(visible=False) as assignment_group:
3453
  with gr.Column():
3454
  with gr.Row():