Spaces:
Sleeping
Sleeping
File size: 24,448 Bytes
15b9da3 46f5603 c775910 a0a3ddc 15b9da3 78f5c78 029fc0c f4a1918 78f5c78 d947472 46f5603 c71a877 46f5603 029fc0c 46f5603 8a473de 11b1dbb 877f289 11b1dbb 877f289 11b1dbb c9e97ad 11b1dbb c71a877 c9e97ad daa5f8e cc83ab8 12a83ad c1ee647 f35c083 19c63ff f35c083 19c63ff f35c083 46f5603 c71a877 d947472 0e5dd94 8c415e0 c71a877 46f5603 0e5dd94 c71a877 252a456 0e5dd94 f6fe762 c71a877 252a456 0e5dd94 f6fe762 c71a877 252a456 11b1dbb c9e97ad 11b1dbb f6fe762 d947472 cc83ab8 daa5f8e bbacb5a daa5f8e f6fe762 daa5f8e cc83ab8 f6fe762 cc83ab8 252a456 12a83ad fa5cc21 252a456 0fea6e9 fa5cc21 0fea6e9 46f5603 19c63ff f35c083 19c63ff 46f5603 d947472 c71a877 46f5603 8a473de 11b1dbb dad95ce 11b1dbb daa5f8e cc83ab8 1d7518e cc83ab8 12a83ad 0fea6e9 19c63ff f35c083 19c63ff f35c083 46f5603 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 |
import os
import gradio as gr
from openai import OpenAI
import json
OPEN_AI_KEY = os.getenv("OPEN_AI_KEY")
OPEN_AI_CLIENT = OpenAI(api_key=OPEN_AI_KEY)
def generate_topics(model, max_tokens, sys_content, scenario, eng_level, user_generate_topics_prompt):
"""
根据系统提示和用户输入的情境及主题,调用OpenAI API生成相关的主题句。
"""
user_content = f"""
scenario is {scenario}
english level is {eng_level}
{user_generate_topics_prompt}
"""
messages = [
{"role": "system", "content": sys_content},
{"role": "user", "content": user_content}
]
request_payload = {
"model": model,
"messages": messages,
"max_tokens": max_tokens,
}
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
content = response.choices[0].message.content.strip()
return content
def generate_points(model, max_tokens, sys_content, scenario, eng_level, topic, user_generate_points_prompt):
"""
根据系统提示和用户输入的情境、主题,调用OpenAI API生成相关的主题句。
"""
user_content = f"""
scenario is {scenario}
english level is {eng_level}
topic is {topic}
{user_generate_points_prompt}
"""
messages = [
{"role": "system", "content": sys_content},
{"role": "user", "content": user_content}
]
request_payload = {
"model": model,
"messages": messages,
"max_tokens": max_tokens,
}
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
content = response.choices[0].message.content.strip()
return content
def generate_topic_sentences(model, max_tokens, sys_content, scenario, eng_level, topic, points, user_generate_topic_sentences_prompt):
"""
根据系统提示和用户输入的情境及要点,调用OpenAI API生成相关的主题句及其合理性解释。
"""
user_content = f"""
scenario is {scenario}
english level is {eng_level}
topic is {topic}
points is {points}
{user_generate_topic_sentences_prompt}
"""
messages = [
{"role": "system", "content": sys_content},
{"role": "user", "content": user_content}
]
response_format = { "type": "json_object" }
request_payload = {
"model": model,
"messages": messages,
"max_tokens": max_tokens,
"response_format": response_format
}
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
json_content = response.choices[0].message.content
content = parse_and_display_topic_sentences(json_content)
return content
def parse_and_display_topic_sentences(json_data):
"""
解析JSON格式的主题句数据,并转换成易于阅读的格式。
"""
# 将JSON字符串解析成Python字典
data = json.loads(json_data)
# 初始化一个空字符串用于存放最终的格式化文本
formatted_text = ""
# 遍历每个主题句及其评价
for key, value in data.items():
topic_sentence = value[0]['topic-sentence']
appropriate = "適當" if value[0]['appropriate'] == "Y" else "不適當"
reason = value[0]['reason']
# 将每个主题句的信息添加到格式化文本中
formatted_text += f"主题句 {int(key)+1}: {topic_sentence}\n"
formatted_text += f"是否適當: {appropriate}\n"
formatted_text += f"原因: {reason}\n\n"
return formatted_text
def generate_supporting_sentences(model, max_tokens, sys_content, scenario, eng_level, topic, points, topic_sentence, user_generate_supporting_sentences_prompt):
"""
根据系统提示和用户输入的情境、主题、要点、主题句,调用OpenAI API生成相关的支持句。
"""
user_content = f"""
scenario is {scenario}
english level is {eng_level}
topic is {topic}
points is {points}
topic sentence is {topic_sentence}
{user_generate_supporting_sentences_prompt}
"""
messages = [
{"role": "system", "content": sys_content},
{"role": "user", "content": user_content}
]
request_payload = {
"model": model,
"messages": messages,
"max_tokens": max_tokens,
}
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
content = response.choices[0].message.content.strip()
return content
def generate_conclusion_sentences(model, max_tokens, sys_content, scenario, eng_level, topic, points, topic_sentence, user_generate_conclusion_sentence_prompt):
"""
根据系统提示和用户输入的情境、主题、要点、主题句,调用OpenAI API生成相关的结论句。
"""
user_content = f"""
scenario is {scenario}
english level is {eng_level}
topic is {topic}
points is {points}
topic sentence is {topic_sentence}
{user_generate_conclusion_sentence_prompt}
"""
messages = [
{"role": "system", "content": sys_content},
{"role": "user", "content": user_content}
]
request_payload = {
"model": model,
"messages": messages,
"max_tokens": max_tokens,
}
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
content = response.choices[0].message.content.strip()
return content
def generate_paragraph(topic_sentence, supporting_sentences, conclusion_sentence):
"""
根据用户输入的主题句、支持句、结论句,生成完整的段落。
"""
paragraph = f"{topic_sentence}\n{supporting_sentences}\n{conclusion_sentence}"
return paragraph
def generate_paragraph_evaluate(paragraph, user_generate_paragraph_evaluate_prompt):
"""
根据用户输入的段落,调用OpenAI API生成相关的段落分析。
"""
user_content = f"""
paragraph is {paragraph}
{user_generate_paragraph_evaluate_prompt}
"""
messages = [
{"role": "system", "content": paragraph},
{"role": "user", "content": user_content}
]
response_format = { "type": "json_object" }
request_payload = {
"model": "gpt-3.5-turbo",
"messages": messages,
"max_tokens": 500,
"response_format": response_format
}
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
content = response.choices[0].message.content
return content
def generate_correct_grammatical_spelling_errors(eng_level, paragraph, user_correct_grammatical_spelling_errors_prompt):
"""
根据用户输入的段落,调用OpenAI API生成相关的文法和拼字错误修正。
"""
user_content = f"""
level is {eng_level}
paragraph is {paragraph}
{user_correct_grammatical_spelling_errors_prompt}
"""
messages = [
{"role": "system", "content": paragraph},
{"role": "user", "content": user_content}
]
response_format = { "type": "json_object" }
request_payload = {
"model": "gpt-3.5-turbo",
"messages": messages,
"max_tokens": 500,
"response_format": response_format
}
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
content = response.choices[0].message.content
return content
def generate_refine_paragraph(eng_level, paragraph, user_refine_paragraph_prompt):
"""
根据用户输入的段落,调用OpenAI API生成相关的段落改善建议。
"""
user_content = f"""
eng_level is {eng_level}
paragraph is {paragraph}
{user_refine_paragraph_prompt}
"""
messages = [
{"role": "system", "content": paragraph},
{"role": "user", "content": user_content}
]
response_format = { "type": "json_object" }
request_payload = {
"model": "gpt-3.5-turbo",
"messages": messages,
"max_tokens": 500,
"response_format": response_format
}
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
content = response.choices[0].message.content
return content
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
# basic inputs
gr.Markdown("## 1. Basic Inputs")
model = gr.Radio(["gpt-4-1106-preview", "gpt-3.5-turbo"], label="Model", value="gpt-4-1106-preview")
max_tokens = gr.Slider(minimum=50, maximum=4000, value=1000, label="Max Tokens")
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.")
scenario_input = gr.Textbox(label="Scenario")
eng_level_input = gr.Radio(["beginner", "intermediate", "advanced"], label="English Level", value="beginner")
gr.Markdown("## 2. Generate Topic 主題")
default_generate_topics_prompt = """
Give me 10 topics relevant to Scenario,
for a paragraph. Just the topics, no explanation, use simple English language.
Make sure the vocabulary you use is at english level.
"""
user_generate_topics_prompt = gr.Textbox(label="Topics Prompt", value=default_generate_topics_prompt)
generate_topics_button = gr.Button("AI Generate Topic Sentences")
topic_output = gr.Textbox(label="AI Generated Topic 主題")
topic_input = gr.Textbox(label="Topic")
gr.Markdown("## 3. Generate Points 要點")
default_generate_points_prompt = """
Please provide main points to develop in a paragraph about topic in the context of scenario,
use simple English language and make sure the vocabulary you use is at eng_level.
No more explanation either no developing these points into a simple paragraph.
"""
user_generate_points_prompt = gr.Textbox(label="Points Prompt", value=default_generate_points_prompt)
generate_points_button = gr.Button("AI Generate Points")
points_output = gr.Textbox(label="AI Generated Points 要點")
points_input = gr.Textbox(label="Points")
gr.Markdown("## 4. Generate Topic Sentences 主題句")
default_generate_topic_sentences_prompt = """
Please provide one appropriate topic sentence that aptly introduces the subject for the given scenario and topic.
Additionally, provide two topic sentences that, while related to the topic,
would be considered inappropriate or less effective for the specified context.
Those sentences must include the three main points:".
Use English language and each sentence should not be too long.
For each sentence, explain the reason in Traditional Chinese, Taiwan, 繁體中文 zh-TW.
Make sure the vocabulary you use is at level.
Only return the result in JSON format starting as:
{{
"0": [ {{ "topic-sentence": "#","appropriate": "Y/N", "reason": "#中文解釋" }} ],
"1": [ {{ "topic-sentence": "#","appropriate": "Y/N", "reason": "#中文解釋" }} ],
"2": [ {{ "topic-sentence": "#","appropriate": "Y/N", "reason": "#中文解釋" }} ]
}}
"""
user_generate_topic_sentences_prompt = gr.Textbox(label="Topic Sentences Prompt", value=default_generate_topic_sentences_prompt)
generate_topic_sentences_button = gr.Button("AI Generate Topic Sentences")
topic_sentence_output = gr.Textbox(label="AI Generated Topic Sentences 主題句")
topic_sentence_input = gr.Textbox(label="Topic Sentences")
gr.Markdown("## 5. Generate Supporting Sentence 支持句")
default_generate_supporting_sentences_prompt = """
I'm aiming to improve my writing. I have a topic sentence as topic_sentence_input.
Please assist me by "Developing supporting detials" based on the keyword: points to write three sentences as an example.
- Make sure any revised vocabulary aligns with the eng_level.
- Guidelines for Length and Complexity:
Please keep the example concise and straightforward,
avoiding overly technical language.
Total word-count is around 50. no more explanation either no more extra non-relation sentences.
"""
user_generate_supporting_sentences_prompt = gr.Textbox(label="Supporting Sentences Prompt", value=default_generate_supporting_sentences_prompt)
generate_supporting_sentences_button = gr.Button("AI Generate Supporting Sentences")
supporting_sentences_output = gr.Textbox(label="AI Generated Supporting Sentences 支持句")
supporting_sentences_input = gr.Textbox(label="Supporting Sentences")
gr.Markdown("## 6. Conclusion sentence 結論句")
default_generate_conclusion_sentence_prompt = """
I'm aiming to improve my writing.
By the topic sentence, please assist me by "Developing conclusion sentences"
based on keywords of points to finish a paragrpah as an example.
- Make sure any revised vocabulary aligns with the correctly level.
- Guidelines for Length and Complexity:
Please keep the example concise and straightforward,
avoiding overly technical language.
Total word-count is around 20.
"""
user_generate_conclusion_sentence_prompt = gr.Textbox(label="Conclusion Sentence Prompt", value=default_generate_conclusion_sentence_prompt)
generate_conclusion_sentence_button = gr.Button("AI Generate Conclusion Sentence")
conclusion_sentence_output = gr.Textbox(label="AI Generated Conclusion Sentence 結論句")
conclusion_sentence_input = gr.Textbox(label="Conclusion Sentence")
gr.Markdown("## 7. Paragraph Integration and Revision 段落確認與修訂")
generate_paragraph_button = gr.Button("Generate Paragraph")
paragraph_output = gr.Textbox(label="Generated Paragraph 完整段落")
paragraph_input = gr.Textbox(label="Paragraph")
gr.Markdown("## 8. Evaluate 分析")
default_user_generate_paragraph_evaluate_prompt = """
Based on the final paragraph provided, evaluate the writing in terms of content, organization, grammar, and vocabulary. Provide feedback in simple and supportive language.
-- 根據上述的文章,以「內容(content)」層面評分。
- 評分等級有三級:beginner, intermediate, advanced. - 以繁體中文解釋
評分結果以 JSON 格式輸出: content: {
"content_level": "#beginner/intermediate/advanced",
"content_explanation": "#中文解釋"
}
-- 根據上述的文章,以「組織(organization)」層面評分。
- 評分等級有三級:beginner, intermediate, advanced. - 以繁體中文解釋
評分結果以 JSON 格式輸出: organization: {
"organization_level": "#beginner/intermediate/advanced",
"organization_explanation": "#中文解釋"
}
-- 根據上述的文章,以「文法和用法(Grammar and usage)」層面評分。
- 評分等級有三級:beginner, intermediate, advanced. - 以繁體中文解釋
評分結果以 JSON 格式輸出: grammar_and_usage: {
"GrammarAndUsage_level": "#beginner/intermediate/advanced",
"GrammarAndUsage_explanation": "#中文解釋"
}
-- 根據上述的文章,以「詞彙(Vocabulary )」層面評分。
- 評分等級有三級:beginner, intermediate, advanced. - 以繁體中文解釋
評分結果以 JSON 格式輸出: vocabulary: {
"Vocabulary_level": "#beginner/intermediate/advanced",
"Vocabulary_explanation": "#中文解釋"
}
-- 根據上述的文章,以「連貫性和連接詞(Coherence and Cohesion)」層面評分。
- 評分等級有三級:beginner, intermediate, advanced. - 以繁體中文解釋
評分結果以 JSON 格式輸出: coherence_and_cohesion: {
"CoherenceAndCohesion_level": "#beginner/intermediate/advanced",
"CoherenceAndCohesion_explanation": "#中文解釋"
}
將上述的輸出為 JSON:
{{
“content“: {content’s dict},
“organization“: {organization dict},
“grammar_and_usage“: {grammar_and_usage dict},
“vocabulary“: {vocabulary dict},
“coherence_and_cohesion“: {coherence_and_cohesion dict}
}}
"""
user_generate_paragraph_evaluate_prompt = gr.Textbox(label="Paragraph evaluate Prompt", value=default_user_generate_paragraph_evaluate_prompt)
generate_paragraph_evaluate_button = gr.Button("Save and Evaluate")
paragraph_evaluate_output = gr.Textbox(label="Generated Paragraph evaluate 完整段落分析")
gr.Markdown("## 9. Correct Grammatical and Spelling Errors 修訂文法與拼字錯誤")
default_user_correct_grammatical_spelling_errors_prompt = """
I'm aiming to improve my writing.
Please assist me by "Correcting Grammatical and Spelling Errors" in the provided paragraph.
For every correction you make, I'd like an "Explanation" to understand the reasoning behind it.
- Paragraph for Correction: [paragraph_ai_modification(split by 標點符號)]
- The sentence to remain unchanged: [sentence_to_remain_unchanged]
- When explaining, use Traditional Chinese (Taiwan, 繁體中文) for clarity.
- Make sure any revised vocabulary aligns with the eng level.
- Guidelines for Length and Complexity: Please keep explanations concise and straightforward,
avoiding overly technical language.
The response should strictly be in the below JSON format and nothing else:
{
"Corrections and Explanations": [
{ "original": "# original_sentence1", "Correction": "#correction_1", "Explanation": "#explanation_1_in_traditional_chinese" },
{ "original": "# original_sentence2", "Correction": "#correction_2", "Explanation": "#explanation_2_in_traditional_chinese" },
...
],
"Revised Paragraph": "#revised_paragraph"
}
"""
user_correct_grammatical_spelling_errors_prompt = gr.Textbox(label="Correct Grammatical and Spelling Errors Prompt", value=default_user_correct_grammatical_spelling_errors_prompt)
generate_correct_grammatical_spelling_errors_button = gr.Button("Correct Grammatical and Spelling Errors")
correct_grammatical_spelling_errors_output = gr.Textbox(label="Correct Grammatical and Spelling Errors 修訂文法與拼字錯誤")
paragraph_correct_grammatical_spelling_errors_input = gr.Textbox(label="Paragraph")
gr.Markdown("## 10. Refine Paragraph 段落改善建議")
default_user_refine_paragraph_prompt = """
I need assistance with revising a paragraph. Please "Refine" the "Revised Version 1" and immediately "Provide Explanations" for each suggestion you made.
- Revised Version 1 (for correction): paragraph_ai_modification(split by 標點符號)
- Do not modify the sentence: topicSentence"
- Make sure any revised vocabulary aligns with the level.
- Guidelines for Length and Complexity:
Please keep explanations concise and straightforward,
avoiding overly technical language.
The response should strictly be in the below JSON format and nothing else:
{
"Suggestions and Explanations": [
{ "Origin": "#original_text_1", "Suggestion": "#suggestion_1", "Explanation": "#explanation_1" },
{ "Origin": "#original_text_2", "Suggestion": "#suggestion_2", "Explanation": "#explanation_2" },
... ],
"Revised Paragraph": "#revised_paragraph_v2"
}
"""
user_refine_paragraph_prompt = gr.Textbox(label="Refine Paragraph Prompt", value=default_user_refine_paragraph_prompt)
generate_refine_paragraph_button = gr.Button("Refine Paragraph")
paragraph_refine_output = gr.Textbox(label="Refine Paragraph 段落改善建議")
generate_topics_button.click(
fn=generate_topics,
inputs=[
model,
max_tokens,
sys_content_input,
scenario_input,
eng_level_input,
user_generate_topics_prompt
],
outputs=topic_output
)
generate_points_button.click(
fn=generate_points,
inputs=[
model,
max_tokens,
sys_content_input,
scenario_input,
eng_level_input,
topic_input,
user_generate_points_prompt
],
outputs=points_output
)
generate_topic_sentences_button.click(
fn=generate_topic_sentences,
inputs=[
model,
max_tokens,
sys_content_input,
scenario_input,
eng_level_input,
topic_input,
points_input,
user_generate_topic_sentences_prompt
],
outputs=topic_sentence_output
)
generate_supporting_sentences_button.click(
fn=generate_supporting_sentences,
inputs=[
model,
max_tokens,
sys_content_input,
scenario_input,
eng_level_input,
topic_input,
points_input,
topic_sentence_input,
user_generate_supporting_sentences_prompt
],
outputs=supporting_sentences_output
)
generate_conclusion_sentence_button.click(
fn=generate_conclusion_sentences,
inputs=[
model,
max_tokens,
sys_content_input,
scenario_input,
eng_level_input,
topic_input,
points_input,
topic_sentence_input,
user_generate_conclusion_sentence_prompt
],
outputs=conclusion_sentence_output
)
generate_paragraph_button.click(
fn=generate_paragraph,
inputs=[
topic_sentence_input,
supporting_sentences_input,
conclusion_sentence_input
],
outputs=paragraph_output
)
generate_paragraph_evaluate_button.click(
fn=generate_paragraph_evaluate,
inputs=[
paragraph_input,
user_generate_paragraph_evaluate_prompt
],
outputs=paragraph_evaluate_output
)
generate_correct_grammatical_spelling_errors_button.click(
fn=generate_correct_grammatical_spelling_errors,
inputs=[
eng_level_input,
paragraph_input,
user_correct_grammatical_spelling_errors_prompt
],
outputs=correct_grammatical_spelling_errors_output
)
generate_refine_paragraph_button.click(
fn=generate_refine_paragraph,
inputs=[
eng_level_input,
paragraph_correct_grammatical_spelling_errors_input,
user_refine_paragraph_prompt
],
outputs=paragraph_refine_output
)
demo.launch()
|