youngtsai commited on
Commit
c9e97ad
·
1 Parent(s): 877f289

content = parse_and_display_topic_sentences(json_content)

Browse files
Files changed (1) hide show
  1. app.py +26 -2
app.py CHANGED
@@ -84,10 +84,34 @@ def generate_topic_sentences(model, max_tokens, sys_content, scenario, eng_level
84
  }
85
 
86
  response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
87
- content = response.choices[0].message.content
 
88
 
89
  return content
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  with gr.Blocks() as demo:
92
  with gr.Row():
93
  with gr.Column():
@@ -127,8 +151,8 @@ with gr.Blocks() as demo:
127
  Those sentences must include the three main points:".
128
  Use English language and each sentence should not be too long.
129
  For each sentence, explain the reason in Traditional Chinese, Taiwan, 繁體中文 zh-TW.
130
-
131
  Make sure the vocabulary you use is at level.
 
132
  Only return the result in JSON format starting as:
133
  {{
134
  "0": [ {{ "topic-sentence": "#","appropriate": "Y/N", "reason": "#中文解釋" }} ],
 
84
  }
85
 
86
  response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
87
+ json_content = response.choices[0].message.content
88
+ content = parse_and_display_topic_sentences(json_content)
89
 
90
  return content
91
 
92
+ def parse_and_display_topic_sentences(json_data):
93
+ """
94
+ 解析JSON格式的主题句数据,并转换成易于阅读的格式。
95
+ """
96
+ # 将JSON字符串解析成Python字典
97
+ data = json.loads(json_data)
98
+
99
+ # 初始化一个空字符串用于存放最终的格式化文本
100
+ formatted_text = ""
101
+
102
+ # 遍历每个主题句及其评价
103
+ for key, value in data.items():
104
+ topic_sentence = value[0]['topic-sentence']
105
+ appropriate = "適當" if value[0]['appropriate'] == "Y" else "不適當"
106
+ reason = value[0]['reason']
107
+
108
+ # 将每个主题句的信息添加到格式化文本中
109
+ formatted_text += f"主题句 {int(key)+1}: {topic_sentence}\n"
110
+ formatted_text += f"是否適當: {appropriate}\n"
111
+ formatted_text += f"原因: {reason}\n\n"
112
+
113
+ return formatted_text
114
+
115
  with gr.Blocks() as demo:
116
  with gr.Row():
117
  with gr.Column():
 
151
  Those sentences must include the three main points:".
152
  Use English language and each sentence should not be too long.
153
  For each sentence, explain the reason in Traditional Chinese, Taiwan, 繁體中文 zh-TW.
 
154
  Make sure the vocabulary you use is at level.
155
+
156
  Only return the result in JSON format starting as:
157
  {{
158
  "0": [ {{ "topic-sentence": "#","appropriate": "Y/N", "reason": "#中文解釋" }} ],