Spaces:
Runtime error
Runtime error
Commit
Β·
dc6820a
1
Parent(s):
0446903
Update demo.py
Browse files
demo.py
CHANGED
@@ -25,21 +25,22 @@ firebase_current_ref = None
|
|
25 |
|
26 |
|
27 |
base_url = "https://skapi.polyglot-edu.com/"
|
28 |
-
levels = ["
|
|
|
29 |
|
30 |
def get_level_mapping(level):
|
31 |
if level is None:
|
32 |
raise gr.Error("Please select a level.")
|
33 |
|
34 |
return {
|
35 |
-
"
|
36 |
-
"
|
37 |
-
"
|
38 |
-
"
|
39 |
-
"
|
40 |
}[level]
|
41 |
|
42 |
-
def generate_fill_gaps(original_text, level, number_of_words, number_of_gaps, number_of_distractors, temperature):
|
43 |
"""
|
44 |
Generate a fill-gaps question from a given text.
|
45 |
|
@@ -56,6 +57,8 @@ def generate_fill_gaps(original_text, level, number_of_words, number_of_gaps, nu
|
|
56 |
The number of distractors to generate for each gap.
|
57 |
temperature : float
|
58 |
The temperature for the generation.
|
|
|
|
|
59 |
|
60 |
Returns
|
61 |
-------
|
@@ -77,7 +80,8 @@ def generate_fill_gaps(original_text, level, number_of_words, number_of_gaps, nu
|
|
77 |
"n_o_w": int(number_of_words),
|
78 |
"n_o_g": int(number_of_gaps),
|
79 |
"n_o_d": int(number_of_distractors),
|
80 |
-
"temperature": int(temperature) * 0.2
|
|
|
81 |
}
|
82 |
|
83 |
output = ""
|
@@ -107,7 +111,7 @@ def generate_fill_gaps(original_text, level, number_of_words, number_of_gaps, nu
|
|
107 |
"flagged": False,
|
108 |
})
|
109 |
|
110 |
-
def generate_open_question(original_text, level, temperature):
|
111 |
"""
|
112 |
Generate an open question from a given text.
|
113 |
|
@@ -118,6 +122,12 @@ def generate_open_question(original_text, level, temperature):
|
|
118 |
The original text from which to generate the open question.
|
119 |
temperature : float
|
120 |
The temperature for the generation.
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
Returns
|
123 |
-------
|
@@ -128,7 +138,10 @@ def generate_open_question(original_text, level, temperature):
|
|
128 |
input_json = {
|
129 |
"text": original_text,
|
130 |
"level": get_level_mapping(level),
|
131 |
-
"temperature": int(temperature) * 0.2
|
|
|
|
|
|
|
132 |
}
|
133 |
|
134 |
output = ""
|
@@ -158,7 +171,7 @@ def generate_open_question(original_text, level, temperature):
|
|
158 |
"flagged": False,
|
159 |
})
|
160 |
|
161 |
-
def generate_multiplechoice(original_text, level,
|
162 |
"""
|
163 |
Generate a multiple-choice question from a given text.
|
164 |
|
@@ -167,14 +180,14 @@ def generate_multiplechoice(original_text, level, number_of_options, number_of_e
|
|
167 |
----------
|
168 |
original_text : str
|
169 |
The original text from which to generate the multiple-choice question.
|
170 |
-
number_of_options : int
|
171 |
-
The number of options to generate.
|
172 |
number_of_easy_distractors : int
|
173 |
The number of easy distractors to generate for each option.
|
174 |
number_of_distractors : int
|
175 |
The number of distractors to generate for each option.
|
176 |
temperature : float
|
177 |
The temperature for the generation.
|
|
|
|
|
178 |
|
179 |
Returns
|
180 |
-------
|
@@ -188,9 +201,12 @@ def generate_multiplechoice(original_text, level, number_of_options, number_of_e
|
|
188 |
"level": get_level_mapping(level),
|
189 |
"n_o_d": int(number_of_distractors),
|
190 |
"nedd": int(number_of_easy_distractors),
|
191 |
-
"temperature": int(temperature) * 0.2
|
|
|
192 |
}
|
193 |
|
|
|
|
|
194 |
output = ""
|
195 |
try:
|
196 |
response = requests.post(
|
@@ -278,15 +294,17 @@ def build_fill_gaps_interface():
|
|
278 |
with gr.Blocks(title="Fill-gaps") as demo:
|
279 |
with gr.Row():
|
280 |
with gr.Column(scale=5):
|
281 |
-
input_field = gr.TextArea(lines=10, max_lines=10, label="Input
|
|
|
282 |
submit_btn = gr.Button(value="Submit")
|
283 |
with gr.Column(scale=4):
|
284 |
-
output_text_length = gr.Radio(["β 150 Words", "β 250 Words", "β 350 Words"], label="Output text length", value="β 150 Words")
|
285 |
level = gr.Radio(levels, label="Level")
|
|
|
|
|
286 |
with gr.Row():
|
287 |
blanks = gr.Number(value=5, minimum=4, maximum=8, step=1, label="Number of blanks")
|
288 |
distractors = gr.Number(value=2, minimum=0, maximum=5, step=1, label="Number of distractors")
|
289 |
-
temperature = gr.Checkbox(value=False, label="Increase creativity (decreases preciseness)")
|
290 |
|
291 |
def update_numeric(output_text_length, blanks, distractors):
|
292 |
if output_text_length == "β 150 Words":
|
@@ -314,16 +332,11 @@ def build_fill_gaps_interface():
|
|
314 |
remove_preference_btn = gr.Button(value="Remove Preference")
|
315 |
downvote_btn = gr.Button(value="π Downvote")
|
316 |
flag_btn = gr.Button(value="β οΈ Flag")
|
317 |
-
|
318 |
-
|
319 |
-
comment_text = gr.TextArea(placeholder="Comment", label="Leave a comment about the generated text", scale=6)
|
320 |
-
comment_btn = gr.Button(value="π¬ Comment", scale=2)
|
321 |
-
|
322 |
-
submit_btn.click(generate_fill_gaps, [input_field, level, output_text_length, blanks, distractors, temperature], [output])
|
323 |
upvote_btn.click(like)
|
324 |
remove_preference_btn.click(neutral_like)
|
325 |
downvote_btn.click(dislike)
|
326 |
-
comment_btn.click(comment, [comment_text])
|
327 |
flag_btn.click(flag, [flag_btn], [flag_btn])
|
328 |
|
329 |
return demo
|
@@ -335,31 +348,22 @@ def build_multiplechoice_interface():
|
|
335 |
with gr.Blocks(title="Open Question") as demo:
|
336 |
with gr.Row():
|
337 |
with gr.Column(scale=5):
|
338 |
-
input_field = gr.TextArea(lines=10, max_lines=10, label="Input
|
|
|
339 |
submit_btn = gr.Button(value="Submit")
|
340 |
with gr.Column(scale=4):
|
341 |
level = gr.Radio(levels, label="Level")
|
|
|
342 |
with gr.Row():
|
343 |
-
options = gr.Number(value=4, minimum=2, maximum=8, step=1, label="Number of blanks", interactive=False)
|
344 |
easy_distractors = gr.Number(value=1, minimum=0, maximum=8, step=1, label="Number of easy distractors")
|
345 |
distractors = gr.Number(value=1, minimum=0, maximum=8, step=1, label="Number of distractors")
|
346 |
-
temperature = gr.Checkbox(value=False, label="Increase creativity (decreases preciseness)")
|
347 |
-
|
348 |
-
def update_options(options, easy_distractors, distractors):
|
349 |
-
distractors = clamp(distractors, 0, options)
|
350 |
-
easy_distractors = clamp(easy_distractors, 0, distractors)
|
351 |
-
|
352 |
-
return (
|
353 |
-
gr.Number(value=easy_distractors, minimum=0, maximum=distractors, label="Number of easy distractors"),
|
354 |
-
gr.Number(value=distractors, minimum=0, maximum=options, label="Number of distractors")
|
355 |
-
)
|
356 |
|
357 |
def update_distractors(easy_distractors, distractors):
|
358 |
easy_distractors = clamp(easy_distractors, 0, distractors)
|
359 |
|
360 |
return gr.Number(value=easy_distractors, minimum=0, maximum=distractors, label="Number of easy distractors")
|
361 |
|
362 |
-
options.change(update_options, [options, easy_distractors, distractors], [easy_distractors, distractors])
|
363 |
distractors.change(update_distractors, [easy_distractors, distractors], [easy_distractors])
|
364 |
|
365 |
with gr.Row():
|
@@ -369,16 +373,11 @@ def build_multiplechoice_interface():
|
|
369 |
remove_preference_btn = gr.Button(value="Remove Preference")
|
370 |
downvote_btn = gr.Button(value="π Downvote")
|
371 |
flag_btn = gr.Button(value="β οΈ Flag")
|
372 |
-
|
373 |
-
|
374 |
-
comment_text = gr.TextArea(placeholder="Comment", label="Comment", scale=6)
|
375 |
-
comment_btn = gr.Button(value="π¬ Comment", scale=2)
|
376 |
-
|
377 |
-
submit_btn.click(generate_multiplechoice, [input_field, level, options, easy_distractors, distractors, temperature], [output])
|
378 |
upvote_btn.click(like)
|
379 |
remove_preference_btn.click(neutral_like)
|
380 |
downvote_btn.click(dislike)
|
381 |
-
comment_btn.click(comment, [comment_text])
|
382 |
flag_btn.click(flag, [flag_btn], [flag_btn])
|
383 |
|
384 |
return demo
|
@@ -390,13 +389,18 @@ def build_open_question_interface():
|
|
390 |
with gr.Blocks(title="Multiple choice") as demo:
|
391 |
with gr.Row():
|
392 |
with gr.Column(scale=5):
|
393 |
-
input_field = gr.TextArea(lines=10, max_lines=10, label="Input
|
|
|
394 |
submit_btn = gr.Button(value="Submit")
|
395 |
with gr.Column(scale=4):
|
396 |
level = gr.Radio(levels, label="Level")
|
|
|
397 |
with gr.Row():
|
398 |
-
|
399 |
-
|
|
|
|
|
|
|
400 |
with gr.Row():
|
401 |
output = gr.TextArea(placeholder="Generated text", label="Output")
|
402 |
with gr.Row() as button_row:
|
@@ -404,26 +408,40 @@ def build_open_question_interface():
|
|
404 |
remove_preference_btn = gr.Button(value="Remove Preference")
|
405 |
downvote_btn = gr.Button(value="π Downvote")
|
406 |
flag_btn = gr.Button(value="β οΈ Flag")
|
407 |
-
gr.Markdown("<hr/>")
|
408 |
-
with gr.Row():
|
409 |
-
comment_text = gr.TextArea(placeholder="Comment", label="Comment", scale=6)
|
410 |
-
comment_btn = gr.Button(value="π¬ Comment", scale=2)
|
411 |
|
412 |
-
submit_btn.click(generate_open_question, [input_field, level, temperature], [output])
|
413 |
upvote_btn.click(like)
|
414 |
remove_preference_btn.click(neutral_like)
|
415 |
downvote_btn.click(dislike)
|
416 |
-
comment_btn.click(comment, [comment_text])
|
417 |
flag_btn.click(flag, [flag_btn], [flag_btn])
|
418 |
|
419 |
return demo
|
420 |
|
421 |
def build_demo():
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
427 |
|
428 |
if __name__ == "__main__":
|
429 |
build_demo().launch(share=False)
|
|
|
25 |
|
26 |
|
27 |
base_url = "https://skapi.polyglot-edu.com/"
|
28 |
+
levels = ["Primary School", "Middle School", "High School", "College", "Academy"]
|
29 |
+
languages = ["English", "Italian", "French", "German", "Spanish"]
|
30 |
|
31 |
def get_level_mapping(level):
|
32 |
if level is None:
|
33 |
raise gr.Error("Please select a level.")
|
34 |
|
35 |
return {
|
36 |
+
"Primary School": 0,
|
37 |
+
"Middle School": 1,
|
38 |
+
"High School": 2,
|
39 |
+
"College": 3,
|
40 |
+
"Academy": 4,
|
41 |
}[level]
|
42 |
|
43 |
+
def generate_fill_gaps(original_text, level, number_of_words, number_of_gaps, number_of_distractors, temperature, language):
|
44 |
"""
|
45 |
Generate a fill-gaps question from a given text.
|
46 |
|
|
|
57 |
The number of distractors to generate for each gap.
|
58 |
temperature : float
|
59 |
The temperature for the generation.
|
60 |
+
language : str
|
61 |
+
The language of the generated text.
|
62 |
|
63 |
Returns
|
64 |
-------
|
|
|
80 |
"n_o_w": int(number_of_words),
|
81 |
"n_o_g": int(number_of_gaps),
|
82 |
"n_o_d": int(number_of_distractors),
|
83 |
+
"temperature": int(temperature) * 0.2,
|
84 |
+
"language": language
|
85 |
}
|
86 |
|
87 |
output = ""
|
|
|
111 |
"flagged": False,
|
112 |
})
|
113 |
|
114 |
+
def generate_open_question(original_text, level, temperature, language, question_type, question_category):
|
115 |
"""
|
116 |
Generate an open question from a given text.
|
117 |
|
|
|
122 |
The original text from which to generate the open question.
|
123 |
temperature : float
|
124 |
The temperature for the generation.
|
125 |
+
language : str
|
126 |
+
The language of the generated text.
|
127 |
+
question_type : str
|
128 |
+
The type of the question.
|
129 |
+
question_category : str
|
130 |
+
The category of the question.
|
131 |
|
132 |
Returns
|
133 |
-------
|
|
|
138 |
input_json = {
|
139 |
"text": original_text,
|
140 |
"level": get_level_mapping(level),
|
141 |
+
"temperature": int(temperature) * 0.2,
|
142 |
+
"language": language,
|
143 |
+
"question_type": question_type,
|
144 |
+
"question_category": question_category
|
145 |
}
|
146 |
|
147 |
output = ""
|
|
|
171 |
"flagged": False,
|
172 |
})
|
173 |
|
174 |
+
def generate_multiplechoice(original_text, level, number_of_easy_distractors, number_of_distractors, temperature, language):
|
175 |
"""
|
176 |
Generate a multiple-choice question from a given text.
|
177 |
|
|
|
180 |
----------
|
181 |
original_text : str
|
182 |
The original text from which to generate the multiple-choice question.
|
|
|
|
|
183 |
number_of_easy_distractors : int
|
184 |
The number of easy distractors to generate for each option.
|
185 |
number_of_distractors : int
|
186 |
The number of distractors to generate for each option.
|
187 |
temperature : float
|
188 |
The temperature for the generation.
|
189 |
+
language : str
|
190 |
+
The language of the generated text.
|
191 |
|
192 |
Returns
|
193 |
-------
|
|
|
201 |
"level": get_level_mapping(level),
|
202 |
"n_o_d": int(number_of_distractors),
|
203 |
"nedd": int(number_of_easy_distractors),
|
204 |
+
"temperature": int(temperature) * 0.2,
|
205 |
+
"language": language
|
206 |
}
|
207 |
|
208 |
+
print(input_json)
|
209 |
+
|
210 |
output = ""
|
211 |
try:
|
212 |
response = requests.post(
|
|
|
294 |
with gr.Blocks(title="Fill-gaps") as demo:
|
295 |
with gr.Row():
|
296 |
with gr.Column(scale=5):
|
297 |
+
input_field = gr.TextArea(lines=10, max_lines=10, label="Input Educational resource text or URL",
|
298 |
+
info="Note: If the input resource is too long, the API will return an error. If the input is an URL, it must be a valid URL to webpage or a file.")
|
299 |
submit_btn = gr.Button(value="Submit")
|
300 |
with gr.Column(scale=4):
|
|
|
301 |
level = gr.Radio(levels, label="Level")
|
302 |
+
language = gr.Dropdown(languages, label="Language", value="English")
|
303 |
+
output_text_length = gr.Radio(["β 150 Words", "β 250 Words", "β 350 Words"], label="Output text length", value="β 150 Words")
|
304 |
with gr.Row():
|
305 |
blanks = gr.Number(value=5, minimum=4, maximum=8, step=1, label="Number of blanks")
|
306 |
distractors = gr.Number(value=2, minimum=0, maximum=5, step=1, label="Number of distractors")
|
307 |
+
temperature = gr.Checkbox(value=False, label="Increase creativity (decreases preciseness)", visible=False)
|
308 |
|
309 |
def update_numeric(output_text_length, blanks, distractors):
|
310 |
if output_text_length == "β 150 Words":
|
|
|
332 |
remove_preference_btn = gr.Button(value="Remove Preference")
|
333 |
downvote_btn = gr.Button(value="π Downvote")
|
334 |
flag_btn = gr.Button(value="β οΈ Flag")
|
335 |
+
|
336 |
+
submit_btn.click(generate_fill_gaps, [input_field, level, output_text_length, blanks, distractors, temperature, language], [output])
|
|
|
|
|
|
|
|
|
337 |
upvote_btn.click(like)
|
338 |
remove_preference_btn.click(neutral_like)
|
339 |
downvote_btn.click(dislike)
|
|
|
340 |
flag_btn.click(flag, [flag_btn], [flag_btn])
|
341 |
|
342 |
return demo
|
|
|
348 |
with gr.Blocks(title="Open Question") as demo:
|
349 |
with gr.Row():
|
350 |
with gr.Column(scale=5):
|
351 |
+
input_field = gr.TextArea(lines=10, max_lines=10, label="Input Educational resource text or URL",
|
352 |
+
info="Note: If the input resource is too long, the API will return an error. If the input is an URL, it must be a valid URL to webpage or a file.")
|
353 |
submit_btn = gr.Button(value="Submit")
|
354 |
with gr.Column(scale=4):
|
355 |
level = gr.Radio(levels, label="Level")
|
356 |
+
language = gr.Dropdown(languages, label="Language", value="English")
|
357 |
with gr.Row():
|
|
|
358 |
easy_distractors = gr.Number(value=1, minimum=0, maximum=8, step=1, label="Number of easy distractors")
|
359 |
distractors = gr.Number(value=1, minimum=0, maximum=8, step=1, label="Number of distractors")
|
360 |
+
temperature = gr.Checkbox(value=False, label="Increase creativity (decreases preciseness)", visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
361 |
|
362 |
def update_distractors(easy_distractors, distractors):
|
363 |
easy_distractors = clamp(easy_distractors, 0, distractors)
|
364 |
|
365 |
return gr.Number(value=easy_distractors, minimum=0, maximum=distractors, label="Number of easy distractors")
|
366 |
|
|
|
367 |
distractors.change(update_distractors, [easy_distractors, distractors], [easy_distractors])
|
368 |
|
369 |
with gr.Row():
|
|
|
373 |
remove_preference_btn = gr.Button(value="Remove Preference")
|
374 |
downvote_btn = gr.Button(value="π Downvote")
|
375 |
flag_btn = gr.Button(value="β οΈ Flag")
|
376 |
+
|
377 |
+
submit_btn.click(generate_multiplechoice, [input_field, level, easy_distractors, distractors, temperature, language], [output])
|
|
|
|
|
|
|
|
|
378 |
upvote_btn.click(like)
|
379 |
remove_preference_btn.click(neutral_like)
|
380 |
downvote_btn.click(dislike)
|
|
|
381 |
flag_btn.click(flag, [flag_btn], [flag_btn])
|
382 |
|
383 |
return demo
|
|
|
389 |
with gr.Blocks(title="Multiple choice") as demo:
|
390 |
with gr.Row():
|
391 |
with gr.Column(scale=5):
|
392 |
+
input_field = gr.TextArea(lines=10, max_lines=10, label="Input Educational resource text or URL",
|
393 |
+
info="Note: If the input resource is too long, the API will return an error. If the input is an URL, it must be a valid URL to webpage or a file.")
|
394 |
submit_btn = gr.Button(value="Submit")
|
395 |
with gr.Column(scale=4):
|
396 |
level = gr.Radio(levels, label="Level")
|
397 |
+
language = gr.Dropdown(languages, label="Language", value="English")
|
398 |
with gr.Row():
|
399 |
+
question_type = gr.Dropdown(["Open", "ShortAnswer", "TrueFalse"], label="Question Type", value="Open", type="index")
|
400 |
+
question_category = gr.Dropdown(
|
401 |
+
["Factual Knowledge", "Understanding of Concepts", "Application of Skills", "Analysys And Evaluation"],
|
402 |
+
label="Question Category", value="Factual Knowledge", type="index")
|
403 |
+
temperature = gr.Checkbox(value=False, label="Increase creativity (decreases preciseness)", visible=False)
|
404 |
with gr.Row():
|
405 |
output = gr.TextArea(placeholder="Generated text", label="Output")
|
406 |
with gr.Row() as button_row:
|
|
|
408 |
remove_preference_btn = gr.Button(value="Remove Preference")
|
409 |
downvote_btn = gr.Button(value="π Downvote")
|
410 |
flag_btn = gr.Button(value="β οΈ Flag")
|
|
|
|
|
|
|
|
|
411 |
|
412 |
+
submit_btn.click(generate_open_question, [input_field, level, temperature, language, question_type, question_category], [output])
|
413 |
upvote_btn.click(like)
|
414 |
remove_preference_btn.click(neutral_like)
|
415 |
downvote_btn.click(dislike)
|
|
|
416 |
flag_btn.click(flag, [flag_btn], [flag_btn])
|
417 |
|
418 |
return demo
|
419 |
|
420 |
def build_demo():
|
421 |
+
with gr.Blocks(title="Educational AI") as demo:
|
422 |
+
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Educational AI</h1>")
|
423 |
+
|
424 |
+
|
425 |
+
with gr.Row():
|
426 |
+
gr.Markdown("<h4>Click on the button on the right to fill up our questionnaire!</h4>")
|
427 |
+
gr.Button(value="π Questionnaire", link="https://forms.gle/T8CS5CiQgPbKUdeM9", scale=0.5, interactive=True)
|
428 |
+
|
429 |
+
with gr.Tab("Fill-gaps"):
|
430 |
+
build_fill_gaps_interface()
|
431 |
+
|
432 |
+
with gr.Tab("Open question"):
|
433 |
+
build_open_question_interface()
|
434 |
+
|
435 |
+
with gr.Tab("Multiple-choice"):
|
436 |
+
build_multiplechoice_interface()
|
437 |
+
|
438 |
+
with gr.Blocks():
|
439 |
+
with gr.Row():
|
440 |
+
comment_text = gr.TextArea(placeholder="Comment", label="Comment", scale=6)
|
441 |
+
comment_btn = gr.Button(value="π¬ Comment", scale=2)
|
442 |
+
comment_btn.click(comment, [comment_text])
|
443 |
+
|
444 |
+
return demo
|
445 |
|
446 |
if __name__ == "__main__":
|
447 |
build_demo().launch(share=False)
|