Spaces:
Sleeping
Sleeping
def update_topic_sentence_input(topic_sentences_radio, topic_sentence_output):
Browse files
app.py
CHANGED
@@ -45,9 +45,8 @@ def generate_topics(model, max_tokens, sys_content, scenario, eng_level, user_ge
|
|
45 |
topics = json.loads(content)["topics"]
|
46 |
topics_text = json.dumps(topics)
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
return topics_text, is_visible_false
|
51 |
|
52 |
def update_topic_input(topic):
|
53 |
return topic
|
@@ -74,9 +73,16 @@ def generate_points(model, max_tokens, sys_content, scenario, eng_level, topic,
|
|
74 |
}
|
75 |
|
76 |
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
|
77 |
-
content = response.choices[0].message.content
|
78 |
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
def generate_topic_sentences(model, max_tokens, sys_content, scenario, eng_level, topic, points, user_generate_topic_sentences_prompt):
|
82 |
"""
|
@@ -95,7 +101,6 @@ def generate_topic_sentences(model, max_tokens, sys_content, scenario, eng_level
|
|
95 |
]
|
96 |
response_format = { "type": "json_object" }
|
97 |
|
98 |
-
|
99 |
request_payload = {
|
100 |
"model": model,
|
101 |
"messages": messages,
|
@@ -104,33 +109,32 @@ def generate_topic_sentences(model, max_tokens, sys_content, scenario, eng_level
|
|
104 |
}
|
105 |
|
106 |
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
-
return
|
111 |
-
|
112 |
-
def parse_and_display_topic_sentences(json_data):
|
113 |
-
"""
|
114 |
-
解析JSON格式的主题句数据,并转换成易于阅读的格式。
|
115 |
-
"""
|
116 |
-
# 将JSON字符串解析成Python字典
|
117 |
-
data = json.loads(json_data)
|
118 |
-
|
119 |
-
# 初始化一个空字符串用于存放最终的格式化文本
|
120 |
-
formatted_text = ""
|
121 |
-
|
122 |
-
# 遍历每个主题句及其评价
|
123 |
-
for key, value in data.items():
|
124 |
-
topic_sentence = value[0]['topic-sentence']
|
125 |
-
appropriate = "適當" if value[0]['appropriate'] == "Y" else "不適當"
|
126 |
-
reason = value[0]['reason']
|
127 |
-
|
128 |
-
# 将每个主题句的信息添加到格式化文本中
|
129 |
-
formatted_text += f"主题句 {int(key)+1}: {topic_sentence}\n"
|
130 |
-
formatted_text += f"是否適當: {appropriate}\n"
|
131 |
-
formatted_text += f"原因: {reason}\n\n"
|
132 |
-
|
133 |
-
return formatted_text
|
134 |
|
135 |
def generate_supporting_sentences(model, max_tokens, sys_content, scenario, eng_level, topic, points, topic_sentence, user_generate_supporting_sentences_prompt):
|
136 |
"""
|
@@ -305,9 +309,9 @@ def paragraph_save_and_tts(paragraph_text):
|
|
305 |
return paragraph_text, None
|
306 |
|
307 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue, secondary_hue=gr.themes.colors.orange)) as demo:
|
|
|
308 |
with gr.Row():
|
309 |
with gr.Column():
|
310 |
-
# basic inputs
|
311 |
model = gr.Radio(["gpt-4o", "gpt-3.5-turbo"], label="Model", value="gpt-4o", visible=False)
|
312 |
max_tokens = gr.Slider(minimum=50, maximum=4000, value=4000, label="Max Tokens", visible=False)
|
313 |
sys_content_input = gr.Textbox(label="System Prompt", value="You are an English teacher who is practicing with me to improve my English writing skill.", visible=False)
|
@@ -338,8 +342,10 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue, secondary
|
|
338 |
"My Favorite Cartoon/Anime"
|
339 |
]
|
340 |
scenario_input = gr.Dropdown(label="先選擇一個大範圍的情境:", choices=scenario_values, value="Health")
|
341 |
-
|
342 |
-
|
|
|
|
|
343 |
with gr.Row():
|
344 |
gr.Markdown("# Step 2. 確定段落主題")
|
345 |
with gr.Row():
|
@@ -379,39 +385,116 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue, secondary
|
|
379 |
outputs=[topic_input]
|
380 |
)
|
381 |
return topic_radio
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
"
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
415 |
|
416 |
gr.Markdown("## 5. Generate Supporting Sentence 支持句")
|
417 |
default_generate_supporting_sentences_prompt = """
|
@@ -568,7 +651,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue, secondary
|
|
568 |
eng_level_input,
|
569 |
user_generate_topics_prompt
|
570 |
],
|
571 |
-
outputs=[topic_output
|
572 |
)
|
573 |
|
574 |
generate_points_button.click(
|
|
|
45 |
topics = json.loads(content)["topics"]
|
46 |
topics_text = json.dumps(topics)
|
47 |
|
48 |
+
gr_update = gr.update(visible=False, value=topics_text)
|
49 |
+
return gr_update
|
|
|
50 |
|
51 |
def update_topic_input(topic):
|
52 |
return topic
|
|
|
73 |
}
|
74 |
|
75 |
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
|
76 |
+
content = response.choices[0].message.content
|
77 |
|
78 |
+
points = json.loads(content)["points"]
|
79 |
+
points_text = json.dumps(points)
|
80 |
+
|
81 |
+
gr_update = gr.update(visible=False, value=points_text)
|
82 |
+
return gr_update
|
83 |
+
|
84 |
+
def update_points_input(points):
|
85 |
+
return points
|
86 |
|
87 |
def generate_topic_sentences(model, max_tokens, sys_content, scenario, eng_level, topic, points, user_generate_topic_sentences_prompt):
|
88 |
"""
|
|
|
101 |
]
|
102 |
response_format = { "type": "json_object" }
|
103 |
|
|
|
104 |
request_payload = {
|
105 |
"model": model,
|
106 |
"messages": messages,
|
|
|
109 |
}
|
110 |
|
111 |
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
|
112 |
+
response_content = json.loads(response.choices[0].message.content)
|
113 |
+
json_content = response_content["results"]
|
114 |
+
topic_sentences_text = json.dumps(json_content, ensure_ascii=False)
|
115 |
+
|
116 |
+
gr_update = gr.update(visible=False, value=topic_sentences_text)
|
117 |
+
return gr_update
|
118 |
+
|
119 |
+
def update_topic_sentence_input(topic_sentences_radio, topic_sentence_output):
|
120 |
+
selected_topic_sentence = topic_sentences_radio
|
121 |
+
topic_sentence_output = json.loads(topic_sentence_output)
|
122 |
+
for ts in topic_sentence_output:
|
123 |
+
if ts["topic-sentence"] == selected_topic_sentence:
|
124 |
+
appropriate = "O 適合" if ts["appropriate"] == "Y" else "X 不適合"
|
125 |
+
border_color = "green" if ts["appropriate"] == "Y" else "red"
|
126 |
+
background_color = "#e0ffe0" if ts["appropriate"] == "Y" else "#ffe0e0"
|
127 |
+
|
128 |
+
suggestion_html = f"""
|
129 |
+
<div style="border: 2px solid {border_color}; background-color: {background_color}; padding: 10px; border-radius: 5px;">
|
130 |
+
<p>你選了主題句:{selected_topic_sentence}</p>
|
131 |
+
<p>是否適當:{appropriate}</p>
|
132 |
+
<p>原因:{ts['reason']}</p>
|
133 |
+
</div>
|
134 |
+
"""
|
135 |
+
break
|
136 |
|
137 |
+
return selected_topic_sentence, suggestion_html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
def generate_supporting_sentences(model, max_tokens, sys_content, scenario, eng_level, topic, points, topic_sentence, user_generate_supporting_sentences_prompt):
|
140 |
"""
|
|
|
309 |
return paragraph_text, None
|
310 |
|
311 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue, secondary_hue=gr.themes.colors.orange)) as demo:
|
312 |
+
# basic inputs 主題與情境
|
313 |
with gr.Row():
|
314 |
with gr.Column():
|
|
|
315 |
model = gr.Radio(["gpt-4o", "gpt-3.5-turbo"], label="Model", value="gpt-4o", visible=False)
|
316 |
max_tokens = gr.Slider(minimum=50, maximum=4000, value=4000, label="Max Tokens", visible=False)
|
317 |
sys_content_input = gr.Textbox(label="System Prompt", value="You are an English teacher who is practicing with me to improve my English writing skill.", visible=False)
|
|
|
342 |
"My Favorite Cartoon/Anime"
|
343 |
]
|
344 |
scenario_input = gr.Dropdown(label="先選擇一個大範圍的情境:", choices=scenario_values, value="Health")
|
345 |
+
|
346 |
+
# Step 2. 確定段落主題
|
347 |
+
with gr.Row():
|
348 |
+
with gr.Column():
|
349 |
with gr.Row():
|
350 |
gr.Markdown("# Step 2. 確定段落主題")
|
351 |
with gr.Row():
|
|
|
385 |
outputs=[topic_input]
|
386 |
)
|
387 |
return topic_radio
|
388 |
+
# Step 3. 寫出段落要點
|
389 |
+
with gr.Row():
|
390 |
+
with gr.Column():
|
391 |
+
with gr.Row() as points_params:
|
392 |
+
default_generate_points_prompt = """
|
393 |
+
Please provide main points to develop in a paragraph about topic in the context of scenario,
|
394 |
+
use simple English language and make sure the vocabulary you use is at eng_level.
|
395 |
+
No more explanation either no developing these points into a simple paragraph.
|
396 |
+
Output use JSON format
|
397 |
+
|
398 |
+
EXAMPLE:
|
399 |
+
"points":["point1", "point2", "point3"]
|
400 |
+
"""
|
401 |
+
user_generate_points_prompt = gr.Textbox(label="Points Prompt", value=default_generate_points_prompt, visible=False)
|
402 |
+
with gr.Row() as points_html:
|
403 |
+
gr.Markdown("# Step 3. 寫出段落要點")
|
404 |
+
with gr.Row():
|
405 |
+
gr.Markdown("## 根據情境、主題,可以視主題不同,試著寫出 1-3 個要點。段落要點務必選擇比較相關的,才好寫入一個段落。不相關的要點會讓段落缺乏連貫一致性。")
|
406 |
+
with gr.Row():
|
407 |
+
gr.Markdown("### `基礎級使用者` 先從 1 個要點開始練習,比較好掌握;等熟悉之後在 `實力級`,就可選擇 2-3 個要點來發揮。")
|
408 |
+
with gr.Row():
|
409 |
+
with gr.Column():
|
410 |
+
points_input = gr.Textbox(label="#1 要點/關鍵字")
|
411 |
+
with gr.Column():
|
412 |
+
generate_points_button = gr.Button("找尋靈感?使用 🪄 JUTOR 產生要點/關鍵字", variant="primary")
|
413 |
+
points_output = gr.Textbox(label="AI Generated Points 要點", visible=True, value=[])
|
414 |
+
|
415 |
+
@gr.render(inputs=points_output)
|
416 |
+
def render_points(points):
|
417 |
+
points_list = json.loads(points)
|
418 |
+
points_radio = gr.Radio(points_list, label="Points", elem_id="point_button")
|
419 |
+
points_radio.select(
|
420 |
+
fn=update_points_input,
|
421 |
+
inputs=[points_radio],
|
422 |
+
outputs=[points_input]
|
423 |
+
)
|
424 |
+
return points_radio
|
425 |
+
# Step 4. 選定主題句
|
426 |
+
with gr.Row():
|
427 |
+
with gr.Column():
|
428 |
+
with gr.Row() as topic_sentences_params:
|
429 |
+
default_generate_topic_sentences_prompt = """
|
430 |
+
Please provide one appropriate topic sentence that aptly introduces the subject for the given scenario and topic.
|
431 |
+
Additionally, provide two topic sentences that, while related to the topic,
|
432 |
+
would be considered inappropriate or less effective for the specified context.
|
433 |
+
Those sentences must include the three main points:".
|
434 |
+
Use English language and each sentence should not be too long.
|
435 |
+
For each sentence, explain the reason in Traditional Chinese, Taiwan, 繁體中文 zh-TW.
|
436 |
+
Make sure the vocabulary you use is at level.
|
437 |
+
|
438 |
+
Output use JSON format
|
439 |
+
|
440 |
+
EXAMPLE:
|
441 |
+
"results":
|
442 |
+
[
|
443 |
+
{{ "topic-sentence": "#","appropriate": "Y/N", "reason": "#中文解釋" }} ,
|
444 |
+
{{ "topic-sentence": "#","appropriate": "Y/N", "reason": "#中文解釋" }},
|
445 |
+
{{ "topic-sentence": "#","appropriate": "Y/N", "reason": "#中文解釋" }}
|
446 |
+
]
|
447 |
+
"""
|
448 |
+
user_generate_topic_sentences_prompt = gr.Textbox(label="Topic Sentences Prompt", value=default_generate_topic_sentences_prompt, visible=False)
|
449 |
+
|
450 |
+
with gr.Row() as topic_sentences_html:
|
451 |
+
gr.Markdown("# Step 4. 選定主題句")
|
452 |
+
with gr.Row():
|
453 |
+
with gr.Column():
|
454 |
+
gr.Markdown("## 主題句(Topic Sentence)是一個段落中最重要的句子,它介紹主題並含括該段落的所有要點,引起讀者的興趣。就像藍圖一樣,指出客廳、廚房、臥室等位置。")
|
455 |
+
gr.Markdown("## 主題句通常位於段落的開頭,幫助讀者迅速理解段落的內容。如果沒有主題句,段落的架構及內容的一致性及連貫性就會受影響。")
|
456 |
+
gr.Markdown("## 主題句的範圍,應能適當含括你剛才決定的各個要點,範圍不要太大,以致無法在一個段落清楚説明,也不能太小,無法含括段落的所有要點。")
|
457 |
+
with gr.Column():
|
458 |
+
with gr.Accordion("參考指引:合適的主題句?", open=False):
|
459 |
+
gr.Markdown("""舉例,情境是 `School & Learning`,段落主題是 `Time Management`,那麼 `Balancing school work and leisure time is a crucial aspect of effective time management` 就是合適的主題句,因為它清楚點出該段落將説明有效運用時間來讓課業及娛樂取得平衡。""")
|
460 |
+
with gr.Row():
|
461 |
+
with gr.Column():
|
462 |
+
topic_sentence_input = gr.Textbox(label="Topic Sentences")
|
463 |
+
with gr.Column():
|
464 |
+
generate_topic_sentences_button = gr.Button("生成並在下面 3 個 JUTOR 產生的主題句中,選出一個最合適的", variant="primary")
|
465 |
+
topic_sentence_output = gr.Textbox(label="AI Generated Topic Sentences 主題句")
|
466 |
+
|
467 |
+
@gr.render(inputs=topic_sentence_output)
|
468 |
+
def render_topic_sentences(topic_sentences):
|
469 |
+
# Parsing the JSON string to a list
|
470 |
+
topic_sentences_list = json.loads(topic_sentences)
|
471 |
+
|
472 |
+
# Extracting only the topic sentences for the radio button options
|
473 |
+
radio_options = [ts["topic-sentence"] for ts in topic_sentences_list]
|
474 |
+
|
475 |
+
# Creating the radio button element
|
476 |
+
topic_sentences_radio = gr.Radio(radio_options, label="Topic Sentences", elem_id="topic_sentence_button")
|
477 |
+
topic_sentences_suggestions = gr.HTML() # Setting up the action when a radio button is selected
|
478 |
+
topic_sentences_radio.select(
|
479 |
+
fn=update_topic_sentence_input,
|
480 |
+
inputs=[topic_sentences_radio, topic_sentence_output],
|
481 |
+
outputs= [topic_sentence_input, topic_sentences_suggestions]
|
482 |
+
)
|
483 |
+
|
484 |
+
return topic_sentences_radio
|
485 |
+
|
486 |
+
|
487 |
+
|
488 |
+
|
489 |
+
|
490 |
+
|
491 |
+
|
492 |
+
|
493 |
+
|
494 |
+
|
495 |
+
|
496 |
+
|
497 |
+
|
498 |
|
499 |
gr.Markdown("## 5. Generate Supporting Sentence 支持句")
|
500 |
default_generate_supporting_sentences_prompt = """
|
|
|
651 |
eng_level_input,
|
652 |
user_generate_topics_prompt
|
653 |
],
|
654 |
+
outputs=[topic_output]
|
655 |
)
|
656 |
|
657 |
generate_points_button.click(
|