youngtsai commited on
Commit
e205f14
·
1 Parent(s): 11fcd89

def get_chinese_paragraph_practice_log_session_content(file_name):

Browse files
Files changed (1) hide show
  1. app.py +207 -154
app.py CHANGED
@@ -907,12 +907,10 @@ def generate_chinese_paragraph_practice_history(
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,
913
  chinese_full_paragraph_refine_input,
 
914
  chinese_full_paragraph_refine_output_table,
915
- chinese_full_paragraph_refine_output,
916
  chinese_full_paragraph_save_output
917
  ):
918
  if user_data:
@@ -922,26 +920,22 @@ def generate_chinese_paragraph_practice_history(
922
  content = {
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'),
930
- "full_paragraph_refine_input": chinese_full_paragraph_refine_input,
931
- "full_paragraph_refine_output_table": chinese_full_paragraph_refine_output_table.to_dict(orient='records'),
932
- "full_paragraph_refine_output": chinese_full_paragraph_refine_output,
933
- "full_paragraph_save_output": chinese_full_paragraph_save_output
934
  }
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, \
942
  chinese_full_paragraph_refine_input, \
 
943
  chinese_full_paragraph_refine_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):
@@ -949,34 +943,28 @@ def get_chinese_paragraph_practice_log_session_content(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"])
957
- chinese_full_paragraph_refine_input_history = content_json["full_paragraph_refine_input"]
958
- chinese_full_paragraph_refine_output_table_history = pd.DataFrame(content_json["full_paragraph_refine_output_table"])
959
- chinese_full_paragraph_refine_output_history = content_json["full_paragraph_refine_output"]
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()
967
  chinese_full_paragraph_refine_input_history = ""
 
968
  chinese_full_paragraph_refine_output_table_history = pd.DataFrame()
969
- chinese_full_paragraph_refine_output_history = ""
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, \
977
  chinese_full_paragraph_refine_input_history, \
 
978
  chinese_full_paragraph_refine_output_table_history, \
979
- chinese_full_paragraph_refine_output_history, \
980
  chinese_full_paragraph_save_output_history
981
 
982
 
@@ -1008,15 +996,54 @@ def get_chinese_paragraph_evaluate_content(model, sys_content, paragraph, user_g
1008
  ["錯別字與標點符號", data['錯別字與標點符號']['level'], data['錯別字與標點符號']['explanation']]
1009
  ]
1010
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1011
  gr_update = gr.update(value=table_data, headers=headers, visible=True)
1012
 
1013
  return content_text, gr_update
1014
 
1015
-
1016
  def generate_content_by_open_ai_assistant(user_content, thread_id=None, model_name=None):
1017
  client = CUTOR_OPEN_AI_CLIENT
1018
  assistant_id = CUTOR_OPEN_AI_ASSISTANT_ID
1019
 
 
 
 
 
 
 
 
1020
  try:
1021
  if not thread_id:
1022
  thread = client.beta.threads.create()
@@ -1035,7 +1062,11 @@ def generate_content_by_open_ai_assistant(user_content, thread_id=None, model_na
1035
 
1036
  # Run the assistant
1037
  print("==============Run the assistant====================")
1038
- run = client.beta.threads.runs.create_and_poll(thread_id=thread.id, assistant_id=assistant_id)
 
 
 
 
1039
 
1040
  # Wait for the response
1041
  # run_status = poll_run_status(run.id, thread.id, timeout=30)
@@ -2784,36 +2815,22 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
2784
  with gr.Row(visible=False) as chinese_full_paragraph_params:
2785
  chinese_full_paragraph_sys_content_input = gr.Textbox(label="System Prompt", value="You are a Chinese teacher who is practicing with me to improve my Chinese writing skill.")
2786
  default_user_generate_chinese_full_paragraph_evaluate_prompt = """
2787
- 你是一位台灣的繁體中文(zh-tw)作文老師,你的學生包含的年齡層從國小三年級(9歲)到國小六年級(12歲)不等。你的初衷是希望在給予學生鼓勵的同時,直接從學生原文的遣詞、造句以及結構提出建議。請注意,所有的批改範例或是資料庫的範文僅作為範例參考,請勿直接引用。
2788
-
2789
- 請你用以下的指示批改作文:
2790
- 1. 首先判斷學生的年級並使用資料庫的「國小作文評分標準」
2791
-
2792
- 2. 就「主題與內容」、「段落結構」、「遣詞造句」和「錯別字與標點符號」四個面向,請根據裡面A+、A、A- B+、 B、 B-等級,每一個面向給予(1) 等級,以及(2)回饋。並請遵守以下框架:
2793
- - 「主題與內容」:提供一段回饋(優點+建議),並從稱讚學生的優點開始。若成績是A+,則不需要給予建議;若是其他等級,請以「三明治回饋法」進行回饋。
2794
- - 「段落結構」:這一個面向的重點是學生的分段是否恰當,文意的銜接是否通順,請勿改錯別字。請幫每一段的重點摘要之後,若段落的銜接有明顯不通順或邏輯斷裂之處,給予建議。請注意不要用「銜接」或「過渡」等艱澀用語,小學生無法理解。
2795
- 《批改範例》:
2796
- 原文:「二月十九日結業式,就要放寒假了!我既期待又開心。大家就像是離開牢籠的狗、直奔回家,可是我卻得收拾行李,展開五天四夜的冬令營。 寒假第一天開始了。我滿心期待地去台北集合,既興奮又緊張。踏上遊覽車後一路上,隊輔一直講童軍的注意事項及這幾天的行程,我才發覺訓練要開始了。每天洗五分鐘戰鬥澡、上將近十二小時的舞蹈課和合唱團練習,每天都過得充實又開心!因為天氣太冷,所以沒有睡帳篷、改睡風雨教室。大家躺在同一地板,一起聊天,還聽到一堆人的打呼聲,吵到睡不著。 這次的冬令營還請了泰雅族頭目教我們射弓箭、做勇士帽、訴說族靈及傳統屋的習俗。我們還去了粉鳥林魚港的秘境、豆腐岬聽海浪、去寒溪聚落走寒溪吊橋、還去了宜蘭縣政府聽宜蘭的文化史。五天四夜的竹卜水冬令營既忙碌又充實啊! 最後一天的離別是最難過的時候。才剛認識新朋友就要分開了,不知何時才會再見面。雖然每次離開家去參加冬令營都會哭,但要離開新朋友時又會難過。 美好的冬令營就這樣結束了。我以後還是會參加的!」
2797
- 批改回饋:你的段落結構清晰,每段都有明確的主題和內容。第一段說明寒假開始了;第二段描述冬令營每天的作息;第三段分享特殊的原住民生活體驗;第四段描繪冬令營結束捨不得的心情;第五段期待未來的冬令營。整體來每一段的發揮都不錯,不過第二段沒有點出你參加的是「竹卜水冬令營」,就直接從冬令營的行程開始,文意連接不夠順暢。建議第二段可以改為「寒假第一天我滿心期待地去台北集合,前往竹卜水冬令營的地點」,讓讀者更能跟上文章的情節。
2798
-
2799
- - 「遣詞造句」: 這一個面向的重點是根據上下文,判斷學生的詞語是否恰當,以及句子的結構是否需要優化。成語的使用請參考資料庫裡的「教育部成語字典」。請按照原文逐段給予回饋(如果該段沒有需要修改的可跳過)。
2800
- 《批改範例》:
2801
- 原文:「二月十九日結業式,就要放寒假了!我既期待又開心。大家就像是離開牢籠的狗、直奔回家,可是我卻得收拾行李,展開五天四夜的冬令營。 寒假第一天開始了。我滿心期待地去台北集合,既興奮又緊張。踏上遊覽車後一路上,隊輔一直講童軍的注意事項及這幾天的行程,我才發覺訓練要開始了。每天洗五分鐘戰鬥澡、上將近十二小時的舞蹈課和合唱團練習,每天都過得充實又開心!因為天氣太冷,所以沒有睡帳篷、改睡風雨教室。大家躺在同一地板,一起聊天,還聽到一堆人的打呼聲,吵到睡不著。 這次的冬令營還請了泰雅族頭目教我們射弓箭、做勇士帽、訴說族靈及傳統屋的習俗。我們還去了粉鳥林魚港的秘境、豆腐岬聽海浪、去寒溪聚落走寒溪吊橋、還去了宜蘭縣政府聽宜蘭的文化史。五天四夜的竹卜水冬令營既忙碌又充實啊! 最後一天的離別是最難過的時候。才剛認識新朋友就要分開了,不知何時才會再見面。雖然每次離開家去參加冬令營都會哭,但要離開新朋友時又會難過。 美好的冬令營就這樣結束了。我以後還是會參加的!」
2802
- 批改回饋:你的文章用詞基本正確,句子結構也清楚。但有些地方的用詞可以更精準。第一段建議把「離開牢籠的狗」改為「離開牢籠的鳥」,更符合一般的用法。第二段「每天洗五分鐘戰鬥澡、上將近十二小時的舞蹈課和合唱團練習,每天都過得充實又開心」可以藉由連接讓整句話更生動。此外也記得善用成語或狀聲詞,例如「一堆人的打呼聲」可以改為「一堆人的鼾聲隆隆」。最後,小心避免過多的驚嘆號,而是用其他詞語加強情感的表達 。
2803
-
2804
- - 「錯別字與標點符號」: 請參考資料庫裡的「教育部標點符號手冊」和「的和得使用方法」批改,不在「教育部標點符號手冊」內的標點符號不用批改。
2805
-
2806
- 4. 參考資料庫的「國小作文批改範本」作為批改格式和原則的參考,但每篇文章的實際情況不同,請勿直接挪用範本裡的評語。
2807
-
2808
- 5. 國小學生的識字量有限,給予回饋和修改文章時,請盡量使用資料庫裡「2500常用字」裡列出的漢字。
2809
-
2810
- 6. 批改回饋的最後請引用原文給予至少三個詞語或是句型改寫的範例,列出原文和修改的版本,修改的案例應該和前面四個面向的批改內容前後呼應。
2811
-
2812
- 7. 將評分標準與回饋的內容以JSON格式輸出,並且請使用繁體中文(ZH-TW)來評分段落並輸出,用 ```json ..... ``` 包裹:
2813
-
2814
- please use Chinese language (ZH-TW) to evaluate the paragraph and output use JSON format:
2815
 
2816
  EXAMPLE:
 
 
2817
  # 綜合回饋
2818
  你的文章...............(寫出一段話,來總結這篇作文的好壞)
2819
 
@@ -2827,11 +2844,12 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
2827
  錯別字與標點符號:A+ 你的文章沒有明顯的錯別字,並且標點符號的使用也相當正確,這點做得很好。
2828
 
2829
  # 修改範例
2830
- 原文:內容雖然簡短,但主題明確。 修改:雖然內容相對簡短,但主題表達得非常明確。
2831
-
2832
- 原文:缺乏實例和具體情境來支持內容。 修改:目前缺少具體的實例及情境來支持文章的內容與主張。
2833
-
2834
- 原文:可以進一步擴展。 修改:可以進一步擴展來豐富內容,讓讀者更有共鳴。
 
2835
 
2836
  ```json
2837
  {{
@@ -2871,36 +2889,115 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
2871
  with gr.Row():
2872
  chinese_full_paragraph_evaluate_output_text = gr.Markdown(label="段落全文分析")
2873
  with gr.Row():
2874
- chinese_full_paragraph_evaluate_output = gr.Dataframe(label="段落全文分析表格", wrap=True, column_widths=[20, 15, 65], interactive=False)
2875
 
2876
  # JUTOR 段落批改與整體建議
2877
- with gr.Row():
2878
- gr.Markdown("# JUTOR 段落批改與整體建議")
2879
- with gr.Row():
2880
- gr.Markdown("## 修訂文法與拼字錯誤")
2881
- with gr.Row():
2882
- with gr.Column():
2883
- chinese_full_paragraph_correct_grammatical_spelling_errors_input = gr.TextArea(label="這是你的原始寫作內容,參考 JUTOR 的建議,你可以選擇是否修改:")
2884
- with gr.Column():
2885
- generate_chinese_full_paragraph_correct_grammatical_spelling_errors_button = gr.Button("修訂文法與拼字錯誤", variant="primary")
2886
- chinese_full_paragraph_correct_grammatical_spelling_errors_output_table = gr.Dataframe(label="修訂文法與拼字錯誤", interactive=False, column_widths=[30, 30, 40])
2887
- revised_chinese_full_paragraph_output = gr.Textbox(label="Revised Paragraph", show_copy_button=True, visible=False)
2888
- gr.Markdown("## 修訂結果")
2889
- revised_chinese_full_paragraph_diff = gr.HTML()
2890
 
2891
  # JUTOR 段落批改與整體建議
 
 
 
 
 
 
 
 
 
 
 
 
 
2892
  with gr.Row():
2893
- gr.Markdown("## 段落改善建議")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2894
  with gr.Row():
2895
  with gr.Column():
2896
- chinese_full_paragraph_refine_input = gr.TextArea(label="這是你的原始寫作內容,參考 JUTOR 的建議,你可以選擇是否修改:", show_copy_button=True)
2897
  with gr.Column():
2898
- generate_chinese_full_paragraph_refine_button = gr.Button("段落改善建議", variant="primary")
2899
- chinese_full_paragraph_refine_output_table = gr.DataFrame(label="段落改善建議", wrap=True, interactive=False)
2900
- chinese_full_paragraph_refine_output = gr.HTML(label="修改建議", visible=False)
2901
- gr.Markdown("## 修改結果")
2902
- chinese_full_paragraph_refine_output_diff = gr.HTML()
2903
-
2904
  # 寫作完成
2905
  with gr.Row():
2906
  gr.Markdown("# 寫作完成")
@@ -2919,20 +3016,15 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
2919
  chinese_full_paragraph_input_history = gr.Markdown()
2920
  gr.Markdown("<span style='color:#4e80ee'>段落全文分析</span>")
2921
  chinese_full_paragraph_evaluate_output_text_history = gr.Markdown()
2922
- chinese_full_paragraph_evaluate_output_history = gr.Dataframe(wrap=True, column_widths=[35, 15, 50], interactive=False)
2923
-
2924
- gr.Markdown("<span style='color:#4e80ee'>修訂文法與拼字錯誤 輸入</span>")
2925
- chinese_full_paragraph_correct_grammatical_spelling_errors_input_history = gr.Markdown()
2926
- gr.Markdown("<span style='color:#4e80ee'>修訂文法與拼字錯誤</span>")
2927
- chinese_full_paragraph_correct_grammatical_spelling_errors_output_table_history = gr.Dataframe(interactive=False, wrap=True, column_widths=[30, 30, 40])
2928
-
2929
- gr.Markdown("<span style='color:#4e80ee'>段落改善建議 輸入</span>")
2930
- chinese_full_paragraph_refine_input_history = gr.Markdown()
2931
- gr.Markdown("<span style='color:#4e80ee'>段落改善建議</span>")
2932
- chinese_full_paragraph_refine_output_table_history = gr.Dataframe(wrap=True, interactive=False, column_widths=[30, 30, 40])
2933
 
2934
- gr.Markdown("<span style='color:#4e80ee'>修改建議</span>")
2935
- chinese_full_paragraph_refine_output_history = gr.Markdown()
2936
  gr.Markdown("<span style='color:#4e80ee'>修改結果</span>")
2937
  chinese_full_paragraph_save_output_history = gr.Markdown()
2938
 
@@ -2944,23 +3036,19 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
2944
  request_origin,
2945
  chinese_full_paragraph_input,
2946
  chinese_full_paragraph_evaluate_output_text,
2947
- chinese_full_paragraph_evaluate_output,
2948
- chinese_full_paragraph_correct_grammatical_spelling_errors_input,
2949
- chinese_full_paragraph_correct_grammatical_spelling_errors_output_table,
2950
  chinese_full_paragraph_refine_input,
 
2951
  chinese_full_paragraph_refine_output_table,
2952
- chinese_full_paragraph_refine_output,
2953
  chinese_full_paragraph_save_output
2954
  ],
2955
  outputs=[
2956
  chinese_full_paragraph_input_history,
2957
  chinese_full_paragraph_evaluate_output_text_history,
2958
- chinese_full_paragraph_evaluate_output_history,
2959
- chinese_full_paragraph_correct_grammatical_spelling_errors_input_history,
2960
- chinese_full_paragraph_correct_grammatical_spelling_errors_output_table_history,
2961
  chinese_full_paragraph_refine_input_history,
 
2962
  chinese_full_paragraph_refine_output_table_history,
2963
- chinese_full_paragraph_refine_output_history,
2964
  chinese_full_paragraph_save_output_history
2965
  ]
2966
  ).then(
@@ -2969,47 +3057,20 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
2969
  outputs=[chinese_grapragh_practice_history_accordion]
2970
  )
2971
 
2972
- # chinese_full_paragraph_evaluate_button.click(
2973
- # fn=generate_chinese_evaluation_table,
2974
- # inputs=[model, chinese_full_paragraph_sys_content_input, user_generate_chinese_full_paragraph_evaluate_prompt, chinese_full_paragraph_input],
2975
- # outputs=chinese_full_paragraph_evaluate_output
2976
- # ).then(
2977
- # fn=update_paragraph_correct_grammatical_spelling_errors_input,
2978
- # inputs=[chinese_full_paragraph_input],
2979
- # outputs=chinese_full_paragraph_correct_grammatical_spelling_errors_input
2980
- # )
2981
  chinese_full_paragraph_evaluate_button.click(
2982
  fn=get_chinese_paragraph_evaluate_content,
2983
  inputs=[model, chinese_full_paragraph_sys_content_input, chinese_full_paragraph_input, user_generate_chinese_full_paragraph_evaluate_prompt],
2984
- outputs=[chinese_full_paragraph_evaluate_output_text, chinese_full_paragraph_evaluate_output]
2985
  ).then(
2986
- fn=update_paragraph_correct_grammatical_spelling_errors_input,
2987
  inputs=[chinese_full_paragraph_input],
2988
- outputs=chinese_full_paragraph_correct_grammatical_spelling_errors_input
2989
- )
2990
-
2991
- generate_chinese_full_paragraph_correct_grammatical_spelling_errors_button.click(
2992
- fn=generate_correct_grammatical_spelling_errors,
2993
- inputs=[model, chinese_full_paragraph_sys_content_input, eng_level_input, chinese_full_paragraph_correct_grammatical_spelling_errors_input, user_correct_grammatical_spelling_errors_prompt],
2994
- outputs=[chinese_full_paragraph_correct_grammatical_spelling_errors_output_table, revised_chinese_full_paragraph_output]
2995
- ).then(
2996
- fn=highlight_diff_texts,
2997
- inputs=[chinese_full_paragraph_correct_grammatical_spelling_errors_output_table, revised_chinese_full_paragraph_output],
2998
- outputs=revised_chinese_full_paragraph_diff
2999
- ).then(
3000
- fn=update_paragraph_refine_input,
3001
- inputs=[chinese_full_paragraph_correct_grammatical_spelling_errors_input],
3002
  outputs=chinese_full_paragraph_refine_input
3003
  )
3004
 
3005
  generate_chinese_full_paragraph_refine_button.click(
3006
- fn=generate_refine_paragraph,
3007
- inputs=[model, chinese_full_paragraph_sys_content_input, eng_level_input, chinese_full_paragraph_refine_input, user_refine_paragraph_prompt],
3008
- outputs=[chinese_full_paragraph_refine_output_table, chinese_full_paragraph_refine_output]
3009
- ).then(
3010
- fn=highlight_diff_texts,
3011
- inputs=[chinese_full_paragraph_refine_output_table, chinese_full_paragraph_refine_output],
3012
- outputs=chinese_full_paragraph_refine_output_diff
3013
  )
3014
 
3015
  chinese_full_paragraph_save_button.click(
@@ -3033,20 +3094,14 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
3033
  chinese_full_paragraph_input_history_log = gr.Markdown()
3034
  gr.Markdown("<span style='color:#4e80ee'>段落全文分析</span>")
3035
  chinese_full_paragraph_evaluate_output_text_history_log = gr.Markdown()
3036
- chinese_full_paragraph_evaluate_output_history_log = gr.Dataframe(wrap=True, column_widths=[35, 15, 50], interactive=False)
3037
-
3038
- gr.Markdown("<span style='color:#4e80ee'>修訂文法與拼字錯誤 輸入</span>")
3039
- chinese_full_paragraph_correct_grammatical_spelling_errors_input_history_log = gr.Markdown()
3040
- gr.Markdown("<span style='color:#4e80ee'>修訂文法與拼字錯誤</span>")
3041
- chinese_full_paragraph_correct_grammatical_spelling_errors_output_table_history_log = gr.Dataframe(interactive=False, wrap=True, column_widths=[30, 30, 40])
3042
 
3043
  gr.Markdown("<span style='color:#4e80ee'>段落改善建議 輸入</span>")
3044
  chinese_full_paragraph_refine_input_history_log = gr.Markdown()
3045
  gr.Markdown("<span style='color:#4e80ee'>段落改善建議</span>")
 
3046
  chinese_full_paragraph_refine_output_table_history_log = gr.Dataframe(wrap=True, interactive=False, column_widths=[30, 30, 40])
3047
 
3048
- gr.Markdown("<span style='color:#4e80ee'>修改建議</span>")
3049
- chinese_full_paragraph_refine_output_history_log = gr.Markdown()
3050
  gr.Markdown("<span style='color:#4e80ee'>修改結果</span>")
3051
  chinese_full_paragraph_save_output_history_log = gr.Markdown()
3052
 
@@ -3062,12 +3117,10 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
3062
  outputs=[
3063
  chinese_full_paragraph_input_history_log,
3064
  chinese_full_paragraph_evaluate_output_text_history_log,
3065
- chinese_full_paragraph_evaluate_output_history_log,
3066
- chinese_full_paragraph_correct_grammatical_spelling_errors_input_history_log,
3067
- chinese_full_paragraph_correct_grammatical_spelling_errors_output_table_history_log,
3068
  chinese_full_paragraph_refine_input_history_log,
 
3069
  chinese_full_paragraph_refine_output_table_history_log,
3070
- chinese_full_paragraph_refine_output_history_log,
3071
  chinese_full_paragraph_save_output_history_log
3072
  ]
3073
  )
 
907
  request_origin,
908
  chinese_full_paragraph_input,
909
  chinese_full_paragraph_evaluate_output_text,
910
+ chinese_full_paragraph_evaluate_output_table,
 
 
911
  chinese_full_paragraph_refine_input,
912
+ chinese_full_paragraph_refine_output_text,
913
  chinese_full_paragraph_refine_output_table,
 
914
  chinese_full_paragraph_save_output
915
  ):
916
  if user_data:
 
920
  content = {
921
  "session_timestamp": session_timestamp,
922
  "request_origin": request_origin,
923
+ "chinese_full_paragraph_input": chinese_full_paragraph_input,
924
  "chinese_full_paragraph_evaluate_output_text": chinese_full_paragraph_evaluate_output_text,
925
+ "chinese_full_paragraph_evaluate_output_table": chinese_full_paragraph_evaluate_output_table.to_dict(orient='records'),
926
+ "chinese_full_paragraph_refine_input": chinese_full_paragraph_refine_input,
927
+ "chinese_full_paragraph_refine_output_text": chinese_full_paragraph_refine_output_text,
928
+ "chinese_full_paragraph_refine_output_table": chinese_full_paragraph_refine_output_table.to_dict(orient='records'),
929
+ "chinese_full_paragraph_save_output": chinese_full_paragraph_save_output
 
 
930
  }
931
  GCS_SERVICE.upload_json_string("jutor_logs", file_name, json.dumps(content))
932
 
933
  return chinese_full_paragraph_input, \
934
  chinese_full_paragraph_evaluate_output_text, \
935
+ chinese_full_paragraph_evaluate_output_table, \
 
 
936
  chinese_full_paragraph_refine_input, \
937
+ chinese_full_paragraph_refine_output_text, \
938
  chinese_full_paragraph_refine_output_table, \
 
939
  chinese_full_paragraph_save_output
940
 
941
  def get_chinese_paragraph_practice_log_session_content(file_name):
 
943
  content = GCS_SERVICE.download_as_string("jutor_logs", file_name)
944
  print(f"content: {content}")
945
  content_json = json.loads(content)
946
+ chinese_full_paragraph_input_history = content_json["chinese_full_paragraph_input"]
947
  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 ""
948
+ chinese_full_paragraph_evaluate_output_table_history = pd.DataFrame(content_json["chinese_full_paragraph_evaluate_output_table"])
949
+ chinese_full_paragraph_refine_input_history = content_json["chinese_full_paragraph_refine_input"]
950
+ chinese_full_paragraph_refine_output_text_history = content_json["chinese_full_paragraph_refine_output_text"]
951
+ chinese_full_paragraph_refine_output_table_history = pd.DataFrame(content_json["chinese_full_paragraph_refine_output_table"])
952
+ chinese_full_paragraph_save_output_history = content_json["chinese_full_paragraph_save_output"]
 
 
953
  else:
954
  chinese_full_paragraph_input_history = ""
955
  chinese_full_paragraph_evaluate_output_text_history = ""
956
+ chinese_full_paragraph_evaluate_output_table_history = pd.DataFrame()
 
 
957
  chinese_full_paragraph_refine_input_history = ""
958
+ chinese_full_paragraph_refine_output_text_history = ""
959
  chinese_full_paragraph_refine_output_table_history = pd.DataFrame()
 
960
  chinese_full_paragraph_save_output_history = ""
961
 
962
  return chinese_full_paragraph_input_history, \
963
  chinese_full_paragraph_evaluate_output_text_history, \
964
+ chinese_full_paragraph_evaluate_output_table_history, \
 
 
965
  chinese_full_paragraph_refine_input_history, \
966
+ chinese_full_paragraph_refine_output_text_history, \
967
  chinese_full_paragraph_refine_output_table_history, \
 
968
  chinese_full_paragraph_save_output_history
969
 
970
 
 
996
  ["錯別字與標點符號", data['錯別字與標點符號']['level'], data['錯別字與標點符號']['explanation']]
997
  ]
998
 
999
+ content_table = gr.update(value=table_data, headers=headers, visible=True)
1000
+
1001
+ return content_text, content_table
1002
+
1003
+ def get_chinese_paragraph_refine_evaluate_content(model, sys_content, paragraph_1, paragraph_2, user_refine_paragraph_prompt):
1004
+ user_content = f"""
1005
+ sys_content: {sys_content}
1006
+ ---
1007
+ original paragraph is: {paragraph_1}
1008
+ ---
1009
+ refined paragraph is: {paragraph_2}
1010
+ ---
1011
+ {user_refine_paragraph_prompt}
1012
+ """
1013
+ content = generate_content_by_open_ai_assistant(user_content, thread_id=None, model_name=model)
1014
+ print(f"====generate_paragraph_evaluate====")
1015
+ print(content)
1016
+
1017
+ content_list = content.split("```json")
1018
+ content_text = content_list[0]
1019
+ print(f"content_text: {content_text}")
1020
+ content_json = content_list[1].replace("```", "")
1021
+ print(f"content_json: {content_json}")
1022
+
1023
+ data = json.loads(content_json)["results"]
1024
+ headers = ["架構", "評分", "解釋"]
1025
+ table_data = [
1026
+ ["主題與內容", data['主題與內容']['level'], data['主題與內容']['explanation']],
1027
+ ["段落結構", data['段落結構']['level'], data['段落結構']['explanation']],
1028
+ ["遣詞造句", data['遣詞造句']['level'], data['遣詞造句']['explanation']],
1029
+ ["錯別字與標點符號", data['錯別字與標點符號']['level'], data['錯別字與標點符號']['explanation']]
1030
+ ]
1031
+
1032
  gr_update = gr.update(value=table_data, headers=headers, visible=True)
1033
 
1034
  return content_text, gr_update
1035
 
 
1036
  def generate_content_by_open_ai_assistant(user_content, thread_id=None, model_name=None):
1037
  client = CUTOR_OPEN_AI_CLIENT
1038
  assistant_id = CUTOR_OPEN_AI_ASSISTANT_ID
1039
 
1040
+ assistant = client.beta.assistants.update(
1041
+ assistant_id=assistant_id,
1042
+ tools=[{"type": "file_search"}],
1043
+ )
1044
+ print(f"My assistant: {assistant}")
1045
+ print(f"instructions: {assistant.instructions}")
1046
+
1047
  try:
1048
  if not thread_id:
1049
  thread = client.beta.threads.create()
 
1062
 
1063
  # Run the assistant
1064
  print("==============Run the assistant====================")
1065
+ run = client.beta.threads.runs.create_and_poll(
1066
+ thread_id=thread.id,
1067
+ assistant_id=assistant.id,
1068
+ tools=[{"type": "file_search"}],
1069
+ )
1070
 
1071
  # Wait for the response
1072
  # run_status = poll_run_status(run.id, thread.id, timeout=30)
 
2815
  with gr.Row(visible=False) as chinese_full_paragraph_params:
2816
  chinese_full_paragraph_sys_content_input = gr.Textbox(label="System Prompt", value="You are a Chinese teacher who is practicing with me to improve my Chinese writing skill.")
2817
  default_user_generate_chinese_full_paragraph_evaluate_prompt = """
2818
+ # 請嚴格根據 instructions
2819
+
2820
+ # 整體評分標準
2821
+ - 🟢 :「主題與內容」為A+ 或 A,其他三個面向(「段落結構」、「遣詞造句」和「錯別字與標點符號」) 至少有一個 A+ 或 A,所有等級都不能低於B
2822
+ - 🟡:「主題與內容」為A- 或 B+,其他三個面向(「段落結構」、「遣詞造句」和「錯別字與標點符號」) 至少有一個 A- 或 B+,所有等級都不能低於B
2823
+ - 🔴:「主題與內容」所有等級都是BB-
2824
+ - 任何不屬於以上三個範疇的狀況都請給🟡
2825
+
2826
+ # Output rules:
2827
+ 1. 先給 整體評分、綜合回饋、評分標準與回饋、修改範例
2828
+ 2. 再將評分標準與回饋的內容以JSON格式輸出,並且請使用繁體中文(ZH-TW)來評分段落並輸出,用 ```json ..... ``` 包裹:
2829
+ 3. please use Chinese language (ZH-TW) to evaluate the paragraph and output use JSON format:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2830
 
2831
  EXAMPLE:
2832
+ # 整體評分:🟡
2833
+
2834
  # 綜合回饋
2835
  你的文章...............(寫出一段話,來總結這篇作文的好壞)
2836
 
 
2844
  錯別字與標點符號:A+ 你的文章沒有明顯的錯別字,並且標點符號的使用也相當正確,這點做得很好。
2845
 
2846
  # 修改範例
2847
+ - 原文:內容雖然簡短,但主題明確。
2848
+ - 修改:雖然內容相對簡短,但主題表達得非常明確。
2849
+ - 原文:缺乏實例和具體情境來支持內容。
2850
+ - 修改:目前缺少具體的實例及情境來支持文章的內容與主張。
2851
+ - 原文:可以進一步擴展。
2852
+ - 修改:可以進一步擴展來豐富內容,讓讀者更有共鳴。
2853
 
2854
  ```json
2855
  {{
 
2889
  with gr.Row():
2890
  chinese_full_paragraph_evaluate_output_text = gr.Markdown(label="段落全文分析")
2891
  with gr.Row():
2892
+ chinese_full_paragraph_evaluate_output_table = gr.Dataframe(label="段落全文分析表格", wrap=True, column_widths=[20, 15, 65], interactive=False)
2893
 
2894
  # JUTOR 段落批改與整體建議
2895
+ # with gr.Row():
2896
+ # gr.Markdown("# JUTOR 段落批改與整體建議")
2897
+ # with gr.Row():
2898
+ # gr.Markdown("## 修訂文法與拼字錯誤")
2899
+ # with gr.Row():
2900
+ # with gr.Column():
2901
+ # chinese_full_paragraph_correct_grammatical_spelling_errors_input = gr.TextArea(label="這是你的原始寫作內容,參考 JUTOR 的建議,你可以選擇是否修改:")
2902
+ # with gr.Column():
2903
+ # generate_chinese_full_paragraph_correct_grammatical_spelling_errors_button = gr.Button("修訂文法與拼字錯誤", variant="primary")
2904
+ # chinese_full_paragraph_correct_grammatical_spelling_errors_output_table = gr.Dataframe(label="修訂文法與拼字錯誤", interactive=False, column_widths=[30, 30, 40])
2905
+ # revised_chinese_full_paragraph_output = gr.Textbox(label="Revised Paragraph", show_copy_button=True, visible=False)
2906
+ # gr.Markdown("## 修訂結果")
2907
+ # revised_chinese_full_paragraph_diff = gr.HTML()
2908
 
2909
  # JUTOR 段落批改與整體建議
2910
+ # with gr.Row():
2911
+ # gr.Markdown("## 段落改善建議")
2912
+ # with gr.Row():
2913
+ # with gr.Column():
2914
+ # chinese_full_paragraph_refine_input = gr.TextArea(label="這是你的原始寫作內容,參考 JUTOR 的建議,你可以選擇是否修改:", show_copy_button=True)
2915
+ # with gr.Column():
2916
+ # generate_chinese_full_paragraph_refine_button = gr.Button("段落改善建議", variant="primary")
2917
+ # chinese_full_paragraph_refine_output_table = gr.DataFrame(label="段落改善建議", wrap=True, interactive=False)
2918
+ # chinese_full_paragraph_refine_output = gr.HTML(label="修改建議", visible=False)
2919
+ # gr.Markdown("## 修改結果")
2920
+ # chinese_full_paragraph_refine_output_diff = gr.HTML()
2921
+
2922
+ # 修改文章
2923
  with gr.Row():
2924
+ gr.Markdown("# 根據建議修改文章")
2925
+ with gr.Row(visible=False) as chinese_full_paragraph_refine_params:
2926
+ default_user_generate_chinese_full_paragraph_refine_evaluate_prompt = """
2927
+ # 請嚴格根據 instructions
2928
+ 我給你兩篇文章,請進行比較跟批改,並給出建議。
2929
+
2930
+ # 整體評分標準
2931
+ - 🟢 :「主題與內容」為A+ 或 A,其他三個面向(「段落結構」、「遣詞造句」和「錯別字與標點符號」) 至少有一個 A+ 或 A,所有等級都不能低於B
2932
+ - 🟡:「主題與內容」為A- 或 B+,其他三個面向(「段落結構」、「遣詞造句」和「錯別字與標點符號」) 至少有一個 A- 或 B+,所有等級都不能低於B
2933
+ - 🔴:「主題與內容」所有等級都是B或B-
2934
+ - 任何不屬於以上三個範疇的狀況都請給🟡
2935
+
2936
+ # Output rules:
2937
+ 1. 先給 整體評分、綜合回饋、評分標準與回饋、修改範例
2938
+ 2. 再將評分標準與回饋的內容以JSON格式輸出,並且請使用繁體中文(ZH-TW)來評分段落並輸出,用 ```json ..... ``` 包裹:
2939
+ 3. please use Chinese language (ZH-TW) to evaluate the paragraph and output use JSON format:
2940
+
2941
+ EXAMPLE:
2942
+ # 整體評分:🟡
2943
+
2944
+ # 綜合回饋(前後比較)
2945
+ 你的文章...............(寫出一段話,比較兩篇作文的差異)
2946
+
2947
+ # 評分標準與回饋
2948
+ 主題與內容:B+ 你的主題很明確,講述了CSS在渲染空間上的問題及解決方案,這是一個重要而實用的話題。然而,內容相對較少,缺乏足夠的細節與實例來支撐你的觀點。建議你可以添加一些具體情境或例子,讓讀者更容易理解CSS的應用情況,例如提到常見的渲染問題以及具體的解決方法。
2949
+
2950
+ 段落結構:B 你的段落結構基本清晰,但目前只有一段,這使得整體文章顯得有些單薄。建議你可以將內容分成幾個小段落,每個段落著重於不同的要點,例如一段說明問題,另一段探討解決方案,這樣整體更具條理性。
2951
+
2952
+ 遣詞造句:A 你的遣詞造句大致良好,用詞得體且通順。不過可以嘗試加一些更具體的技術詞彙或示例,使文章更專業化。
2953
+
2954
+ 錯別字與標點符號:A+ 你的文章沒有明顯的錯別字,並且標點符號的使用也相當正確,這點做得很好。
2955
+
2956
+ # 修改範例
2957
+ - 原文:內容雖然簡短,但主題明確。
2958
+ - 修改:雖然內容相對簡短,但主題表達得非常明確。
2959
+ - 原文:缺乏實例和具體情境來支持內容。
2960
+ - 修改:目前缺少具體的實例及情境來支持文章的內容與主張。
2961
+ - 原文:可以進一步擴展。
2962
+ - 修改:可以進一步擴展來豐富內容,讓讀者更有共鳴。
2963
+
2964
+ ```json
2965
+ {{
2966
+ "results": {{
2967
+ "主題與內容": {{
2968
+ "level": "A+",
2969
+ "explanation": "#中文解釋 ZH-TW"
2970
+ }},
2971
+ "段落結構": {{
2972
+ "level": "B+",
2973
+ "explanation": "#中文解釋 ZH-TW"
2974
+ }},
2975
+ "遣詞造句": {{
2976
+ "level": "C",
2977
+ "explanation": "#中文解釋 ZH-TW"
2978
+ }},
2979
+ "錯別字與標點符號": {{
2980
+ "level": "C-",
2981
+ "explanation": "#中文解釋 ZH-TW"
2982
+ }}
2983
+ }}
2984
+ }}
2985
+ ```
2986
+
2987
+ Restrictions:
2988
+ - ALL the content should be in Traditional Chinese (zh-TW), it's very important.
2989
+ """
2990
+ user_generate_chinese_full_paragraph_refine_evaluate_prompt = gr.Textbox(label="Paragraph evaluate Prompt", value=default_user_generate_chinese_full_paragraph_refine_evaluate_prompt)
2991
  with gr.Row():
2992
  with gr.Column():
2993
+ chinese_full_paragraph_refine_input = gr.Textbox(label="這是你的原始寫作內容,參考建議,你可以選擇是否修改:", show_copy_button=True)
2994
  with gr.Column():
2995
+ with gr.Row():
2996
+ generate_chinese_full_paragraph_refine_button = gr.Button("段落全文分析", variant="primary")
2997
+ with gr.Row():
2998
+ chinese_full_paragraph_refine_output_text = gr.Markdown(label="段落全文分析")
2999
+ with gr.Row():
3000
+ chinese_full_paragraph_refine_output_table = gr.Dataframe(label="段落全文分析表格", wrap=True, column_widths=[20, 15, 65], interactive=False)
3001
  # 寫作完成
3002
  with gr.Row():
3003
  gr.Markdown("# 寫作完成")
 
3016
  chinese_full_paragraph_input_history = gr.Markdown()
3017
  gr.Markdown("<span style='color:#4e80ee'>段落全文分析</span>")
3018
  chinese_full_paragraph_evaluate_output_text_history = gr.Markdown()
3019
+ chinese_full_paragraph_evaluate_output_table_history = gr.Dataframe(wrap=True, column_widths=[35, 15, 50], interactive=False)
3020
+
3021
+ # 根據建議修改文章
3022
+ gr.Markdown("<span style='color:#4e80ee'>根據建議修改文章 輸入</span>")
3023
+ chinese_full_paragraph_refine_input_history = gr.Markdown()
3024
+ gr.Markdown("<span style='color:#4e80ee'>全文分析</span>")
3025
+ chinese_full_paragraph_refine_output_text_history = gr.Markdown()
3026
+ chinese_full_paragraph_refine_output_table_history = gr.Dataframe(interactive=False, wrap=True, column_widths=[30, 30, 40])
 
 
 
3027
 
 
 
3028
  gr.Markdown("<span style='color:#4e80ee'>修改結果</span>")
3029
  chinese_full_paragraph_save_output_history = gr.Markdown()
3030
 
 
3036
  request_origin,
3037
  chinese_full_paragraph_input,
3038
  chinese_full_paragraph_evaluate_output_text,
3039
+ chinese_full_paragraph_evaluate_output_table,
 
 
3040
  chinese_full_paragraph_refine_input,
3041
+ chinese_full_paragraph_refine_output_text,
3042
  chinese_full_paragraph_refine_output_table,
 
3043
  chinese_full_paragraph_save_output
3044
  ],
3045
  outputs=[
3046
  chinese_full_paragraph_input_history,
3047
  chinese_full_paragraph_evaluate_output_text_history,
3048
+ chinese_full_paragraph_evaluate_output_table_history,
 
 
3049
  chinese_full_paragraph_refine_input_history,
3050
+ chinese_full_paragraph_refine_output_text_history,
3051
  chinese_full_paragraph_refine_output_table_history,
 
3052
  chinese_full_paragraph_save_output_history
3053
  ]
3054
  ).then(
 
3057
  outputs=[chinese_grapragh_practice_history_accordion]
3058
  )
3059
 
 
 
 
 
 
 
 
 
 
3060
  chinese_full_paragraph_evaluate_button.click(
3061
  fn=get_chinese_paragraph_evaluate_content,
3062
  inputs=[model, chinese_full_paragraph_sys_content_input, chinese_full_paragraph_input, user_generate_chinese_full_paragraph_evaluate_prompt],
3063
+ outputs=[chinese_full_paragraph_evaluate_output_text, chinese_full_paragraph_evaluate_output_table]
3064
  ).then(
3065
+ fn=duplicate_element,
3066
  inputs=[chinese_full_paragraph_input],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3067
  outputs=chinese_full_paragraph_refine_input
3068
  )
3069
 
3070
  generate_chinese_full_paragraph_refine_button.click(
3071
+ fn=get_chinese_paragraph_refine_evaluate_content,
3072
+ inputs=[model, chinese_full_paragraph_sys_content_input, chinese_full_paragraph_input, chinese_full_paragraph_refine_input, user_generate_chinese_full_paragraph_refine_evaluate_prompt],
3073
+ outputs=[chinese_full_paragraph_refine_output_text, chinese_full_paragraph_refine_output_table]
 
 
 
 
3074
  )
3075
 
3076
  chinese_full_paragraph_save_button.click(
 
3094
  chinese_full_paragraph_input_history_log = gr.Markdown()
3095
  gr.Markdown("<span style='color:#4e80ee'>段落全文分析</span>")
3096
  chinese_full_paragraph_evaluate_output_text_history_log = gr.Markdown()
3097
+ chinese_full_paragraph_evaluate_output_table_history_log = gr.Dataframe(wrap=True, column_widths=[35, 15, 50], interactive=False)
 
 
 
 
 
3098
 
3099
  gr.Markdown("<span style='color:#4e80ee'>段落改善建議 輸入</span>")
3100
  chinese_full_paragraph_refine_input_history_log = gr.Markdown()
3101
  gr.Markdown("<span style='color:#4e80ee'>段落改善建議</span>")
3102
+ chinese_full_paragraph_refine_output_text_history_log = gr.Markdown()
3103
  chinese_full_paragraph_refine_output_table_history_log = gr.Dataframe(wrap=True, interactive=False, column_widths=[30, 30, 40])
3104
 
 
 
3105
  gr.Markdown("<span style='color:#4e80ee'>修改結果</span>")
3106
  chinese_full_paragraph_save_output_history_log = gr.Markdown()
3107
 
 
3117
  outputs=[
3118
  chinese_full_paragraph_input_history_log,
3119
  chinese_full_paragraph_evaluate_output_text_history_log,
3120
+ chinese_full_paragraph_evaluate_output_table_history_log,
 
 
3121
  chinese_full_paragraph_refine_input_history_log,
3122
+ chinese_full_paragraph_refine_output_text_history_log,
3123
  chinese_full_paragraph_refine_output_table_history_log,
 
3124
  chinese_full_paragraph_save_output_history_log
3125
  ]
3126
  )