youngtsai commited on
Commit
51534fc
·
1 Parent(s): ac81183

with gr.Accordion("📚 中文段落練習歷程回顧", open=False) as chinese_grapragh_practice_logs_accordion:

Browse files
Files changed (1) hide show
  1. app.py +185 -1
app.py CHANGED
@@ -897,6 +897,79 @@ def update_exam_contents(selected_title):
897
  if exam["title"] == selected_title:
898
  return exam["title"], exam["question"], exam["hint"], exam["image_url"]
899
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
900
 
901
  # 小工具
902
  def show_elements():
@@ -2545,6 +2618,7 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
2545
  with gr.Column():
2546
  with gr.Row() as page_title_chinese:
2547
  gr.Markdown("# 🔮 JUTOR 國文段落寫作練習")
 
2548
  # =====中文作文工具=====
2549
  with gr.Tab("中文作文工具") as chinese_idea_tab:
2550
  # 輸入題目、輸出靈感
@@ -2674,7 +2748,63 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
2674
  with gr.Row():
2675
  chinese_full_paragraph_save_output = gr.TextArea(label="最後結果")
2676
  chinese_full_audio_output = gr.Audio(label="音檔", type="filepath")
2677
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2678
  chinese_full_paragraph_evaluate_button.click(
2679
  fn=generate_chinese_evaluation_table,
2680
  inputs=[model, chinese_full_paragraph_sys_content_input, user_generate_chinese_full_paragraph_evaluate_prompt, chinese_full_paragraph_input],
@@ -2715,6 +2845,60 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
2715
  outputs=[chinese_full_paragraph_save_output, chinese_full_audio_output]
2716
  )
2717
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2718
 
2719
  demo.load(
2720
  init_params,
 
897
  if exam["title"] == selected_title:
898
  return exam["title"], exam["question"], exam["hint"], exam["image_url"]
899
 
900
+ def generate_chinese_paragraph_practice_history(
901
+ user_data,
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,
908
+ chinese_full_paragraph_refine_input,
909
+ chinese_full_paragraph_refine_output_table,
910
+ chinese_full_paragraph_refine_output,
911
+ chinese_full_paragraph_save_output
912
+ ):
913
+ if user_data:
914
+ encoded_user_id_url = urllib.parse.quote(user_data, safe='')
915
+ log_type_name = "jutor_write_chinese_full_paragraph_evaluation"
916
+ file_name = f"{encoded_user_id_url}/{log_type_name}/{session_timestamp}.json"
917
+ content = {
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'),
924
+ "full_paragraph_refine_input": chinese_full_paragraph_refine_input,
925
+ "full_paragraph_refine_output_table": chinese_full_paragraph_refine_output_table.to_dict(orient='records'),
926
+ "full_paragraph_refine_output": chinese_full_paragraph_refine_output,
927
+ "full_paragraph_save_output": chinese_full_paragraph_save_output
928
+ }
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, \
935
+ chinese_full_paragraph_refine_input, \
936
+ chinese_full_paragraph_refine_output_table, \
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"])
951
+ chinese_full_paragraph_refine_input_history = content_json["full_paragraph_refine_input"]
952
+ chinese_full_paragraph_refine_output_table_history = pd.DataFrame(content_json["full_paragraph_refine_output_table"])
953
+ chinese_full_paragraph_refine_output_history = content_json["full_paragraph_refine_output"]
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()
960
+ chinese_full_paragraph_refine_input_history = ""
961
+ chinese_full_paragraph_refine_output_table_history = pd.DataFrame()
962
+ chinese_full_paragraph_refine_output_history = ""
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, \
969
+ chinese_full_paragraph_refine_input_history, \
970
+ chinese_full_paragraph_refine_output_table_history, \
971
+ chinese_full_paragraph_refine_output_history, \
972
+ chinese_full_paragraph_save_output_history
973
 
974
  # 小工具
975
  def show_elements():
 
2618
  with gr.Column():
2619
  with gr.Row() as page_title_chinese:
2620
  gr.Markdown("# 🔮 JUTOR 國文段落寫作練習")
2621
+
2622
  # =====中文作文工具=====
2623
  with gr.Tab("中文作文工具") as chinese_idea_tab:
2624
  # 輸入題目、輸出靈感
 
2748
  with gr.Row():
2749
  chinese_full_paragraph_save_output = gr.TextArea(label="最後結果")
2750
  chinese_full_audio_output = gr.Audio(label="音檔", type="filepath")
2751
+
2752
+ # 建立歷程回顧
2753
+ with gr.Row():
2754
+ chinese_paragraph_save_history_button = gr.Button("建立歷程回顧", variant="primary")
2755
+ with gr.Row():
2756
+ with gr.Accordion("歷程回顧", open=False) as chinese_grapragh_practice_history_accordion:
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>")
2763
+ chinese_full_paragraph_correct_grammatical_spelling_errors_input_history = gr.Markdown()
2764
+ gr.Markdown("<span style='color:#4e80ee'>修訂文法與拼字錯誤</span>")
2765
+ chinese_full_paragraph_correct_grammatical_spelling_errors_output_table_history = gr.Dataframe(interactive=False, wrap=True, column_widths=[30, 30, 40])
2766
+
2767
+ gr.Markdown("<span style='color:#4e80ee'>段落改善建議 輸入</span>")
2768
+ chinese_full_paragraph_refine_input_history = gr.Markdown()
2769
+ gr.Markdown("<span style='color:#4e80ee'>段落改善建議</span>")
2770
+ chinese_full_paragraph_refine_output_table_history = gr.Dataframe(wrap=True, interactive=False, column_widths=[30, 30, 40])
2771
+
2772
+ gr.Markdown("<span style='color:#4e80ee'>修改建議</span>")
2773
+ chinese_full_paragraph_refine_output_history = gr.Markdown()
2774
+ gr.Markdown("<span style='color:#4e80ee'>修改結果</span>")
2775
+ chinese_full_paragraph_save_output_history = gr.Markdown()
2776
+
2777
+ chinese_paragraph_save_history_button.click(
2778
+ fn=generate_chinese_paragraph_practice_history,
2779
+ inputs=[
2780
+ user_data,
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,
2787
+ chinese_full_paragraph_refine_input,
2788
+ chinese_full_paragraph_refine_output_table,
2789
+ chinese_full_paragraph_refine_output,
2790
+ chinese_full_paragraph_save_output
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,
2797
+ chinese_full_paragraph_refine_input_history,
2798
+ chinese_full_paragraph_refine_output_table_history,
2799
+ chinese_full_paragraph_refine_output_history,
2800
+ chinese_full_paragraph_save_output_history
2801
+ ]
2802
+ ).then(
2803
+ fn=update_history_accordion,
2804
+ inputs=[],
2805
+ outputs=[chinese_grapragh_practice_history_accordion]
2806
+ )
2807
+
2808
  chinese_full_paragraph_evaluate_button.click(
2809
  fn=generate_chinese_evaluation_table,
2810
  inputs=[model, chinese_full_paragraph_sys_content_input, user_generate_chinese_full_paragraph_evaluate_prompt, chinese_full_paragraph_input],
 
2845
  outputs=[chinese_full_paragraph_save_output, chinese_full_audio_output]
2846
  )
2847
 
2848
+ # === 歷程 session 列表
2849
+ with gr.Tab("歷程回顧"):
2850
+ with gr.Accordion("📚 中文段落練習歷程回顧", open=False) as chinese_grapragh_practice_logs_accordion:
2851
+ # 變數名稱就是加上 logs
2852
+ with gr.Row():
2853
+ with gr.Column(scale=1):
2854
+ # 取得中文段落練習 log from GCS
2855
+ chinese_paragraph_practice_logs_type = gr.State("jutor_write_chinese_full_paragraph_evaluation")
2856
+ get_chinese_paragraph_practice_logs_button = gr.Button("👉 取得中文段落寫作練習歷程", variant="primary")
2857
+ chinese_paragraph_practice_logs_session_list = gr.Radio(label="歷程時間列表")
2858
+ with gr.Column(scale=3, variant="compact"):
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>")
2865
+ chinese_full_paragraph_correct_grammatical_spelling_errors_input_history_log = gr.Markdown()
2866
+ gr.Markdown("<span style='color:#4e80ee'>修訂文法與拼字錯誤</span>")
2867
+ chinese_full_paragraph_correct_grammatical_spelling_errors_output_table_history_log = gr.Dataframe(interactive=False, wrap=True, column_widths=[30, 30, 40])
2868
+
2869
+ gr.Markdown("<span style='color:#4e80ee'>段落改善建議 輸入</span>")
2870
+ chinese_full_paragraph_refine_input_history_log = gr.Markdown()
2871
+ gr.Markdown("<span style='color:#4e80ee'>段落改善建議</span>")
2872
+ chinese_full_paragraph_refine_output_table_history_log = gr.Dataframe(wrap=True, interactive=False, column_widths=[30, 30, 40])
2873
+
2874
+ gr.Markdown("<span style='color:#4e80ee'>修改建議</span>")
2875
+ chinese_full_paragraph_refine_output_history_log = gr.Markdown()
2876
+ gr.Markdown("<span style='color:#4e80ee'>修改結果</span>")
2877
+ chinese_full_paragraph_save_output_history_log = gr.Markdown()
2878
+
2879
+ get_chinese_paragraph_practice_logs_button.click(
2880
+ fn=get_logs_sessions,
2881
+ inputs=[user_data, chinese_paragraph_practice_logs_type],
2882
+ outputs=[chinese_paragraph_practice_logs_session_list]
2883
+ )
2884
+
2885
+ chinese_paragraph_practice_logs_session_list.select(
2886
+ fn=get_chinese_paragraph_practice_log_session_content,
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,
2893
+ chinese_full_paragraph_refine_input_history_log,
2894
+ chinese_full_paragraph_refine_output_table_history_log,
2895
+ chinese_full_paragraph_refine_output_history_log,
2896
+ chinese_full_paragraph_save_output_history_log
2897
+ ]
2898
+ )
2899
+
2900
+
2901
+
2902
 
2903
  demo.load(
2904
  init_params,