Spaces:
Build error
Build error
Anshoo Mehra
commited on
Commit
·
978ee76
1
Parent(s):
3b12a0e
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,208 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from transformers import (
|
4 |
+
pipeline,
|
5 |
+
AutoModelForSeq2SeqLM,
|
6 |
+
AutoTokenizer
|
7 |
+
)
|
8 |
+
|
9 |
+
M1 = "anshoomehra/question-generation-auto-t5-v1-base-s-q"
|
10 |
+
M2 = "anshoomehra/question-generation-auto-t5-v1-base-s-q-c"
|
11 |
+
|
12 |
+
M4 = "anshoomehra/question-generation-auto-hints-t5-v1-base-s-q"
|
13 |
+
M5 = "anshoomehra/question-generation-auto-hints-t5-v1-base-s-q-c"
|
14 |
+
|
15 |
+
device = ['cuda' if torch.cuda.is_available() else 'cpu'][0]
|
16 |
+
|
17 |
+
_m1 = AutoModelForSeq2SeqLM.from_pretrained(M1).to(device)
|
18 |
+
_tk1 = AutoTokenizer.from_pretrained(M1, cache_dir="./cache")
|
19 |
+
|
20 |
+
_m2 = AutoModelForSeq2SeqLM.from_pretrained(M2).to(device)
|
21 |
+
_tk2 = AutoTokenizer.from_pretrained(M2, cache_dir="./cache")
|
22 |
+
|
23 |
+
_m4 = AutoModelForSeq2SeqLM.from_pretrained(M4).to(device)
|
24 |
+
_tk4 = AutoTokenizer.from_pretrained(M4, cache_dir="./cache")
|
25 |
+
|
26 |
+
_m5 = AutoModelForSeq2SeqLM.from_pretrained(M5).to(device)
|
27 |
+
_tk5 = AutoTokenizer.from_pretrained(M5, cache_dir="./cache")
|
28 |
+
|
29 |
+
def _formatQs(questions):
|
30 |
+
_finalQs = ""
|
31 |
+
|
32 |
+
if questions is not None:
|
33 |
+
_qList = questions[0].strip().split("?")
|
34 |
+
|
35 |
+
qIdx = 1
|
36 |
+
if len(_qList) > 1:
|
37 |
+
for idx, _q in enumerate(_qList):
|
38 |
+
_q = _q.strip()
|
39 |
+
if _q is not None and len(_q) !=0:
|
40 |
+
_finalQs += str(qIdx) + ". " + _q + "? \n"
|
41 |
+
qIdx+=1
|
42 |
+
else:
|
43 |
+
if len(_qList[0])>1:
|
44 |
+
_finalQs = "1. " + str(_qList[0]) + "?"
|
45 |
+
else:
|
46 |
+
_finalQs = None
|
47 |
+
return _finalQs
|
48 |
+
|
49 |
+
def _generate(mode, context, hint=None, minLength=50, maxLength=500, lengthPenalty=2.0, earlyStopping=True, numReturnSequences=1, numBeams=2, noRepeatNGramSize=0, doSample=False, topK=0, topP=0, temperature=0):
|
50 |
+
|
51 |
+
predictionM1 = None
|
52 |
+
predictionM2 = None
|
53 |
+
predictionM4 = None
|
54 |
+
predictionM5 = None
|
55 |
+
|
56 |
+
if mode == 'Auto':
|
57 |
+
_inputText = "question_context: " + context
|
58 |
+
|
59 |
+
_encoding = _tk1.encode(_inputText, return_tensors='pt', truncation=True, padding='max_length').to(device) # max_length=1024
|
60 |
+
_outputEncoded = _m1.generate(_encoding,
|
61 |
+
min_length=minLength,
|
62 |
+
max_length=maxLength,
|
63 |
+
length_penalty=lengthPenalty,
|
64 |
+
early_stopping=earlyStopping,
|
65 |
+
num_return_sequences=numReturnSequences,
|
66 |
+
num_beams=numBeams,
|
67 |
+
no_repeat_ngram_size=noRepeatNGramSize,
|
68 |
+
do_sample=doSample,
|
69 |
+
top_k=topK,
|
70 |
+
top_p=topP,
|
71 |
+
temperature=temperature
|
72 |
+
)
|
73 |
+
predictionM1 = [_tk1.decode(id, clean_up_tokenization_spaces=False, skip_special_tokens=True) for id in _outputEncoded]
|
74 |
+
|
75 |
+
_encoding = _tk2.encode(_inputText, return_tensors='pt', truncation=True, padding='max_length').to(device) # max_length=1024 .to(device)
|
76 |
+
_outputEncoded = _m2.generate(_encoding,
|
77 |
+
min_length=minLength,
|
78 |
+
max_length=maxLength,
|
79 |
+
length_penalty=lengthPenalty,
|
80 |
+
early_stopping=earlyStopping,
|
81 |
+
num_return_sequences=numReturnSequences,
|
82 |
+
num_beams=numBeams,
|
83 |
+
no_repeat_ngram_size=noRepeatNGramSize,
|
84 |
+
do_sample=doSample,
|
85 |
+
top_k=topK,
|
86 |
+
top_p=topP,
|
87 |
+
temperature=temperature
|
88 |
+
)
|
89 |
+
predictionM2 = [_tk2.decode(id, clean_up_tokenization_spaces=False, skip_special_tokens=True) for id in _outputEncoded]
|
90 |
+
|
91 |
+
_encoding = _tk4.encode(_inputText, return_tensors='pt', truncation=True, padding='max_length').to(device) # max_length=1024 .to(device)
|
92 |
+
_outputEncoded = _m4.generate(_encoding,
|
93 |
+
min_length=minLength,
|
94 |
+
max_length=maxLength,
|
95 |
+
length_penalty=lengthPenalty,
|
96 |
+
early_stopping=earlyStopping,
|
97 |
+
num_return_sequences=numReturnSequences,
|
98 |
+
num_beams=numBeams,
|
99 |
+
no_repeat_ngram_size=noRepeatNGramSize,
|
100 |
+
do_sample=doSample,
|
101 |
+
top_k=topK,
|
102 |
+
top_p=topP,
|
103 |
+
temperature=temperature
|
104 |
+
)
|
105 |
+
predictionM4 = [_tk4.decode(id, clean_up_tokenization_spaces=False, skip_special_tokens=True) for id in _outputEncoded]
|
106 |
+
|
107 |
+
_encoding = _tk5.encode(_inputText, return_tensors='pt', truncation=True, padding='max_length').to(device) # max_length=1024 .to(device)
|
108 |
+
_outputEncoded = _m5.generate(_encoding,
|
109 |
+
min_length=minLength,
|
110 |
+
max_length=maxLength,
|
111 |
+
length_penalty=lengthPenalty,
|
112 |
+
early_stopping=earlyStopping,
|
113 |
+
num_return_sequences=numReturnSequences,
|
114 |
+
num_beams=numBeams,
|
115 |
+
no_repeat_ngram_size=noRepeatNGramSize,
|
116 |
+
do_sample=doSample,
|
117 |
+
top_k=topK,
|
118 |
+
top_p=topP,
|
119 |
+
temperature=temperature
|
120 |
+
)
|
121 |
+
predictionM5 = [_tk5.decode(id, clean_up_tokenization_spaces=False, skip_special_tokens=True) for id in _outputEncoded]
|
122 |
+
|
123 |
+
elif mode == 'Hints':
|
124 |
+
_inputText = "question_hint: " + hint + "</s>question_context: " + context
|
125 |
+
|
126 |
+
_encoding = _tk4.encode(_inputText, return_tensors='pt', truncation=True, padding='max_length').to(device) # max_length=1024 .to(device)
|
127 |
+
_outputEncoded = _m4.generate(_encoding,
|
128 |
+
min_length=minLength,
|
129 |
+
max_length=maxLength,
|
130 |
+
length_penalty=lengthPenalty,
|
131 |
+
early_stopping=earlyStopping,
|
132 |
+
num_return_sequences=numReturnSequences,
|
133 |
+
num_beams=numBeams,
|
134 |
+
no_repeat_ngram_size=noRepeatNGramSize,
|
135 |
+
do_sample=doSample,
|
136 |
+
top_k=topK,
|
137 |
+
top_p=topP,
|
138 |
+
temperature=temperature
|
139 |
+
)
|
140 |
+
predictionM4 = [_tk4.decode(id, clean_up_tokenization_spaces=False, skip_special_tokens=True) for id in _outputEncoded]
|
141 |
+
|
142 |
+
_encoding = _tk5.encode(_inputText, return_tensors='pt', truncation=True, padding='max_length').to(device) # max_length=1024 .to(device)
|
143 |
+
_outputEncoded = _m5.generate(_encoding,
|
144 |
+
min_length=minLength,
|
145 |
+
max_length=maxLength,
|
146 |
+
length_penalty=lengthPenalty,
|
147 |
+
early_stopping=earlyStopping,
|
148 |
+
num_return_sequences=numReturnSequences,
|
149 |
+
num_beams=numBeams,
|
150 |
+
no_repeat_ngram_size=noRepeatNGramSize,
|
151 |
+
do_sample=doSample,
|
152 |
+
top_k=topK,
|
153 |
+
top_p=topP,
|
154 |
+
temperature=temperature
|
155 |
+
)
|
156 |
+
predictionM5 = [_tk5.decode(id, clean_up_tokenization_spaces=False, skip_special_tokens=True) for id in _outputEncoded]
|
157 |
+
|
158 |
+
predictionM1 = _formatQs(predictionM1)
|
159 |
+
predictionM2 = _formatQs(predictionM2)
|
160 |
+
predictionM4 = _formatQs(predictionM4)
|
161 |
+
predictionM5 = _formatQs(predictionM5)
|
162 |
+
|
163 |
+
return predictionM1, predictionM2, predictionM4, predictionM5
|
164 |
+
|
165 |
+
with gr.Blocks() as demo:
|
166 |
+
|
167 |
+
gr.Markdown(value="# Question Generation Demo")
|
168 |
+
with gr.Accordion(variant='compact', label='Hyperparams', open=False):
|
169 |
+
with gr.Row():
|
170 |
+
mode = gr.Radio(["Auto", "Hints"], value="Auto", label="Mode")
|
171 |
+
with gr.Row():
|
172 |
+
minLength = gr.Slider(10, 512, 50, step=1, label="Min Length")
|
173 |
+
maxLength = gr.Slider(20, 512, 164, step=1, label="Max Length")
|
174 |
+
lengthPenalty = gr.Slider(-5, 5, 1, label="Length Penalty")
|
175 |
+
earlyStopping = gr.Checkbox(True, label="Early Stopping [EOS]")
|
176 |
+
numReturnSequences = gr.Slider(1, 3, 1, step=1, label="Num return Sequences")
|
177 |
+
with gr.Row():
|
178 |
+
numBeams = gr.Slider(1, 10, 4, step=1, label="Beams")
|
179 |
+
noRepeatNGramSize = gr.Slider(0, 5, 3, step=1, label="No Repeat N-Gram Size")
|
180 |
+
with gr.Row():
|
181 |
+
doSample = gr.Checkbox(label="Do Random Sample")
|
182 |
+
topK = gr.Slider(0, 50, 0, step=1, label="Top K")
|
183 |
+
topP = gr.Slider(0, 1, 0, label="Top P/Nucleus Sampling")
|
184 |
+
temperature = gr.Slider(0.01, 1, 1, label="Temperature")
|
185 |
+
|
186 |
+
with gr.Accordion(variant='compact', label='Input Values'):
|
187 |
+
with gr.Row(variant='compact'):
|
188 |
+
contextDefault = "Google LLC is an American multinational technology company focusing on search engine technology, online advertising, cloud computing, computer software, quantum computing, e-commerce, artificial intelligence, and consumer electronics. It has been referred to as 'the most powerful company in the world' and one of the world's most valuable brands due to its market dominance, data collection, and technological advantages in the area of artificial intelligence. Its parent company Alphabet is considered one of the Big Five American information technology companies, alongside Amazon, Apple, Meta, and Microsoft."
|
189 |
+
hintDefault = ""
|
190 |
+
context = gr.Textbox(contextDefault, label="Context", placeholder="Dummy Context", lines=5)
|
191 |
+
hint = gr.Textbox(hintDefault, label="Hint", placeholder="Enter hint here. Ensure the mode is set to 'Hints' prior using hints.", lines=2)
|
192 |
+
|
193 |
+
with gr.Accordion(variant='compact', label='Multi-Task Model(s) Sensitive To Hints'):
|
194 |
+
with gr.Row(variant='compact'):
|
195 |
+
_predictionM5 = gr.Textbox(label="Predicted Questions - question-generation-auto-hints-t5-v1-base-s-q-c [Hints Sensitive]")
|
196 |
+
_predictionM4 = gr.Textbox(label="Predicted Questions - question-generation-auto-hints-t5-v1-base-s-q [Hints Sensitive]")
|
197 |
+
|
198 |
+
with gr.Accordion(variant='compact', label='Uni-Task Model(s) Non-Sensitive To Hints'):
|
199 |
+
with gr.Row(variant='compact'):
|
200 |
+
_predictionM2 = gr.Textbox(label="Predicted Questions - question-generation-auto-t5-v1-base-s-q-c [No Hints]")
|
201 |
+
_predictionM1 = gr.Textbox(label="Predicted Questions - question-generation-auto-t5-v1-base-s-q [No Hints]")
|
202 |
+
|
203 |
+
with gr.Row():
|
204 |
+
gen_btn = gr.Button("Generate Questions")
|
205 |
+
gen_btn.click(fn=_generate,
|
206 |
+
inputs=[mode, context, hint, minLength, maxLength, lengthPenalty, earlyStopping, numReturnSequences, numBeams, noRepeatNGramSize, doSample, topK, topP, temperature],
|
207 |
+
outputs=[_predictionM1, _predictionM2, _predictionM4, _predictionM5]
|
208 |
+
)
|