-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
304 lines (265 loc) · 11.2 KB
/
main.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
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
import gradio as gr
from utils import *
with gr.Blocks(theme="gstaff/xkcd", title="Esslay") as demo:
gr.HTML(
"""
<h1 style="font-size: 300%"><b>Esslay</b></h1>
<h3 style="color: gray">Slay your essays with Esslay<h3>
<p style="font-size: 83%; color: gray">Read the essay guide <a href="https://gp.sg/general-paper-essay-examples-notes.pdf" target="_blank">here<a/></p>
"""
)
with gr.Row():
qn_box = gr.Textbox(label="Essay question", scale=6)
submit_btn = gr.Button("Submit", scale=1)
btn_names = [f"{i}: For" for i in range(1,6)]
btn_names += [f"{i}: Against" for i in range(6,11)]
# print(btn_names)
point_btns = [None] * 10
example_gen_btns = [None] * 10
example_feedback_btns = [None] * 10
para_feedback_btns = [None] * 10
para_gen_btns = [None] * 10
point_textboxes = [None] * 10
point_feedback = [None] * 10
fixed_point_textboxes = [None] * 10
example_textboxes = [None] * 10
example_feedback = [None] * 10
paragraph_textboxes = [None] * 10
paragraph_feedback = [None] * 10
# points = []
# point_feedbacks = []
# paragraphs = []
# paragraph_feedbacks = []
# examples = []
# example_feedbacks = []
with gr.Column(visible=False) as output_col:
with gr.Tab("Points overview"):
for i, x in enumerate(btn_names):
fixed_point_textboxes[i] = gr.Textbox(label=f"Point {x}")
with gr.Row():
intro_textbox = gr.Textbox(label="Introduction", interactive=True)
intro_feedback = gr.Textbox(label="Introduction feedback", interactive=False)
with gr.Row():
intro_gen_btn = gr.Button("Generate introduction")
intro_feedback_btn = gr.Button("Get feedback on introduction")
with gr.Row():
conclusion_textbox = gr.Textbox(label="Conclusion", interactive=True)
conclusion_feedback = gr.Textbox(label="Conclusion feedback", interactive=False)
with gr.Row():
conclusion_gen_btn = gr.Button("Generate conclusion")
conclusion_feedback_btn = gr.Button("Get feedback on conclusion")
for i, x in enumerate(btn_names):
with gr.Tab(f"Point {x}"):
with gr.Column():
with gr.Row():
point_textboxes[i] = gr.Textbox(label=f"Point {x}", interactive=True)
point_feedback[i] = gr.Textbox(label="Point feedback", interactive=False)
with gr.Row():
point_btns[i] = gr.Button("Get feedback on point")
with gr.Row():
example_textboxes[i] = gr.Textbox(label="Example", interactive=True)
example_feedback[i] = gr.Textbox(label="Example feedback", interactive=False)
with gr.Row():
example_gen_btns[i] = gr.Button("Generate example")
example_feedback_btns[i] = gr.Button("Get feedback on example")
with gr.Row():
paragraph_textboxes[i] = gr.Textbox(label="Paragraph", interactive=True)
paragraph_feedback[i] = gr.Textbox(label="Paragraph feedback", interactive=False)
with gr.Row():
para_gen_btns[i] = gr.Button("Generate paragraph")
para_feedback_btns[i] = gr.Button("Get feedback on paragraph")
def submit(topic_sentence):
if len(topic_sentence) == 0:
raise gr.Error("Please enter an essay question.")
# call api
pos_points = get_points(topic_sentence, support=True)
neg_points = get_points(topic_sentence, support=False)
points = pos_points + neg_points
res = {x:gr.update(value=points[i], lines=len(points[i])//97 + (1 if len(points[i])%97 else 0)) for i, x in enumerate(point_textboxes)}
res2 = {x:points[i] for i, x in enumerate(fixed_point_textboxes)}
return {
output_col: gr.update(visible=True),
**res,
**res2
}
submit_btn.click(
submit,
[qn_box],
[output_col, *point_textboxes, *fixed_point_textboxes],
)
def gen_point_feedback(i):
def feedback_point(topic_sentence, point):
if len(point) == 0:
raise gr.Error("Please enter an point.")
if len(topic_sentence) == 0:
raise gr.Error("Please enter an essay question.")
res = get_feedback_point(topic_sentence=topic_sentence, point=point)
# res = f"TEST EV PT {i}"
return{
point_feedback[i]: res
}
return feedback_point
for i in range(10):
func = gen_point_feedback(i)
point_btns[i].click(
func,
[qn_box, point_textboxes[i]],
[point_feedback[i]]
)
def gen_example_gen(i):
def gen_example(topic_sentence, point, old_example="", feedback=""):
if len(point) == 0:
raise gr.Error("Please enter an point.")
if len(topic_sentence) == 0:
raise gr.Error("Please enter an essay question.")
res = get_example(topic_sentence=topic_sentence, point=point, old_example=old_example, feedback=feedback)
# res = f"TEST EG {i}"
return{
example_textboxes[i]: res
}
return gen_example
for i in range(10):
func = gen_example_gen(i)
example_gen_btns[i].click(
func,
[qn_box, point_textboxes[i], example_textboxes[i], example_feedback[i]],
[example_textboxes[i]]
)
def gen_example_feedback(i):
def feedback_example(topic_sentence, example, point):
if len(point) == 0:
raise gr.Error("Please enter an point.")
if len(example) == 0:
raise gr.Error("Please enter an example.")
if len(topic_sentence) == 0:
raise gr.Error("Please enter an essay question.")
res = get_feedback_example(topic_sentence=topic_sentence, example=example, point=point)
# res = f"TEST EV {i}"
return{
example_feedback[i]: res
}
return feedback_example
for i in range(10):
func = gen_example_feedback(i)
example_feedback_btns[i].click(
func,
[qn_box, example_textboxes[i], point_textboxes[i]],
[example_feedback[i]]
)
def gen_para_gen(i):
def gen_para(topic_sentence, point, example, old_para="", feedback=""):
if len(point) == 0:
raise gr.Error("Please enter an point.")
if len(topic_sentence) == 0:
raise gr.Error("Please enter an essay question.")
if len(example) == 0:
raise gr.Error("Please enter an example.")
res = get_paragraph(topic_sentence=topic_sentence, point=point, example=example, old_para=old_para, feedback=feedback)
# res = f"TEST EG PARA {i}"
return{
paragraph_textboxes[i]: res
}
return gen_para
for i in range(10):
func = gen_para_gen(i)
para_gen_btns[i].click(
func,
[qn_box, point_textboxes[i], example_textboxes[i], paragraph_textboxes[i], example_feedback[i]],
[paragraph_textboxes[i]]
)
def gen_para_feedback(i):
def feedback_para(topic_sentence, paragraph):
if len(paragraph) == 0:
raise gr.Error("Please enter an paragraph.")
if len(topic_sentence) == 0:
raise gr.Error("Please enter an essay question.")
res = get_feedback_paragraph(topic_sentence=topic_sentence, paragraph=paragraph)
# res = f"TEST EV PARA {i}"
return{
paragraph_feedback[i]: res
}
return feedback_para
for i in range(10):
func = gen_para_feedback(i)
para_feedback_btns[i].click(
func,
[qn_box, paragraph_textboxes[i]],
[paragraph_feedback[i]]
)
def gen_intro(topic_sentence, *points, old_intro="", feedback=""):
if len(topic_sentence) == 0:
raise gr.Error("Please enter an essay question.")
res = get_introduction(topic_sentence=topic_sentence, main_points=list(points), old_intro=old_intro, feedback=feedback)
# res = f"TEST EG INTRO {i}"
return{
intro_textbox: res
}
intro_gen_btn.click(
gen_intro,
[qn_box, *fixed_point_textboxes, intro_textbox, intro_feedback],
[intro_textbox]
)
def gen_intro_feedback(topic_sentence, intro, *points):
if len(intro) == 0:
raise gr.Error("Please enter an introduction.")
if len(topic_sentence) == 0:
raise gr.Error("Please enter an essay question.")
res = get_feedback_introduction(topic_sentence=topic_sentence, intro=intro, main_points=list(points))
# res = f"TEST EV INTRO {i}"
return{
intro_feedback: res
}
intro_feedback_btn.click(
gen_intro_feedback,
[qn_box, intro_textbox, *fixed_point_textboxes],
[intro_feedback]
)
def gen_conclusion(topic_sentence, *points, old_conclusion="", feedback=""):
if len(topic_sentence) == 0:
raise gr.Error("Please enter an essay question.")
res = get_conclusion(topic_sentence=topic_sentence, main_points=list(points), old_conclusion=old_conclusion, feedback=feedback)
# res = f"TEST EG INTRO {i}"
return{
conclusion_textbox: res
}
conclusion_gen_btn.click(
gen_conclusion,
[qn_box, *fixed_point_textboxes, conclusion_textbox, conclusion_feedback],
[conclusion_textbox]
)
def gen_conclusion_feedback(topic_sentence, conclusion, *points):
if len(conclusion) == 0:
raise gr.Error("Please enter an introduction.")
if len(topic_sentence) == 0:
raise gr.Error("Please enter an essay question.")
res = get_feedback_conclusion(topic_sentence=topic_sentence, conclusion=conclusion, main_points=list(points))
# res = f"TEST EV INTRO {i}"
return{
conclusion_feedback: res
}
conclusion_feedback_btn.click(
gen_conclusion_feedback,
[qn_box, conclusion_textbox, *fixed_point_textboxes],
[conclusion_feedback]
)
# def show_example(i):
# example_vis[i] = not example_vis[i]
# if example_vis[i]:
# return {
# example_boxes[i]: gr.update(visible=False),
# btns[i]: "Show example",
# }
# return {
# example_boxes[i]: gr.update(visible=True),
# btns[i]: "Hide example",
# }
# on button click show example
# funcs = [None] * 10
# for i in range(10):
# funcs[i] = lambda: show_example(i)
# btns[i].click(
# funcs[i],
# [],
# [*example_boxes, *btns],
# )
demo.launch(share=True)