Spaces:
Runtime error
Runtime error
Commit
Β·
0446903
1
Parent(s):
a407095
Update demo.py
Browse files
demo.py
CHANGED
@@ -1,6 +1,10 @@
|
|
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
import requests
|
|
|
|
|
4 |
|
5 |
def clamp(x, minimum, maximum):
|
6 |
return max(minimum, min(x, maximum))
|
@@ -10,12 +14,23 @@ def clamp(x, minimum, maximum):
|
|
10 |
#################################################################################################################################################
|
11 |
|
12 |
# read secret api key
|
13 |
-
API_KEY = os.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
base_url = "https://skapi.polyglot-edu.com/"
|
16 |
levels = ["Pre K", "Primary", "Middle School", "High School", "Academic"]
|
17 |
|
18 |
def get_level_mapping(level):
|
|
|
|
|
|
|
19 |
return {
|
20 |
"Academic": 0,
|
21 |
"Pre K": 1,
|
@@ -56,25 +71,41 @@ def generate_fill_gaps(original_text, level, number_of_words, number_of_gaps, nu
|
|
56 |
case "β 350 Words":
|
57 |
number_of_words = 350
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
def generate_open_question(original_text, level, temperature):
|
80 |
"""
|
@@ -94,22 +125,38 @@ def generate_open_question(original_text, level, temperature):
|
|
94 |
The open question.
|
95 |
"""
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
def generate_multiplechoice(original_text, level, number_of_options, number_of_easy_distractors, number_of_distractors, temperature):
|
115 |
"""
|
@@ -135,34 +182,95 @@ def generate_multiplechoice(original_text, level, number_of_options, number_of_e
|
|
135 |
The multiple-choice question.
|
136 |
"""
|
137 |
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
|
154 |
-
if response.status_code == 200:
|
155 |
-
return response.text
|
156 |
-
raise Exception(f"API call failed with status code {response.status_code} and message {response.text}")
|
157 |
|
158 |
#################################################################################################################################################
|
159 |
# Interface building
|
160 |
#################################################################################################################################################
|
161 |
|
162 |
-
flag_link = "https://www.google.com"
|
163 |
-
comment_link = "https://www.google.com"
|
164 |
-
|
165 |
-
|
166 |
def build_fill_gaps_interface():
|
167 |
"""
|
168 |
Build the fill-gaps interface.
|
@@ -174,10 +282,10 @@ def build_fill_gaps_interface():
|
|
174 |
submit_btn = gr.Button(value="Submit")
|
175 |
with gr.Column(scale=4):
|
176 |
output_text_length = gr.Radio(["β 150 Words", "β 250 Words", "β 350 Words"], label="Output text length", value="β 150 Words")
|
177 |
-
level = gr.Radio(levels, label="Level"
|
178 |
with gr.Row():
|
179 |
blanks = gr.Number(value=5, minimum=4, maximum=8, step=1, label="Number of blanks")
|
180 |
-
distractors = gr.Number(value=
|
181 |
temperature = gr.Checkbox(value=False, label="Increase creativity (decreases preciseness)")
|
182 |
|
183 |
def update_numeric(output_text_length, blanks, distractors):
|
@@ -203,11 +311,20 @@ def build_fill_gaps_interface():
|
|
203 |
output = gr.TextArea(placeholder="Generated text", label="Output")
|
204 |
with gr.Row() as button_row:
|
205 |
upvote_btn = gr.Button(value="π Upvote")
|
|
|
206 |
downvote_btn = gr.Button(value="π Downvote")
|
207 |
-
|
208 |
-
|
|
|
|
|
|
|
209 |
|
210 |
submit_btn.click(generate_fill_gaps, [input_field, level, output_text_length, blanks, distractors, temperature], [output])
|
|
|
|
|
|
|
|
|
|
|
211 |
|
212 |
return demo
|
213 |
|
@@ -221,7 +338,7 @@ def build_multiplechoice_interface():
|
|
221 |
input_field = gr.TextArea(lines=10, max_lines=10, label="Input text (can be a text or a url)")
|
222 |
submit_btn = gr.Button(value="Submit")
|
223 |
with gr.Column(scale=4):
|
224 |
-
level = gr.Radio(levels, label="Level"
|
225 |
with gr.Row():
|
226 |
options = gr.Number(value=4, minimum=2, maximum=8, step=1, label="Number of blanks", interactive=False)
|
227 |
easy_distractors = gr.Number(value=1, minimum=0, maximum=8, step=1, label="Number of easy distractors")
|
@@ -249,11 +366,20 @@ def build_multiplechoice_interface():
|
|
249 |
output = gr.TextArea(placeholder="Generated text", label="Output")
|
250 |
with gr.Row() as button_row:
|
251 |
upvote_btn = gr.Button(value="π Upvote")
|
|
|
252 |
downvote_btn = gr.Button(value="π Downvote")
|
253 |
-
|
254 |
-
|
|
|
|
|
|
|
255 |
|
256 |
submit_btn.click(generate_multiplechoice, [input_field, level, options, easy_distractors, distractors, temperature], [output])
|
|
|
|
|
|
|
|
|
|
|
257 |
|
258 |
return demo
|
259 |
|
@@ -267,7 +393,7 @@ def build_open_question_interface():
|
|
267 |
input_field = gr.TextArea(lines=10, max_lines=10, label="Input text (can be a text or a url)")
|
268 |
submit_btn = gr.Button(value="Submit")
|
269 |
with gr.Column(scale=4):
|
270 |
-
level = gr.Radio(levels, label="Level"
|
271 |
with gr.Row():
|
272 |
pass
|
273 |
temperature = gr.Checkbox(value=False, label="Increase creativity (decreases preciseness)")
|
@@ -275,11 +401,20 @@ def build_open_question_interface():
|
|
275 |
output = gr.TextArea(placeholder="Generated text", label="Output")
|
276 |
with gr.Row() as button_row:
|
277 |
upvote_btn = gr.Button(value="π Upvote")
|
|
|
278 |
downvote_btn = gr.Button(value="π Downvote")
|
279 |
-
|
280 |
-
|
|
|
|
|
|
|
281 |
|
282 |
submit_btn.click(generate_open_question, [input_field, level, temperature], [output])
|
|
|
|
|
|
|
|
|
|
|
283 |
|
284 |
return demo
|
285 |
|
|
|
1 |
+
import datetime
|
2 |
import os
|
3 |
+
import json
|
4 |
import gradio as gr
|
5 |
import requests
|
6 |
+
import firebase_admin
|
7 |
+
from firebase_admin import db, credentials
|
8 |
|
9 |
def clamp(x, minimum, maximum):
|
10 |
return max(minimum, min(x, maximum))
|
|
|
14 |
#################################################################################################################################################
|
15 |
|
16 |
# read secret api key
|
17 |
+
API_KEY = os.environ['ApiKey']
|
18 |
+
FIREBASE_API_KEY = os.environ['FirebaseSecret']
|
19 |
+
FIREBASE_URL = os.environ['FirebaseURL']
|
20 |
+
|
21 |
+
creds = credentials.Certificate(json.loads(FIREBASE_API_KEY))
|
22 |
+
firebase_app = firebase_admin.initialize_app(creds, {'databaseURL': FIREBASE_URL})
|
23 |
+
firebase_data_ref = db.reference("data")
|
24 |
+
firebase_current_ref = None
|
25 |
+
|
26 |
|
27 |
base_url = "https://skapi.polyglot-edu.com/"
|
28 |
levels = ["Pre K", "Primary", "Middle School", "High School", "Academic"]
|
29 |
|
30 |
def get_level_mapping(level):
|
31 |
+
if level is None:
|
32 |
+
raise gr.Error("Please select a level.")
|
33 |
+
|
34 |
return {
|
35 |
"Academic": 0,
|
36 |
"Pre K": 1,
|
|
|
71 |
case "β 350 Words":
|
72 |
number_of_words = 350
|
73 |
|
74 |
+
input_json = {
|
75 |
+
"text": original_text,
|
76 |
+
"level": get_level_mapping(level),
|
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 = ""
|
84 |
+
try:
|
85 |
+
response = requests.post(
|
86 |
+
base_url + "FillTheGaps/generateexercise",
|
87 |
+
headers={"ApiKey": API_KEY},
|
88 |
+
json=input_json,
|
89 |
+
timeout=20
|
90 |
+
)
|
91 |
+
|
92 |
+
if response.status_code != 200:
|
93 |
+
output = f"API call failed with status code {response.status_code} and message '{response.text}'"
|
94 |
+
raise Exception(f"API call failed with status code {response.status_code} and message '{response.text}'")
|
95 |
+
|
96 |
+
output = response.text
|
97 |
+
return output
|
98 |
+
finally:
|
99 |
+
global firebase_current_ref
|
100 |
+
firebase_current_ref = firebase_data_ref.push({
|
101 |
+
"type": "fill_the_gaps",
|
102 |
+
**input_json,
|
103 |
+
"output": output,
|
104 |
+
"datetime": str(datetime.datetime.now()),
|
105 |
+
"like": 0,
|
106 |
+
"comment_text": "",
|
107 |
+
"flagged": False,
|
108 |
+
})
|
109 |
|
110 |
def generate_open_question(original_text, level, temperature):
|
111 |
"""
|
|
|
125 |
The open question.
|
126 |
"""
|
127 |
|
128 |
+
input_json = {
|
129 |
+
"text": original_text,
|
130 |
+
"level": get_level_mapping(level),
|
131 |
+
"temperature": int(temperature) * 0.2
|
132 |
+
}
|
133 |
+
|
134 |
+
output = ""
|
135 |
+
try:
|
136 |
+
response = requests.post(
|
137 |
+
base_url + "QuestionExercise/generateexercise",
|
138 |
+
headers={"ApiKey": API_KEY},
|
139 |
+
json=input_json,
|
140 |
+
timeout=20
|
141 |
+
)
|
142 |
+
|
143 |
+
if response.status_code != 200:
|
144 |
+
output = f"API call failed with status code {response.status_code} and message '{response.text}'"
|
145 |
+
raise Exception(f"API call failed with status code {response.status_code} and message '{response.text}'")
|
146 |
+
|
147 |
+
output = response.text
|
148 |
+
return output
|
149 |
+
finally:
|
150 |
+
global firebase_current_ref
|
151 |
+
firebase_current_ref = firebase_data_ref.push({
|
152 |
+
"type": "open_question",
|
153 |
+
**input_json,
|
154 |
+
"output": output,
|
155 |
+
"datetime": str(datetime.datetime.now()),
|
156 |
+
"like": 0,
|
157 |
+
"comment_text": "",
|
158 |
+
"flagged": False,
|
159 |
+
})
|
160 |
|
161 |
def generate_multiplechoice(original_text, level, number_of_options, number_of_easy_distractors, number_of_distractors, temperature):
|
162 |
"""
|
|
|
182 |
The multiple-choice question.
|
183 |
"""
|
184 |
|
185 |
+
input_json = {
|
186 |
+
"text": original_text,
|
187 |
+
"type": True,
|
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(
|
197 |
+
base_url + "QuizExercise/generateexercise",
|
198 |
+
headers={"ApiKey": API_KEY},
|
199 |
+
json=input_json,
|
200 |
+
timeout=20
|
201 |
+
)
|
202 |
+
|
203 |
+
if response.status_code != 200:
|
204 |
+
output = f"API call failed with status code {response.status_code} and message '{response.text}'"
|
205 |
+
raise Exception(f"API call failed with status code {response.status_code} and message '{response.text}'")
|
206 |
+
|
207 |
+
output = response.text
|
208 |
+
return output
|
209 |
+
finally:
|
210 |
+
global firebase_current_ref
|
211 |
+
firebase_current_ref = firebase_data_ref.push({
|
212 |
+
"type": "open_question",
|
213 |
+
**input_json,
|
214 |
+
"output": output,
|
215 |
+
"datetime": str(datetime.datetime.now()),
|
216 |
+
"like": 0,
|
217 |
+
"comment_text": "",
|
218 |
+
"flagged": False,
|
219 |
+
})
|
220 |
+
|
221 |
+
def like():
|
222 |
+
global firebase_current_ref
|
223 |
+
if firebase_current_ref is not None:
|
224 |
+
firebase_current_ref.update({"like": 1})
|
225 |
+
gr.Info("Generated text liked.")
|
226 |
+
else:
|
227 |
+
gr.Warning("No generated text to vote.")
|
228 |
+
|
229 |
+
def neutral_like():
|
230 |
+
global firebase_current_ref
|
231 |
+
if firebase_current_ref is not None:
|
232 |
+
firebase_current_ref.update({"like": 0})
|
233 |
+
gr.Info("Generated text preference removed.")
|
234 |
+
else:
|
235 |
+
gr.Warning("No generated text to vote.")
|
236 |
+
|
237 |
+
def dislike():
|
238 |
+
global firebase_current_ref
|
239 |
+
if firebase_current_ref is not None:
|
240 |
+
firebase_current_ref.update({"like": -1})
|
241 |
+
gr.Info("Generated text disliked.")
|
242 |
+
else:
|
243 |
+
gr.Warning("No generated text to vote.")
|
244 |
+
|
245 |
+
def comment(comment_text):
|
246 |
+
global firebase_current_ref
|
247 |
+
if firebase_current_ref is not None:
|
248 |
+
firebase_current_ref.update({"comment": comment_text})
|
249 |
+
gr.Info("Comment added.")
|
250 |
+
else:
|
251 |
+
raise gr.Error("No generated text to comment.")
|
252 |
+
|
253 |
+
def flag(flag_btn):
|
254 |
+
global firebase_current_ref
|
255 |
+
if firebase_current_ref is not None:
|
256 |
+
firebase_current_ref.update({"flagged": not firebase_current_ref.get()["flagged"]})
|
257 |
+
if firebase_current_ref.get()["flagged"]:
|
258 |
+
gr.Info("Generated text flagged.")
|
259 |
+
value = "β οΈ Unflag"
|
260 |
+
else:
|
261 |
+
gr.Info("Generated text unflagged.")
|
262 |
+
value = "β οΈ Flag"
|
263 |
+
return gr.Button(value=value)
|
264 |
+
else:
|
265 |
+
gr.Warning("No generated text to flag.")
|
266 |
+
|
267 |
+
return flag_btn
|
268 |
|
|
|
|
|
|
|
269 |
|
270 |
#################################################################################################################################################
|
271 |
# Interface building
|
272 |
#################################################################################################################################################
|
273 |
|
|
|
|
|
|
|
|
|
274 |
def build_fill_gaps_interface():
|
275 |
"""
|
276 |
Build the fill-gaps interface.
|
|
|
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):
|
|
|
311 |
output = gr.TextArea(placeholder="Generated text", label="Output")
|
312 |
with gr.Row() as button_row:
|
313 |
upvote_btn = gr.Button(value="π Upvote")
|
314 |
+
remove_preference_btn = gr.Button(value="Remove Preference")
|
315 |
downvote_btn = gr.Button(value="π Downvote")
|
316 |
+
flag_btn = gr.Button(value="β οΈ Flag")
|
317 |
+
gr.Markdown("<hr/>")
|
318 |
+
with gr.Row():
|
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
|
330 |
|
|
|
338 |
input_field = gr.TextArea(lines=10, max_lines=10, label="Input text (can be a text or a url)")
|
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")
|
|
|
366 |
output = gr.TextArea(placeholder="Generated text", label="Output")
|
367 |
with gr.Row() as button_row:
|
368 |
upvote_btn = gr.Button(value="π Upvote")
|
369 |
+
remove_preference_btn = gr.Button(value="Remove Preference")
|
370 |
downvote_btn = gr.Button(value="π Downvote")
|
371 |
+
flag_btn = gr.Button(value="β οΈ Flag")
|
372 |
+
gr.Markdown("<hr/>")
|
373 |
+
with gr.Row():
|
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
|
385 |
|
|
|
393 |
input_field = gr.TextArea(lines=10, max_lines=10, label="Input text (can be a text or a url)")
|
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 |
pass
|
399 |
temperature = gr.Checkbox(value=False, label="Increase creativity (decreases preciseness)")
|
|
|
401 |
output = gr.TextArea(placeholder="Generated text", label="Output")
|
402 |
with gr.Row() as button_row:
|
403 |
upvote_btn = gr.Button(value="π Upvote")
|
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 |
|