-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
182 lines (169 loc) · 7.23 KB
/
app.py
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
import openai
import streamlit as st
st.header("ChatGPT Prompt Generator")
st.markdown("""
This app allows its users...
1. To generate prompts for ChatGPT to make questions for language learners.
1. To try sending the prompts to InstructGPT, a precedent model of ChatGPT.
""")
target_language = st.sidebar.header("PARAMETERS")
target_language = st.sidebar.radio(
"Select target language",
(
'English',
'Japanese',
))
reader_student = st.sidebar.radio(
"Select readers/students level",
# FIXME: learners より students のほうが精度良いかも
options=(
'Students',
'Elementary learners',
'Pre-intermediate learners',
'Intermediate learners',
'Advanced learners',
'Highly educated native speakers',
),).lower()
subject = reader_student + " of " + target_language
answer_request = st.sidebar.radio("Do you need answers/explanations?", (
"Yes, please.",
"No, thank you.",
))
# リクエストごとに異なる最適なクエリを作成する。
st.sidebar.subheader("Select the request type")
request_type = st.sidebar.radio("Select request type", (
"Vocab quizzes",
"Difficult words",
"Cloze tests",
"Comprehension tasks",
"Discussion topics",
"Word/phrase explanations",
"Rewriting",
"Summarizing",
))
answer_request=answer_request[0]=="Y"
request = ""
base_label = f"N {request_type.lower()}"
request_answers_str = " (please tell me the answers at the bottom)" if answer_request else ""
if request_type == "Vocab quizzes":
# - [x] subject
col1, col2 = st.sidebar.columns(2)
n_vocab_quizzes = col1.slider(label=base_label, min_value=2, max_value=6, value=3)
m_choice = col2.slider(label= "with M choices", min_value=2, max_value=6, value=4)
x_nym = st.sidebar.radio(label= "X-nyms", options = ["a synonym", "an antonym"], horizontal=True)
xx_nym = f"{m_choice-1} antonyms" if x_nym=="a synonym" else f"{m_choice-1} synonyms"
request = " ".join([
f"make {n_vocab_quizzes} vocabulary-building quizzes,",
f"where {subject} must distinguish {x_nym} from the other {xx_nym},",
f"reading the following text{request_answers_str}.",
])
elif request_type == "Difficult words":
# answer_request は不要
# - [x] subject
options = ["all"]+list(range(3, 21))
n_difficult_words = st.sidebar.select_slider(label=base_label + " (all, 3, 4, 5,...20)", options=options, value="all")
request = " ".join([
f"find {n_difficult_words} words that {subject} don't know,",
f"reading the following text{request_answers_str}.",
])
elif request_type == "Comprehension tasks":
col1, col2 = st.sidebar.columns(2)
n_comprehension = col1.slider(label=base_label, min_value=3, max_value=10, value=3)
m_choice = col2.slider(label= "with M choices", min_value=2, max_value=6, value=4)
request = " ".join([
f"make {n_comprehension} comprehension tasks with {m_choice} choices",
f"for {subject},",
f"reading the following text{request_answers_str}."
])
elif request_type == "Discussion topics":
col1, col2 = st.sidebar.columns(2)
n_discussion_topics = col1.slider(label=base_label, min_value=3, max_value=10, value=3)
m_minutes = col2.slider(label= "with M minutes", min_value=5, max_value=60, value=15, step=5)
request = " ".join([
f"suggest {n_discussion_topics} discussion topics",
f"that {subject} are supposed to finish in {m_minutes} minutes,",
f"reading the following text{request_answers_str}.",
])
elif request_type=="Cloze tests":
col1, col2 = st.sidebar.columns(2)
n_cloze_test = col1.slider(label=base_label, min_value=3, max_value=10, value=3)
m_choice = col2.slider(label= "with M choices", min_value=2, max_value=6, value=4)
pos = st.sidebar.radio(label= "PoS", options = ["preposition", "noun", "adjective", "verb"], horizontal=True)
request = " ".join([
f"make {n_cloze_test} cloze tests, each of which has a blank that {subject} are supposed to fill in",
f"selecting a correct {pos} from {m_choice} choices",
f"reading the following text{request_answers_str}.",
])
elif request_type == "Word/phrase explanations":
words_phrases = st.sidebar.text_input("Word/phrase")
st.info(f"answer request is disabled for this request_type: {request_type}")
request_answers_str = ""
request = " ".join([
f'explain the use of "{words_phrases}" and give some other examples,',
f"reading the following text{request_answers_str}.",
])
elif request_type == "Rewriting":
st.info(f"answer request is disabled for this request_type: {request_type}")
request_answers_str = ""
request = " ".join([
f'rewrite the text,',
f"reading the following text{request_answers_str}.",
])
elif request_type == "Summarizing":
st.info(f"answer request is disabled for this request_type: {request_type}")
request_answers_str = ""
request = " ".join([
f'summarize the text,',
f"reading the following text{request_answers_str}.",
])
else:
raise NotImplementedError
text_input = st.sidebar.text_area(label="Reference text input", value="<You can edit here on the sidebar>")
text_input = "\n\n".join(text_input.split("\n"))
reference_txt=f"\n\n{text_input}"
# Please make questions each of which has a blank that students are supposed to fill in selecting a correct preposition such as for and on from four choices, reading the following article.
# - [ ] 質問
# - [ ] 単語の説明
# - [ ] 語義の説明
# - [ ] N個の用例の要求
# - [ ] 文法性判断と説明
# - [ ] 書き換え
# - [ ] 要約
# - [ ] 固有名詞除外
# - [ ] モード(Speaking/Writing)
# - [ ] 単語数(上限不明)
# - [ ] 参考文献
st.subheader("Prompt")
prompt_a = " ".join(["Please", request])
prompt = prompt_a + reference_txt
st.markdown(prompt + "\n\n")
st.markdown("")
if st.button('Submit it to InstructGPT'):
st.subheader("Output")
st.info(" ".join([
"The following output is generated using InstructGPT, a precedent model of ChatGPT.",
"The response might not be complete due to the lack of computational resources."
"It may cost a couple of time to get a ready-to-use response, even with the ChatGPT.",
"You can regenerate the responses from ChatGPT on the latest application.",
])
)
api_params = dict(
model='text-davinci-003', # InstructGPT
# temperature=0.5, # synonym は0.5くらいが結構よさそう
temperature=0.3,
# temperature=0.7,
# temperature=0.9,
max_tokens= 1024, #
# max_tokens=512, # これでも1段落で8問程度はいける
# max_tokens=256, # これだと3問程度
top_p=1,
frequency_penalty=0,
presence_penalty=0,
)
openai.api_key = st.secrets["OPENAI_TOKEN"]
response = openai.Completion.create(**api_params, prompt=prompt)
st.code(response['choices'][0]['text'], language=None)
st.code(str(api_params), language=None)
# st.markdown(body=(response['choices'][0]['text']).replace("\n", "\n\n"))
st.sidebar.markdown("If you have any questions, please email the following address.")
st.sidebar.markdown("kishiyama.t@gmail.com")