youngtsai commited on
Commit
f55a64f
·
1 Parent(s): 37be1f6
Files changed (2) hide show
  1. app.py +114 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import pandas as pd
4
+ from openai import OpenAI
5
+
6
+ # 設置 OpenAI API 客戶端
7
+ openai_api_key = os.getenv("OPENAI_API_KEY")
8
+ openai_client = OpenAI(api_key=openai_api_key)
9
+
10
+ def extract_article_from_content(article_text):
11
+ start_markers = ["新文章:", "New Article:", "Here it is:"]
12
+ end_marker = "\nThank you"
13
+ for start_marker in start_markers:
14
+ start_index = article_text.find(start_marker)
15
+ if start_index != -1:
16
+ start_index += len(start_marker)
17
+ while article_text[start_index] in "\n":
18
+ start_index += 1
19
+ end_index = article_text.find(end_marker, start_index)
20
+ if end_index != -1:
21
+ return article_text[start_index:end_index].rstrip()
22
+ return article_text[start_index:].rstrip()
23
+ return article_text
24
+
25
+ def validate_article(generated_article, lesson_words, base_chars, original_word_count):
26
+ clean_article = "".join(char for char in generated_article if char not in ",。!?;:「」『』()《》【】'\n'")
27
+ not_every_new_word_is_used = not all(word in clean_article for word in lesson_words.split())
28
+ word_out_of_range = not set(clean_article).issubset(set(lesson_words + base_chars))
29
+ new_word_count = len(clean_article)
30
+ word_count_error = not (0.9 * original_word_count <= new_word_count <= 1.1 * original_word_count)
31
+ return not_every_new_word_is_used, word_out_of_range, word_count_error
32
+
33
+ def generate_new_article(lesson_words, original_article, original_word_count, base_chars):
34
+ attempt = 0
35
+ max_attempts = 3
36
+ generated_article = ""
37
+
38
+ while attempt < max_attempts:
39
+ attempt += 1
40
+ prompt = f"""
41
+ Please write a new and original Chinese article tailored for first-grade students. Here's a summary of the key points that you should follow:
42
+
43
+ Use Traditional Chinese Characters: The article should be written in Traditional Chinese, not Simplified Chinese.
44
+
45
+ Adherence to the Original Article: The new creation should closely follow the spirit, style, and rhythmic pattern of the provided original article. The number of words, excluding punctuation marks, should be similar to that of the original, approximately {original_word_count} words.
46
+
47
+ Incorporate "New Words": Every word listed under "new words" must be used in the article. These words are: {lesson_words}.
48
+
49
+ Utilize the "Word Library": Additional words required for the article can be selected from the provided "word library," which includes: {base_chars}.
50
+
51
+ Restriction on Vocabulary: Do not use any words outside the "new words" or the "word library".
52
+
53
+ Originality: The new article must be unique and original, not a copy of the original work.
54
+
55
+ "Original Article" for Reference: The example provided is {original_article}。This article serves as a model for the spirit, style, and rhythmic pattern to be emulated.
56
+ """
57
+
58
+ response = openai_client.Completion.create(
59
+ model="text-davinci-003",
60
+ prompt=prompt,
61
+ max_tokens=1000
62
+ )
63
+
64
+ generated_text = response.choices[0].text.strip()
65
+ generated_article = extract_article_from_content(generated_text)
66
+
67
+ not_every_new_word_is_used, word_out_of_range, word_count_error = validate_article(
68
+ generated_article, lesson_words, base_chars, original_word_count)
69
+
70
+ if not not_every_new_word_is_used and not word_out_of_range and not word_count_error:
71
+ break
72
+
73
+ return generated_article
74
+
75
+ def load_csv(file):
76
+ df = pd.read_csv(file)
77
+ if not df.empty:
78
+ first_row = df.iloc[0]
79
+ return first_row['lesson_words'], first_row['original_article'], first_row['original_word_count'], first_row['base_chars']
80
+ return "", "", 0, ""
81
+
82
+ def main():
83
+ with gr.Blocks() as demo:
84
+ gr.Markdown("Generate New Article")
85
+
86
+ with gr.Tab("Manual Input"):
87
+ lesson_words_input = gr.Textbox(label="Lesson Words (以空格分隔)")
88
+ original_article_input = gr.Textarea(label="Original Article")
89
+ original_word_count_input = gr.Number(label="Original Word Count")
90
+ base_chars_input = gr.Textbox(label="Base Characters")
91
+ generate_button = gr.Button("Generate Article")
92
+
93
+ with gr.Tab("CSV Input"):
94
+ csv_file_input = gr.File(label="Upload CSV file (Columns: lesson_words, original_article, original_word_count, base_chars)")
95
+ load_button = gr.Button("Load from CSV")
96
+
97
+ output_text = gr.Textbox(label="Generated Article")
98
+
99
+ generate_button.click(
100
+ generate_new_article,
101
+ inputs=[lesson_words_input, original_article_input, original_word_count_input, base_chars_input],
102
+ outputs=output_text
103
+ )
104
+
105
+ load_button.click(
106
+ load_csv,
107
+ inputs=[csv_file_input],
108
+ outputs=[lesson_words_input, original_article_input, original_word_count_input, base_chars_input]
109
+ )
110
+
111
+ demo.launch()
112
+
113
+ if __name__ == "__main__":
114
+ main()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ pandas
3
+ openai>=1.0.0