Spaces:
Runtime error
Runtime error
Commit
·
1f50941
1
Parent(s):
124a55b
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import text_gen as gen
|
4 |
+
|
5 |
+
|
6 |
+
NUM_QUESTIONS = 6
|
7 |
+
answers = {}
|
8 |
+
separator = '<[._.]>'
|
9 |
+
|
10 |
+
df = pd.read_csv('lines_2.txt', sep='*')
|
11 |
+
df = pd.concat([df[df.Part == 'Start'].sample(1),
|
12 |
+
df[df.Part == 'Middle_1'].sample(1),
|
13 |
+
df[df.Part == 'Middle_2'].sample(1),
|
14 |
+
df[df.Part == 'Middle_3'].sample(1),
|
15 |
+
df[df.Part == 'Middle_4'].sample(NUM_QUESTIONS-5),
|
16 |
+
df[df.Part == 'End'].sample(1)])
|
17 |
+
|
18 |
+
questions = dict(zip(df.Question, df.Answer))
|
19 |
+
context = dict(zip(df.Question, df.Context))
|
20 |
+
|
21 |
+
def get_answer(question, answer, options):
|
22 |
+
global answers
|
23 |
+
global separator
|
24 |
+
answer = options.split(separator)[answer]
|
25 |
+
answers.update({question:int(answer == questions[question])})
|
26 |
+
|
27 |
+
|
28 |
+
def set_score():
|
29 |
+
global answers
|
30 |
+
return str(sum(answers.values()))
|
31 |
+
|
32 |
+
|
33 |
+
wrong_answers = {q:[gen.generate_text(
|
34 |
+
q,
|
35 |
+
context[q],
|
36 |
+
gen.model_names[i],
|
37 |
+
gen.model[i],
|
38 |
+
gen.tokenizers[i],
|
39 |
+
minimum=len(questions[q].split())+8)
|
40 |
+
for i in range(3)]
|
41 |
+
for q in questions.keys()}
|
42 |
+
|
43 |
+
|
44 |
+
with gr.Blocks(theme='glass') as demo:
|
45 |
+
with gr.Row():
|
46 |
+
|
47 |
+
with gr.Column(scale=1):
|
48 |
+
pass
|
49 |
+
|
50 |
+
with gr.Column(scale=2):
|
51 |
+
|
52 |
+
gr.Markdown(f'## <p style="text-align: center;">TITLE</p>')
|
53 |
+
|
54 |
+
for ind, question in enumerate(list(questions.keys()), start=1):
|
55 |
+
letters = list('ABCD')
|
56 |
+
options = list(set(wrong_answers[question] + [questions[question]]))
|
57 |
+
|
58 |
+
gr.Markdown(f'### <p>{ind}. {question}</p>')
|
59 |
+
|
60 |
+
for letter, option in zip(letters, options):
|
61 |
+
gr.Markdown(f'{letter}. {option}')
|
62 |
+
|
63 |
+
options = gr.State(separator.join(options))
|
64 |
+
|
65 |
+
radio = gr.Radio(letters, type='index', show_label=False)
|
66 |
+
question = gr.State(question)
|
67 |
+
radio.change(fn=get_answer, inputs=[question, radio, options])
|
68 |
+
|
69 |
+
button = gr.Button(value='Get score')
|
70 |
+
score = gr.Markdown()
|
71 |
+
button.click(fn=set_score, outputs=score)
|
72 |
+
|
73 |
+
with gr.Column(scale=1):
|
74 |
+
pass
|
75 |
+
|
76 |
+
|
77 |
+
demo.launch()
|