Spaces:
Sleeping
Sleeping
def highlight_diff_texts(highlight_list, text):
Browse files
app.py
CHANGED
@@ -3,9 +3,12 @@ import gradio as gr
|
|
3 |
from openai import OpenAI
|
4 |
|
5 |
from difflib import Differ
|
|
|
6 |
|
7 |
import json
|
8 |
import tempfile
|
|
|
|
|
9 |
|
10 |
is_env_local = os.getenv("IS_ENV_LOCAL", "false") == "true"
|
11 |
print(f"is_env_local: {is_env_local}")
|
@@ -114,6 +117,7 @@ def generate_topic_sentences(model, max_tokens, sys_content, scenario, eng_level
|
|
114 |
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
|
115 |
response_content = json.loads(response.choices[0].message.content)
|
116 |
json_content = response_content["results"]
|
|
|
117 |
topic_sentences_text = json.dumps(json_content, ensure_ascii=False)
|
118 |
|
119 |
gr_update = gr.update(visible=False, value=topic_sentences_text)
|
@@ -215,7 +219,7 @@ def generate_paragraph(topic_sentence, supporting_sentences, conclusion_sentence
|
|
215 |
paragraph = f"{topic_sentence} {supporting_sentences} {conclusion_sentence}"
|
216 |
return paragraph
|
217 |
|
218 |
-
def generate_paragraph_evaluate(paragraph, user_generate_paragraph_evaluate_prompt):
|
219 |
"""
|
220 |
根据用户输入的段落,调用OpenAI API生成相关的段落分析。
|
221 |
"""
|
@@ -224,7 +228,7 @@ def generate_paragraph_evaluate(paragraph, user_generate_paragraph_evaluate_prom
|
|
224 |
{user_generate_paragraph_evaluate_prompt}
|
225 |
"""
|
226 |
messages = [
|
227 |
-
{"role": "system", "content":
|
228 |
{"role": "user", "content": user_content}
|
229 |
]
|
230 |
|
@@ -257,7 +261,7 @@ def generate_paragraph_evaluate(paragraph, user_generate_paragraph_evaluate_prom
|
|
257 |
return gr_update
|
258 |
|
259 |
|
260 |
-
def generate_correct_grammatical_spelling_errors(eng_level, paragraph, user_correct_grammatical_spelling_errors_prompt):
|
261 |
"""
|
262 |
根据用户输入的段落,调用OpenAI API生成相关的文法和拼字错误修正。
|
263 |
"""
|
@@ -267,7 +271,7 @@ def generate_correct_grammatical_spelling_errors(eng_level, paragraph, user_corr
|
|
267 |
{user_correct_grammatical_spelling_errors_prompt}
|
268 |
"""
|
269 |
messages = [
|
270 |
-
{"role": "system", "content":
|
271 |
{"role": "user", "content": user_content}
|
272 |
]
|
273 |
response_format = { "type": "json_object" }
|
@@ -287,48 +291,40 @@ def generate_correct_grammatical_spelling_errors(eng_level, paragraph, user_corr
|
|
287 |
[item['original'], item['correction'], item['explanation']]
|
288 |
for item in data['Corrections and Explanations']
|
289 |
]
|
290 |
-
headers = ["原文", "
|
291 |
|
292 |
corrections_list_gr_update = gr.update(value=corrections_list, headers=headers, wrap=True)
|
293 |
reverse_paragraph_gr_update = gr.update(value=data["Revised Paragraph"])
|
294 |
|
295 |
return corrections_list_gr_update, reverse_paragraph_gr_update
|
296 |
|
297 |
-
def
|
298 |
-
|
299 |
-
|
300 |
-
revised text is {text2}
|
301 |
-
please check the differences between the original text and the revised text.
|
302 |
-
output a HTML string only
|
303 |
-
highlight the differences with yellow background color
|
304 |
|
305 |
-
|
306 |
-
|
|
|
307 |
|
308 |
-
|
309 |
-
|
310 |
-
"""
|
311 |
-
messages = [
|
312 |
-
{"role": "user", "content": user_content}
|
313 |
-
]
|
314 |
-
request_payload = {
|
315 |
-
"model": "gpt-3.5-turbo",
|
316 |
-
"messages": messages,
|
317 |
-
"max_tokens": 1000,
|
318 |
-
}
|
319 |
|
320 |
-
|
321 |
-
|
322 |
|
323 |
-
|
324 |
-
|
325 |
|
326 |
-
|
|
|
|
|
|
|
|
|
327 |
|
328 |
def update_paragraph_correct_grammatical_spelling_errors_input(paragraph):
|
329 |
return paragraph
|
330 |
|
331 |
-
def generate_refine_paragraph(eng_level, paragraph, user_refine_paragraph_prompt):
|
332 |
"""
|
333 |
根据用户输入的段落,调用OpenAI API生成相关的段落改善建议。
|
334 |
"""
|
@@ -338,7 +334,7 @@ def generate_refine_paragraph(eng_level, paragraph, user_refine_paragraph_prompt
|
|
338 |
{user_refine_paragraph_prompt}
|
339 |
"""
|
340 |
messages = [
|
341 |
-
{"role": "system", "content":
|
342 |
{"role": "user", "content": user_content}
|
343 |
]
|
344 |
|
@@ -347,16 +343,26 @@ def generate_refine_paragraph(eng_level, paragraph, user_refine_paragraph_prompt
|
|
347 |
request_payload = {
|
348 |
"model": "gpt-3.5-turbo",
|
349 |
"messages": messages,
|
350 |
-
"max_tokens":
|
351 |
"response_format": response_format
|
352 |
}
|
353 |
|
354 |
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
|
355 |
content = response.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
356 |
|
357 |
-
|
|
|
358 |
|
|
|
359 |
|
|
|
|
|
360 |
|
361 |
def paragraph_save_and_tts(paragraph_text):
|
362 |
"""
|
@@ -451,6 +457,19 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue, secondary
|
|
451 |
generate_topics_button = gr.Button("使用 🪄 JUTOR 產生 10 個段落主題,再挑選一個來練習吧!", variant="primary")
|
452 |
topic_output = gr.Textbox(label="AI Generated Topic 主題", visible=True, value=[])
|
453 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
454 |
@gr.render(inputs=topic_output)
|
455 |
def render_topics(topics):
|
456 |
topics_list = json.loads(topics)
|
@@ -488,6 +507,20 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue, secondary
|
|
488 |
generate_points_button = gr.Button("找尋靈感?使用 🪄 JUTOR 產生要點/關鍵字", variant="primary")
|
489 |
points_output = gr.Textbox(label="AI Generated Points 要點", visible=True, value=[])
|
490 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
491 |
@gr.render(inputs=points_output)
|
492 |
def render_points(points):
|
493 |
points_list = json.loads(points)
|
@@ -540,6 +573,21 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue, secondary
|
|
540 |
generate_topic_sentences_button = gr.Button("生成並在下面 3 個 JUTOR 產生的主題句中,選出一個最合適的", variant="primary")
|
541 |
topic_sentence_output = gr.Textbox(label="AI Generated Topic Sentences 主題句", value=[])
|
542 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
543 |
@gr.render(inputs=topic_sentence_output)
|
544 |
def render_topic_sentences(topic_sentences):
|
545 |
# Parsing the JSON string to a list
|
@@ -575,6 +623,8 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue, secondary
|
|
575 |
- avoiding overly technical language.
|
576 |
- Total word-count is around 50. no more explanation either no more extra non-relation sentences.
|
577 |
- just output supporting sentences, don't output topic sentence at this step.
|
|
|
|
|
578 |
|
579 |
EXAMPLE:
|
580 |
- Washing your hands often helps you stay healthy. It removes dirt and germs that can make you sick. Clean hands prevent the spread of diseases. You protect yourself and others by washing your hands regularly.
|
@@ -613,6 +663,22 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue, secondary
|
|
613 |
generate_supporting_sentences_button = gr.Button("讓 JUTOR 產生例句,幫助你撰寫支持句。", variant="primary")
|
614 |
supporting_sentences_output = gr.Radio(choices=[],label="AI Generated Supporting Sentences 支持句", elem_id="supporting_sentences_button")
|
615 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
616 |
supporting_sentences_output.select(
|
617 |
fn=update_supporting_sentences_input,
|
618 |
inputs=[supporting_sentences_output],
|
@@ -663,6 +729,22 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue, secondary
|
|
663 |
generate_conclusion_sentence_button = gr.Button("讓 JUTOR 產生例句,幫助你撰寫結論句。", variant="primary")
|
664 |
conclusion_sentence_output = gr.Radio(choices=[], label="AI Generated Conclusion Sentence 結論句")
|
665 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
666 |
conclusion_sentence_output.select(
|
667 |
fn=update_conclusion_sentence_input,
|
668 |
inputs=[conclusion_sentence_output],
|
@@ -800,7 +882,17 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue, secondary
|
|
800 |
with gr.Row():
|
801 |
generate_paragraph_evaluate_button = gr.Button("段落分析", variant="primary")
|
802 |
with gr.Row():
|
803 |
-
paragraph_evaluate_output = gr.Dataframe(label="完整段落分析", wrap=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
804 |
|
805 |
# 修訂文法與拼字錯誤
|
806 |
with gr.Row():
|
@@ -815,8 +907,17 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue, secondary
|
|
815 |
- When explaining, use Traditional Chinese (Taiwan, 繁體中文) for clarity.
|
816 |
- But others(original, Correction, revised_paragraph) in English.
|
817 |
- Make sure any revised vocabulary aligns with the eng_level.
|
818 |
-
|
819 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
820 |
The response should strictly be in the below JSON format and nothing else:
|
821 |
|
822 |
EXAMPLE:
|
@@ -847,24 +948,26 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue, secondary
|
|
847 |
paragraph_correct_grammatical_spelling_errors_input = gr.Textbox(label="這是你的原始寫作內容,參考 JUTOR 的改正,你可以選擇是否修改:")
|
848 |
with gr.Column():
|
849 |
generate_correct_grammatical_spelling_errors_button = gr.Button("JUTOR 修訂", variant="primary")
|
850 |
-
|
851 |
-
revised_paragraph_output = gr.Textbox(label="Revised Paragraph", show_copy_button=True)
|
|
|
852 |
revised_paragraph_diff = gr.HTML()
|
853 |
|
854 |
generate_correct_grammatical_spelling_errors_button.click(
|
855 |
fn=generate_correct_grammatical_spelling_errors,
|
856 |
inputs=[
|
|
|
857 |
eng_level_input,
|
858 |
paragraph_output,
|
859 |
user_correct_grammatical_spelling_errors_prompt,
|
860 |
],
|
861 |
outputs=[
|
862 |
-
|
863 |
revised_paragraph_output
|
864 |
]
|
865 |
).then(
|
866 |
-
fn=
|
867 |
-
inputs=[
|
868 |
outputs=revised_paragraph_diff
|
869 |
).then(
|
870 |
fn=update_paragraph_correct_grammatical_spelling_errors_input,
|
@@ -872,142 +975,132 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue, secondary
|
|
872 |
outputs=paragraph_correct_grammatical_spelling_errors_input
|
873 |
)
|
874 |
|
875 |
-
|
876 |
-
|
877 |
-
|
878 |
-
|
879 |
-
|
880 |
-
|
881 |
-
|
882 |
-
|
883 |
-
|
884 |
-
|
885 |
-
|
886 |
-
|
887 |
-
|
888 |
-
|
889 |
-
|
890 |
-
|
891 |
-
|
892 |
-
|
893 |
-
|
894 |
-
|
895 |
-
|
896 |
-
|
897 |
-
|
898 |
-
|
899 |
-
|
900 |
-
|
901 |
-
|
902 |
-
|
903 |
-
|
904 |
-
|
905 |
-
|
906 |
-
|
907 |
-
|
908 |
-
|
909 |
-
|
910 |
-
|
911 |
-
|
912 |
-
|
913 |
-
|
914 |
-
|
915 |
-
|
916 |
-
|
917 |
-
|
918 |
-
|
919 |
-
|
920 |
-
|
921 |
-
|
922 |
-
|
923 |
-
|
924 |
-
|
925 |
-
|
926 |
-
|
927 |
-
|
928 |
-
|
929 |
-
|
930 |
-
|
931 |
-
|
932 |
-
|
933 |
-
|
934 |
-
|
935 |
-
|
936 |
-
|
937 |
-
|
938 |
-
|
939 |
-
|
940 |
-
|
941 |
-
|
942 |
-
|
943 |
-
|
944 |
-
|
945 |
-
|
946 |
-
|
947 |
-
|
948 |
-
|
949 |
-
|
950 |
-
|
951 |
-
|
952 |
-
|
953 |
-
|
954 |
-
|
955 |
-
|
956 |
-
|
957 |
-
|
958 |
-
|
959 |
-
|
960 |
-
|
961 |
-
|
962 |
-
|
963 |
-
|
964 |
-
|
965 |
-
|
966 |
-
|
967 |
-
|
968 |
-
|
969 |
-
|
970 |
-
|
971 |
-
|
972 |
-
|
973 |
-
|
974 |
-
|
975 |
-
|
976 |
-
|
977 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
978 |
|
979 |
|
980 |
|
981 |
-
|
982 |
-
|
983 |
-
|
984 |
-
paragraph_output,
|
985 |
-
user_generate_paragraph_evaluate_prompt
|
986 |
-
],
|
987 |
-
outputs=paragraph_evaluate_output
|
988 |
-
)
|
989 |
|
990 |
|
991 |
|
992 |
-
|
993 |
-
fn=generate_refine_paragraph,
|
994 |
-
inputs=[
|
995 |
-
eng_level_input,
|
996 |
-
paragraph_correct_grammatical_spelling_errors_input,
|
997 |
-
user_refine_paragraph_prompt
|
998 |
-
],
|
999 |
-
outputs=refine_output
|
1000 |
-
)
|
1001 |
-
|
1002 |
-
paragraph_save_button.click(
|
1003 |
-
fn=paragraph_save_and_tts,
|
1004 |
-
inputs=[
|
1005 |
-
paragraph_refine_input
|
1006 |
-
],
|
1007 |
-
outputs=[
|
1008 |
-
paragraph_save_output,
|
1009 |
-
audio_output
|
1010 |
-
]
|
1011 |
-
)
|
1012 |
|
1013 |
demo.launch()
|
|
|
3 |
from openai import OpenAI
|
4 |
|
5 |
from difflib import Differ
|
6 |
+
import random
|
7 |
|
8 |
import json
|
9 |
import tempfile
|
10 |
+
import pandas as pd
|
11 |
+
|
12 |
|
13 |
is_env_local = os.getenv("IS_ENV_LOCAL", "false") == "true"
|
14 |
print(f"is_env_local: {is_env_local}")
|
|
|
117 |
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
|
118 |
response_content = json.loads(response.choices[0].message.content)
|
119 |
json_content = response_content["results"]
|
120 |
+
random.shuffle(json_content)
|
121 |
topic_sentences_text = json.dumps(json_content, ensure_ascii=False)
|
122 |
|
123 |
gr_update = gr.update(visible=False, value=topic_sentences_text)
|
|
|
219 |
paragraph = f"{topic_sentence} {supporting_sentences} {conclusion_sentence}"
|
220 |
return paragraph
|
221 |
|
222 |
+
def generate_paragraph_evaluate(sys_content, paragraph, user_generate_paragraph_evaluate_prompt):
|
223 |
"""
|
224 |
根据用户输入的段落,调用OpenAI API生成相关的段落分析。
|
225 |
"""
|
|
|
228 |
{user_generate_paragraph_evaluate_prompt}
|
229 |
"""
|
230 |
messages = [
|
231 |
+
{"role": "system", "content": sys_content},
|
232 |
{"role": "user", "content": user_content}
|
233 |
]
|
234 |
|
|
|
261 |
return gr_update
|
262 |
|
263 |
|
264 |
+
def generate_correct_grammatical_spelling_errors(sys_content, eng_level, paragraph, user_correct_grammatical_spelling_errors_prompt):
|
265 |
"""
|
266 |
根据用户输入的段落,调用OpenAI API生成相关的文法和拼字错误修正。
|
267 |
"""
|
|
|
271 |
{user_correct_grammatical_spelling_errors_prompt}
|
272 |
"""
|
273 |
messages = [
|
274 |
+
{"role": "system", "content": sys_content},
|
275 |
{"role": "user", "content": user_content}
|
276 |
]
|
277 |
response_format = { "type": "json_object" }
|
|
|
291 |
[item['original'], item['correction'], item['explanation']]
|
292 |
for item in data['Corrections and Explanations']
|
293 |
]
|
294 |
+
headers = ["原文", "建議", "解釋"]
|
295 |
|
296 |
corrections_list_gr_update = gr.update(value=corrections_list, headers=headers, wrap=True)
|
297 |
reverse_paragraph_gr_update = gr.update(value=data["Revised Paragraph"])
|
298 |
|
299 |
return corrections_list_gr_update, reverse_paragraph_gr_update
|
300 |
|
301 |
+
def highlight_diff_texts(highlight_list, text):
|
302 |
+
# Convert DataFrame to JSON string
|
303 |
+
highlight_list_json = highlight_list.to_json()
|
|
|
|
|
|
|
|
|
304 |
|
305 |
+
# Print the JSON string to see its structure
|
306 |
+
print("=======highlight_list_json=======")
|
307 |
+
print(highlight_list_json)
|
308 |
|
309 |
+
# Parse JSON string back to dictionary
|
310 |
+
highlight_list_dict = json.loads(highlight_list_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
311 |
|
312 |
+
# Extract suggestions from the parsed JSON
|
313 |
+
suggestions = [highlight_list_dict['建議'][str(i)] for i in range(len(highlight_list_dict['建議']))]
|
314 |
|
315 |
+
# Initialize the HTML for text
|
316 |
+
text_html = f"<p>{text}</p>"
|
317 |
|
318 |
+
# Replace each suggestion in text with highlighted version
|
319 |
+
for suggestion in suggestions:
|
320 |
+
text_html = text_html.replace(suggestion, f'<span style="background-color:yellow;">{suggestion}</span>')
|
321 |
+
|
322 |
+
return text_html
|
323 |
|
324 |
def update_paragraph_correct_grammatical_spelling_errors_input(paragraph):
|
325 |
return paragraph
|
326 |
|
327 |
+
def generate_refine_paragraph(sys_content, eng_level, paragraph, user_refine_paragraph_prompt):
|
328 |
"""
|
329 |
根据用户输入的段落,调用OpenAI API生成相关的段落改善建议。
|
330 |
"""
|
|
|
334 |
{user_refine_paragraph_prompt}
|
335 |
"""
|
336 |
messages = [
|
337 |
+
{"role": "system", "content": sys_content},
|
338 |
{"role": "user", "content": user_content}
|
339 |
]
|
340 |
|
|
|
343 |
request_payload = {
|
344 |
"model": "gpt-3.5-turbo",
|
345 |
"messages": messages,
|
346 |
+
"max_tokens": 4000,
|
347 |
"response_format": response_format
|
348 |
}
|
349 |
|
350 |
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
|
351 |
content = response.choices[0].message.content
|
352 |
+
data = json.loads(content)
|
353 |
+
headers = ["原文", "建議", "解釋"]
|
354 |
+
table_data = [
|
355 |
+
[item['origin'], item['suggestion'], item['explanation']]
|
356 |
+
for item in data['Suggestions and Explanations']
|
357 |
+
]
|
358 |
|
359 |
+
refine_paragraph_gr_update = gr.update(value=table_data, headers=headers)
|
360 |
+
revised_paragraph_gr_update = gr.update(value=data["Revised Paragraph"])
|
361 |
|
362 |
+
return refine_paragraph_gr_update, revised_paragraph_gr_update
|
363 |
|
364 |
+
def update_paragraph_refine_input(text):
|
365 |
+
return text
|
366 |
|
367 |
def paragraph_save_and_tts(paragraph_text):
|
368 |
"""
|
|
|
457 |
generate_topics_button = gr.Button("使用 🪄 JUTOR 產生 10 個段落主題,再挑選一個來練習吧!", variant="primary")
|
458 |
topic_output = gr.Textbox(label="AI Generated Topic 主題", visible=True, value=[])
|
459 |
|
460 |
+
generate_topics_button.click(
|
461 |
+
fn=generate_topics,
|
462 |
+
inputs=[
|
463 |
+
model,
|
464 |
+
max_tokens,
|
465 |
+
sys_content_input,
|
466 |
+
scenario_input,
|
467 |
+
eng_level_input,
|
468 |
+
user_generate_topics_prompt
|
469 |
+
],
|
470 |
+
outputs=[topic_output]
|
471 |
+
)
|
472 |
+
|
473 |
@gr.render(inputs=topic_output)
|
474 |
def render_topics(topics):
|
475 |
topics_list = json.loads(topics)
|
|
|
507 |
generate_points_button = gr.Button("找尋靈感?使用 🪄 JUTOR 產生要點/關鍵字", variant="primary")
|
508 |
points_output = gr.Textbox(label="AI Generated Points 要點", visible=True, value=[])
|
509 |
|
510 |
+
generate_points_button.click(
|
511 |
+
fn=generate_points,
|
512 |
+
inputs=[
|
513 |
+
model,
|
514 |
+
max_tokens,
|
515 |
+
sys_content_input,
|
516 |
+
scenario_input,
|
517 |
+
eng_level_input,
|
518 |
+
topic_input,
|
519 |
+
user_generate_points_prompt
|
520 |
+
],
|
521 |
+
outputs=points_output
|
522 |
+
)
|
523 |
+
|
524 |
@gr.render(inputs=points_output)
|
525 |
def render_points(points):
|
526 |
points_list = json.loads(points)
|
|
|
573 |
generate_topic_sentences_button = gr.Button("生成並在下面 3 個 JUTOR 產生的主題句中,選出一個最合適的", variant="primary")
|
574 |
topic_sentence_output = gr.Textbox(label="AI Generated Topic Sentences 主題句", value=[])
|
575 |
|
576 |
+
generate_topic_sentences_button.click(
|
577 |
+
fn=generate_topic_sentences,
|
578 |
+
inputs=[
|
579 |
+
model,
|
580 |
+
max_tokens,
|
581 |
+
sys_content_input,
|
582 |
+
scenario_input,
|
583 |
+
eng_level_input,
|
584 |
+
topic_input,
|
585 |
+
points_input,
|
586 |
+
user_generate_topic_sentences_prompt
|
587 |
+
],
|
588 |
+
outputs=topic_sentence_output
|
589 |
+
)
|
590 |
+
|
591 |
@gr.render(inputs=topic_sentence_output)
|
592 |
def render_topic_sentences(topic_sentences):
|
593 |
# Parsing the JSON string to a list
|
|
|
623 |
- avoiding overly technical language.
|
624 |
- Total word-count is around 50. no more explanation either no more extra non-relation sentences.
|
625 |
- just output supporting sentences, don't output topic sentence at this step.
|
626 |
+
- don't output bullet points, just output sentences.
|
627 |
+
- don't number the sentences.
|
628 |
|
629 |
EXAMPLE:
|
630 |
- Washing your hands often helps you stay healthy. It removes dirt and germs that can make you sick. Clean hands prevent the spread of diseases. You protect yourself and others by washing your hands regularly.
|
|
|
663 |
generate_supporting_sentences_button = gr.Button("讓 JUTOR 產生例句,幫助你撰寫支持句。", variant="primary")
|
664 |
supporting_sentences_output = gr.Radio(choices=[],label="AI Generated Supporting Sentences 支持句", elem_id="supporting_sentences_button")
|
665 |
|
666 |
+
generate_supporting_sentences_button.click(
|
667 |
+
fn=generate_supporting_sentences,
|
668 |
+
inputs=[
|
669 |
+
model,
|
670 |
+
max_tokens,
|
671 |
+
sys_content_input,
|
672 |
+
scenario_input,
|
673 |
+
eng_level_input,
|
674 |
+
topic_input,
|
675 |
+
points_input,
|
676 |
+
topic_sentence_input,
|
677 |
+
user_generate_supporting_sentences_prompt
|
678 |
+
],
|
679 |
+
outputs=supporting_sentences_output
|
680 |
+
)
|
681 |
+
|
682 |
supporting_sentences_output.select(
|
683 |
fn=update_supporting_sentences_input,
|
684 |
inputs=[supporting_sentences_output],
|
|
|
729 |
generate_conclusion_sentence_button = gr.Button("讓 JUTOR 產生例句,幫助你撰寫結論句。", variant="primary")
|
730 |
conclusion_sentence_output = gr.Radio(choices=[], label="AI Generated Conclusion Sentence 結論句")
|
731 |
|
732 |
+
generate_conclusion_sentence_button.click(
|
733 |
+
fn=generate_conclusion_sentences,
|
734 |
+
inputs=[
|
735 |
+
model,
|
736 |
+
max_tokens,
|
737 |
+
sys_content_input,
|
738 |
+
scenario_input,
|
739 |
+
eng_level_input,
|
740 |
+
topic_input,
|
741 |
+
points_input,
|
742 |
+
topic_sentence_input,
|
743 |
+
user_generate_conclusion_sentence_prompt
|
744 |
+
],
|
745 |
+
outputs=conclusion_sentence_output
|
746 |
+
)
|
747 |
+
|
748 |
conclusion_sentence_output.select(
|
749 |
fn=update_conclusion_sentence_input,
|
750 |
inputs=[conclusion_sentence_output],
|
|
|
882 |
with gr.Row():
|
883 |
generate_paragraph_evaluate_button = gr.Button("段落分析", variant="primary")
|
884 |
with gr.Row():
|
885 |
+
paragraph_evaluate_output = gr.Dataframe(label="完整段落分析", wrap=True, column_widths=[35, 15, 50], interactive=False)
|
886 |
+
|
887 |
+
generate_paragraph_evaluate_button.click(
|
888 |
+
fn=generate_paragraph_evaluate,
|
889 |
+
inputs=[
|
890 |
+
sys_content_input,
|
891 |
+
paragraph_output,
|
892 |
+
user_generate_paragraph_evaluate_prompt
|
893 |
+
],
|
894 |
+
outputs=paragraph_evaluate_output
|
895 |
+
)
|
896 |
|
897 |
# 修訂文法與拼字錯誤
|
898 |
with gr.Row():
|
|
|
907 |
- When explaining, use Traditional Chinese (Taiwan, 繁體中文) for clarity.
|
908 |
- But others(original, Correction, revised_paragraph) in English.
|
909 |
- Make sure any revised vocabulary aligns with the eng_level.
|
910 |
+
Guidelines for Length and Complexity:
|
911 |
+
- Please keep explanations concise and straightforward
|
912 |
+
- if no more suggestions, please provide at least 2 suggestions and explain that the paragraph is another possible way to improve.
|
913 |
+
|
914 |
+
Restrictions:
|
915 |
+
- avoiding overly technical language.
|
916 |
+
- don't give any suggestions about the sentence to remain unchanged.
|
917 |
+
- don't give suggestions about the Period, Comma etc.
|
918 |
+
- Do not change the original text's case.
|
919 |
+
- if no mistakes, don't need to revise.
|
920 |
+
|
921 |
The response should strictly be in the below JSON format and nothing else:
|
922 |
|
923 |
EXAMPLE:
|
|
|
948 |
paragraph_correct_grammatical_spelling_errors_input = gr.Textbox(label="這是你的原始寫作內容,參考 JUTOR 的改正,你可以選擇是否修改:")
|
949 |
with gr.Column():
|
950 |
generate_correct_grammatical_spelling_errors_button = gr.Button("JUTOR 修訂", variant="primary")
|
951 |
+
correct_grammatical_spelling_errors_output_table = gr.Dataframe(label="修訂文法與拼字錯誤", interactive=False)
|
952 |
+
revised_paragraph_output = gr.Textbox(label="Revised Paragraph", show_copy_button=True, visible=False)
|
953 |
+
gr.Markdown("## 修訂結果")
|
954 |
revised_paragraph_diff = gr.HTML()
|
955 |
|
956 |
generate_correct_grammatical_spelling_errors_button.click(
|
957 |
fn=generate_correct_grammatical_spelling_errors,
|
958 |
inputs=[
|
959 |
+
sys_content_input,
|
960 |
eng_level_input,
|
961 |
paragraph_output,
|
962 |
user_correct_grammatical_spelling_errors_prompt,
|
963 |
],
|
964 |
outputs=[
|
965 |
+
correct_grammatical_spelling_errors_output_table,
|
966 |
revised_paragraph_output
|
967 |
]
|
968 |
).then(
|
969 |
+
fn=highlight_diff_texts,
|
970 |
+
inputs=[correct_grammatical_spelling_errors_output_table, revised_paragraph_output],
|
971 |
outputs=revised_paragraph_diff
|
972 |
).then(
|
973 |
fn=update_paragraph_correct_grammatical_spelling_errors_input,
|
|
|
975 |
outputs=paragraph_correct_grammatical_spelling_errors_input
|
976 |
)
|
977 |
|
978 |
+
# 段落改善建議
|
979 |
+
with gr.Row():
|
980 |
+
with gr.Column():
|
981 |
+
with gr.Row() as paragraph_refine_params:
|
982 |
+
default_user_refine_paragraph_prompt = """
|
983 |
+
I need assistance with revising a paragraph. Please "Refine" the "Revised Version 1" and immediately "Provide Explanations" for each suggestion you made.
|
984 |
+
- Revised Version 1 (for correction): paragraph_ai_modification(split by punctuation mark)
|
985 |
+
- Do not modify the sentence: topicSentence"
|
986 |
+
- Make sure any revised vocabulary aligns with the eng_level.
|
987 |
+
- When explaining, use Traditional Chinese (Taiwan, 繁體中文 zh-TW) for clarity.
|
988 |
+
- But others(Origin, Suggestion, revised_paragraph_v2) use English, that's very important.
|
989 |
+
|
990 |
+
Guidelines for Length and Complexity:
|
991 |
+
- Please keep explanations concise and straightforward
|
992 |
+
- if no more suggestions, please provide at least 2 suggestions and explain that the paragraph is another possible way to improve.
|
993 |
+
|
994 |
+
Restrictions:
|
995 |
+
- avoiding overly technical language.
|
996 |
+
- don't change the original text's case.
|
997 |
+
- don't give suggestions about the Period, Comma etc.
|
998 |
+
|
999 |
+
The response should strictly be in the below JSON format and nothing else:
|
1000 |
+
|
1001 |
+
EXAMPLE:
|
1002 |
+
{
|
1003 |
+
"Suggestions and Explanations": [
|
1004 |
+
{ "origin": "#original_text_1", "suggestion": "#suggestion_1", "explanation": "#explanation_1(in_traditional_chinese zh-TW)" },
|
1005 |
+
{ "origin": "#original_text_2", "suggestion": "#suggestion_2", "explanation": "#explanation_2(in_traditional_chinese zh-TW)" },
|
1006 |
+
... ],
|
1007 |
+
"Revised Paragraph": "#revised_paragraph_v2"
|
1008 |
+
}
|
1009 |
+
"""
|
1010 |
+
user_refine_paragraph_prompt = gr.Textbox(label="Refine Paragraph Prompt", value=default_user_refine_paragraph_prompt, visible=False)
|
1011 |
+
|
1012 |
+
with gr.Row() as paragraph_refine_html:
|
1013 |
+
gr.Markdown("# Step 9. 段落改善建議(Refine Paragraph)")
|
1014 |
+
with gr.Accordion("參考指引:段落改善建議?", open=False):
|
1015 |
+
gr.Markdown("""
|
1016 |
+
- 段落寫作的過程,如果全程採用 JUTOR 的建議例句,在這部分的批改可能會發生自我修訂的現象。例如:為了符合級別需求,JUTOR 會將自已建議的例句,以換句話說的方式再次修改,你可以忽略。
|
1017 |
+
- 若是自行完成段落寫作,則不會發生自我修訂的混淆狀況。
|
1018 |
+
""")
|
1019 |
+
with gr.Row():
|
1020 |
+
with gr.Column():
|
1021 |
+
paragraph_refine_input = gr.Textbox(label="這是你的原始寫作內���,參考 JUTOR 的建議,你可以選擇是否修改:", show_copy_button=True)
|
1022 |
+
with gr.Column():
|
1023 |
+
generate_refine_paragraph_button = gr.Button("段落改善建議", variant="primary")
|
1024 |
+
refine_output_table = gr.DataFrame(label="Refine Paragraph 段落改善建議", wrap=True, interactive=False)
|
1025 |
+
refine_output = gr.HTML(label="修改建議", visible=False)
|
1026 |
+
gr.Markdown("## 修改結果")
|
1027 |
+
refine_output_diff = gr.HTML()
|
1028 |
+
|
1029 |
+
generate_refine_paragraph_button.click(
|
1030 |
+
fn=generate_refine_paragraph,
|
1031 |
+
inputs=[
|
1032 |
+
sys_content_input,
|
1033 |
+
eng_level_input,
|
1034 |
+
paragraph_correct_grammatical_spelling_errors_input,
|
1035 |
+
user_refine_paragraph_prompt
|
1036 |
+
],
|
1037 |
+
outputs=[refine_output_table, refine_output]
|
1038 |
+
).then(
|
1039 |
+
fn=highlight_diff_texts,
|
1040 |
+
inputs=[refine_output_table, refine_output],
|
1041 |
+
outputs=refine_output_diff
|
1042 |
+
).then(
|
1043 |
+
fn=update_paragraph_refine_input,
|
1044 |
+
inputs=[paragraph_correct_grammatical_spelling_errors_input],
|
1045 |
+
outputs=paragraph_refine_input
|
1046 |
+
)
|
1047 |
+
|
1048 |
+
# Final Step. 寫作完成
|
1049 |
+
with gr.Row():
|
1050 |
+
with gr.Column():
|
1051 |
+
with gr.Row():
|
1052 |
+
gr.Markdown("## Final Step. 寫作完成 Save and Share")
|
1053 |
+
with gr.Row():
|
1054 |
+
gr.Markdown("### 完成修訂!你按部就班地完成了一次段落寫作練習,太棒了!")
|
1055 |
+
with gr.Row():
|
1056 |
+
paragraph_save_button = gr.Button("建立歷程回顧", variant="primary")
|
1057 |
+
with gr.Row():
|
1058 |
+
gr.Markdown("## 歷程回顧")
|
1059 |
+
paragraph_save_output = gr.HTML(label="Save and Share")
|
1060 |
+
|
1061 |
+
with gr.Row():
|
1062 |
+
audio_output = gr.Audio(label="Generated Speech", type="filepath")
|
1063 |
+
|
1064 |
+
|
1065 |
+
paragraph_save_button.click(
|
1066 |
+
fn=paragraph_save_and_tts,
|
1067 |
+
inputs=[
|
1068 |
+
paragraph_refine_input
|
1069 |
+
],
|
1070 |
+
outputs=[
|
1071 |
+
paragraph_save_output,
|
1072 |
+
audio_output
|
1073 |
+
]
|
1074 |
+
)
|
1075 |
+
# paragraph_history_make_button.click(
|
1076 |
+
# fn=generate_paragraph_history,
|
1077 |
+
# inputs=[
|
1078 |
+
# topic_sentence_input,
|
1079 |
+
# supporting_sentences_input,
|
1080 |
+
# conclusion_sentence_input,
|
1081 |
+
# paragraph_output,
|
1082 |
+
# correct_grammatical_spelling_errors_output,
|
1083 |
+
# refine_output_table,
|
1084 |
+
# refine_output
|
1085 |
+
# ],
|
1086 |
+
# outputs=paragraph_save_output
|
1087 |
+
# )
|
1088 |
+
|
1089 |
+
|
1090 |
+
|
1091 |
+
|
1092 |
+
|
1093 |
+
|
1094 |
+
|
1095 |
|
1096 |
|
1097 |
|
1098 |
+
|
1099 |
+
|
1100 |
+
|
|
|
|
|
|
|
|
|
|
|
1101 |
|
1102 |
|
1103 |
|
1104 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1105 |
|
1106 |
demo.launch()
|