Spaces:
Runtime error
Runtime error
stibiumghost
commited on
Commit
·
dcb5a5c
1
Parent(s):
2b5ff89
Update app.py
Browse files
app.py
CHANGED
@@ -1,86 +1,126 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
-
|
4 |
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
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 |
-
).fillna('')
|
18 |
|
19 |
-
questions = dict(zip(df.Question, df.Answer))
|
20 |
-
context = dict(zip(df.Question, df.Context))
|
21 |
|
22 |
-
def
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
27 |
|
28 |
|
29 |
def set_score():
|
30 |
-
|
|
|
31 |
global NUM_QUESTIONS
|
32 |
-
score =
|
|
|
|
|
33 |
start = '## <p style="text-align: center;">'
|
34 |
end = '</p>'
|
35 |
-
if score == 0: return f'{start}Not a single right answer. Are you doing this on purpose?{end}'
|
36 |
-
elif score <= NUM_QUESTIONS / 2 + 1: return f'{start}Only {score} right answers out of {NUM_QUESTIONS}. You ought to pay more attention.{end}'
|
37 |
-
elif score == NUM_QUESTIONS: return f'{start}Perfect score!{end}'
|
38 |
-
else: return f'{start}{score} right answers out of {NUM_QUESTIONS}. It\'s probably alright.{end}'
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
|
51 |
|
52 |
-
|
53 |
-
|
54 |
|
55 |
-
with gr.Column(scale=1):
|
56 |
-
pass
|
57 |
|
58 |
-
|
|
|
|
|
59 |
|
60 |
-
gr.
|
61 |
-
|
62 |
|
63 |
-
|
64 |
-
letters = list('ABCD')
|
65 |
-
options = list(set(wrong_answers[question] + [questions[question]]))
|
66 |
|
67 |
-
gr.Markdown(f'
|
|
|
68 |
|
69 |
-
for
|
70 |
-
|
|
|
71 |
|
72 |
-
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
radio.change(fn=get_answer, inputs=[question, radio, options])
|
77 |
|
78 |
-
|
79 |
-
score = gr.Markdown()
|
80 |
-
button.click(fn=set_score, outputs=score)
|
81 |
|
82 |
-
|
83 |
-
|
|
|
84 |
|
|
|
|
|
|
|
85 |
|
86 |
-
|
|
|
|
|
|
|
|
1 |
+
import transformers
|
2 |
import gradio as gr
|
3 |
import pandas as pd
|
4 |
+
from text_gen import generate_text
|
5 |
|
6 |
|
7 |
+
def get_answer(question, answer, options):
|
8 |
+
'''File given answer'''
|
9 |
+
|
10 |
+
global questions
|
11 |
+
global SEPARATOR
|
12 |
+
answer = options.split(SEPARATOR)[answer]
|
13 |
+
questions['Choice'][questions.Question == question] = answer
|
|
|
|
|
|
|
|
|
|
|
14 |
|
|
|
|
|
15 |
|
16 |
+
def find_score(column_name, questions):
|
17 |
+
'''Count chosen answers in a given column'''
|
18 |
+
|
19 |
+
actual = questions[column_name].to_list()
|
20 |
+
chosen = questions.Choice.to_list()
|
21 |
+
return sum(a == c for a, c in zip(actual, chosen))
|
22 |
|
23 |
|
24 |
def set_score():
|
25 |
+
'''Return the score (for the human and for the models)'''
|
26 |
+
|
27 |
global NUM_QUESTIONS
|
28 |
+
score = find_score(answers[0], questions)
|
29 |
+
ai_score = {name:find_score(name, questions) for name in answers[1:]}
|
30 |
+
|
31 |
start = '## <p style="text-align: center;">'
|
32 |
end = '</p>'
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
if score == NUM_QUESTIONS: text = f'Perfect score!'
|
35 |
+
else:
|
36 |
+
if score == 0: text = 'Not a single right answer. Are you doing this on purpose?'
|
37 |
+
elif score <= NUM_QUESTIONS / 2 + 1: text = f'Only {score} right answers out of {NUM_QUESTIONS}. You ought to pay more attention.'
|
38 |
+
else: text = f'{score} right answers out of {NUM_QUESTIONS}. It\'s probably alright.'
|
39 |
+
for name in ai_score.keys():
|
40 |
+
text += f'\n{name} got {ai_score[name]}.'
|
41 |
+
|
42 |
+
return start + text + end
|
43 |
+
|
44 |
+
if __name__ == "__main__":
|
45 |
+
|
46 |
+
NUM_QUESTIONS = 10
|
47 |
+
SEPARATOR = '<[._.]>'
|
48 |
+
|
49 |
+
|
50 |
+
# Adding/replacing models may require adjustments to text_gen.py
|
51 |
+
model_names = ['microsoft/GODEL-v1_1-large-seq2seq',
|
52 |
+
'facebook/blenderbot-1B-distill',
|
53 |
+
'facebook/blenderbot_small-90M']
|
54 |
+
|
55 |
+
tokenizers = [transformers.AutoTokenizer.from_pretrained(model_names[0]),
|
56 |
+
transformers.BlenderbotTokenizer.from_pretrained(model_names[1]),
|
57 |
+
transformers.BlenderbotSmallTokenizer.from_pretrained(model_names[2])]
|
58 |
+
|
59 |
+
model = [transformers.AutoModelForSeq2SeqLM.from_pretrained(model_names[0]),
|
60 |
+
transformers.BlenderbotForConditionalGeneration.from_pretrained(model_names[1]),
|
61 |
+
transformers.BlenderbotSmallForConditionalGeneration.from_pretrained(model_names[2])]
|
62 |
+
|
63 |
+
|
64 |
+
# Semi-randomly choose questions
|
65 |
+
questions = pd.read_csv('lines_2.txt', sep='*')
|
66 |
+
questions = pd.concat([questions[questions.Part == 'Start'].sample(1),
|
67 |
+
questions[questions.Part == 'Middle_1'].sample(1),
|
68 |
+
questions[questions.Part == 'Middle_2'].sample(1),
|
69 |
+
questions[questions.Part == 'Middle_3'].sample(1),
|
70 |
+
questions[questions.Part == 'Middle_4'].sample(NUM_QUESTIONS-5),
|
71 |
+
questions[questions.Part == 'End'].sample(1)]
|
72 |
+
).fillna('')
|
73 |
+
|
74 |
+
|
75 |
+
# Generate answers
|
76 |
|
77 |
+
for i in range(len(model_names)):
|
78 |
+
questions[model_names[i]] = questions.Question.apply(
|
79 |
+
lambda x: generate_text(
|
80 |
+
x,
|
81 |
+
questions.Context[questions.Question == x].iat[0],
|
82 |
+
model_names[i],
|
83 |
+
model[i],
|
84 |
+
tokenizers[i],
|
85 |
+
minimum=len(questions.Answer[questions.Question == x].iat[0].split())+8))
|
86 |
|
87 |
|
88 |
+
questions['Choice'] = ''
|
89 |
+
answers = ['Answer'] + model_names
|
90 |
|
|
|
|
|
91 |
|
92 |
+
# App
|
93 |
+
with gr.Blocks() as test_gen:
|
94 |
+
with gr.Row():
|
95 |
|
96 |
+
with gr.Column(scale=1):
|
97 |
+
pass
|
98 |
|
99 |
+
with gr.Column(scale=2):
|
|
|
|
|
100 |
|
101 |
+
gr.Markdown(f'## <p style="text-align: center;">IMITATION GAME</p>\n' +
|
102 |
+
f'### <p style="text-align: center;">Choose answers NOT given by an AI model.')
|
103 |
|
104 |
+
for ind, question in enumerate(questions.Question.to_list(), start=1):
|
105 |
+
letters = list('ABCD')
|
106 |
+
options = list(set([questions[col][questions.Question == question].iat[0] for col in answers]))
|
107 |
|
108 |
+
gr.Markdown(f'### <p>{ind}. {question}</p>')
|
109 |
|
110 |
+
for letter, option in zip(letters, options):
|
111 |
+
gr.Markdown(f'{letter}. {option}')
|
|
|
112 |
|
113 |
+
options = gr.State(SEPARATOR.join(options))
|
|
|
|
|
114 |
|
115 |
+
radio = gr.Radio(letters, type='index', show_label=False)
|
116 |
+
question = gr.State(question)
|
117 |
+
radio.change(fn=get_answer, inputs=[question, radio, options])
|
118 |
|
119 |
+
button = gr.Button(value='Get score')
|
120 |
+
score = gr.Markdown()
|
121 |
+
button.click(fn=set_score, outputs=score)
|
122 |
|
123 |
+
with gr.Column(scale=1):
|
124 |
+
pass
|
125 |
+
|
126 |
+
test_gen.launch()
|