youngtsai commited on
Commit
01fac1e
·
1 Parent(s): 613c314

user_generate_topic_sentence_input_feedback_prompt = gr.Textbox(label="Feedback Prompt", value=default_generate_topic_sentence_input_feedback_prompt, visible=False)

Browse files
Files changed (1) hide show
  1. app.py +72 -1
app.py CHANGED
@@ -231,6 +231,37 @@ def generate_topic_sentences(model, max_tokens, sys_content, scenario, eng_level
231
 
232
  return gr_update_json, gr_update_radio
233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
  def update_topic_sentence_input(topic_sentences_json, selected_topic_sentence):
235
  topic_sentence_input = ""
236
  for ts in topic_sentences_json:
@@ -881,7 +912,47 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue, secondary
881
  gr.Markdown("""舉例,情境是 `School & Learning`,段落主題是 `Time Management`,那麼 `Balancing school work and leisure time is a crucial aspect of effective time management` 就是合適的主題句,因為它清楚點出該段落將説明有效運用時間來讓課業及娛樂取得平衡。""")
882
  with gr.Row():
883
  with gr.Column():
884
- topic_sentence_input = gr.Textbox(label="根據主題、要點/關鍵字來寫主題句")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
885
  with gr.Column():
886
  generate_topic_sentences_button = gr.Button("✨ JUTOR 產出三個主題句,選出一個最合適的", variant="primary")
887
  topic_sentence_output_json = gr.JSON(label="AI 產出主題句", visible=False)
 
231
 
232
  return gr_update_json, gr_update_radio
233
 
234
+ def generate_topic_sentence_feedback(model, max_tokens, sys_content, scenario, eng_level, topic, points, topic_sentence, user_generate_topic_sentence_feedback_prompt):
235
+ """
236
+ 根据系统提示和用户输入的情境、主题、要点、主题句,调用OpenAI API生成相关的主题句反饋。
237
+ """
238
+ user_content = f"""
239
+ scenario is: {scenario}
240
+ english level is: {eng_level}
241
+ topic is: {topic}
242
+ points is: {points}
243
+ ---
244
+ my written topic sentence is: {topic_sentence}
245
+ ---
246
+ {user_generate_topic_sentence_feedback_prompt}
247
+ """
248
+ messages = [
249
+ {"role": "system", "content": sys_content},
250
+ {"role": "user", "content": user_content}
251
+ ]
252
+
253
+ request_payload = {
254
+ "model": model,
255
+ "messages": messages,
256
+ "max_tokens": max_tokens,
257
+ }
258
+
259
+ response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
260
+ content = response.choices[0].message.content.strip()
261
+ gr_update = gr.update(value=content, visible=True)
262
+
263
+ return gr_update
264
+
265
  def update_topic_sentence_input(topic_sentences_json, selected_topic_sentence):
266
  topic_sentence_input = ""
267
  for ts in topic_sentences_json:
 
912
  gr.Markdown("""舉例,情境是 `School & Learning`,段落主題是 `Time Management`,那麼 `Balancing school work and leisure time is a crucial aspect of effective time management` 就是合適的主題句,因為它清楚點出該段落將説明有效運用時間來讓課業及娛樂取得平衡。""")
913
  with gr.Row():
914
  with gr.Column():
915
+ with gr.Row():
916
+ topic_sentence_input = gr.Textbox(label="根據主題、要點/關鍵字來寫主題句")
917
+ with gr.Row():
918
+ default_generate_topic_sentence_input_feedback_prompt = """
919
+ Rules:
920
+ - 主題句(Topic Sentence)通常位於段落的開頭,幫助讀者迅速理解段落的內容。是段落中最重要的句子,介紹主題(topic)並含括段落的所有要點/關鍵字(points)。
921
+ - 例如:"Learning a second language improves communication skills and helps you understand other cultures better." "The Benefits of Learning a second language"是主題, "improving communication skills" 和 "understanding other cultures" 則是兩個要點/關鍵字。
922
+ - 書寫段落時,必須確保每個句子都支持和闡述主題句,避免引入無關或偏離主題的討論,否則就會影響段落的架構及內容的一致性及連貫性。
923
+
924
+ Please check my written topic sentence, it should introduces the subject for the given topic and points and follow the rules.
925
+ using Zh-TW to explain the reason.
926
+ please don't give any correct topic sentence as an example in the feedback.
927
+
928
+ EXAMPLE:
929
+ - 主題: "My Favorite Animal"
930
+ - 要點/關鍵字: "Dogs are friendly,"
931
+ - 你寫的主題句: {{xxxxxx}}
932
+
933
+ - 分析結果:✅ 主題句合適/ ❌ 主題句並不合適
934
+ - 解釋: {{中文解釋}}
935
+ """
936
+ user_generate_topic_sentence_input_feedback_prompt = gr.Textbox(label="Feedback Prompt", value=default_generate_topic_sentence_input_feedback_prompt, visible=False)
937
+ topic_sentence_input_feedback_button = gr.Button("✨ 提交主題句,獲得反饋", variant="primary")
938
+ with gr.Row():
939
+ topic_sentence_input_feedback_text = gr.Textbox(label="Feedback")
940
+
941
+ topic_sentence_input_feedback_button.click(
942
+ fn=generate_topic_sentence_feedback,
943
+ inputs=[
944
+ model,
945
+ max_tokens,
946
+ sys_content_input,
947
+ scenario_input,
948
+ eng_level_input,
949
+ topic_input,
950
+ points_input,
951
+ topic_sentence_input,
952
+ user_generate_topic_sentence_input_feedback_prompt
953
+ ],
954
+ outputs=[topic_sentence_input_feedback_text]
955
+ )
956
  with gr.Column():
957
  generate_topic_sentences_button = gr.Button("✨ JUTOR 產出三個主題句,選出一個最合適的", variant="primary")
958
  topic_sentence_output_json = gr.JSON(label="AI 產出主題句", visible=False)