Spaces:
Runtime error
Runtime error
remove maintenance "if"
Browse files
app.py
CHANGED
|
@@ -54,141 +54,137 @@ st.write("Question Generation, Question Answering and Questions/Answers Generati
|
|
| 54 |
ids = {'mt5-small': st.secrets['small'],
|
| 55 |
'mt5-base': st.secrets['base']}
|
| 56 |
|
| 57 |
-
maintenance = False
|
| 58 |
-
|
| 59 |
-
if maintenance:
|
| 60 |
-
st.write("Unavailable for now (maintenance). ")
|
| 61 |
-
else:
|
| 62 |
-
# Download all models from drive
|
| 63 |
-
download_models(ids)
|
| 64 |
-
|
| 65 |
-
# Task selection
|
| 66 |
-
|
| 67 |
-
left, right = st.columns([4, 2])
|
| 68 |
-
task = left.selectbox('Choose the task: ',
|
| 69 |
-
options=['Questions/Answers Pairs Generation', 'Question Answering', 'Question Generation'],
|
| 70 |
-
help='Choose the task you want to try out')
|
| 71 |
-
|
| 72 |
-
# Model selection
|
| 73 |
-
model_path = right.selectbox('', options=[k for k in ids], index=1, help='Model to use. ')
|
| 74 |
-
model = load_model(model_path=f"model/{model_path}.ckpt")
|
| 75 |
-
right.write(model.device)
|
| 76 |
-
|
| 77 |
-
if task == 'Questions/Answers Pairs Generation':
|
| 78 |
-
# Input area
|
| 79 |
-
inputs = st.text_area('Context:', value="A few years after the First Crusade, in 1107, the Normans under "
|
| 80 |
-
"the command of Bohemond, Robert\'s son, landed in Valona and "
|
| 81 |
-
"besieged Dyrrachium using the most sophisticated military "
|
| 82 |
-
"equipment of the time, but to no avail. Meanwhile, they occupied "
|
| 83 |
-
"Petrela, the citadel of Mili at the banks of the river Deabolis, "
|
| 84 |
-
"Gllavenica (Ballsh), Kanina and Jericho. This time, "
|
| 85 |
-
"the Albanians sided with the Normans, dissatisfied by the heavy "
|
| 86 |
-
"taxes the Byzantines had imposed upon them. With their help, "
|
| 87 |
-
"the Normans secured the Arbanon passes and opened their way to "
|
| 88 |
-
"Dibra. The lack of supplies, disease and Byzantine resistance "
|
| 89 |
-
"forced Bohemond to retreat from his campaign and sign a peace "
|
| 90 |
-
"treaty with the Byzantines in the city of Deabolis. ", max_chars=2048,
|
| 91 |
-
height=250)
|
| 92 |
-
split = st.checkbox('Split into sentences', value=True)
|
| 93 |
-
|
| 94 |
-
if split:
|
| 95 |
-
# Split into sentences
|
| 96 |
-
sent_tokenized = nltk.sent_tokenize(inputs)
|
| 97 |
-
res = {}
|
| 98 |
-
|
| 99 |
-
with st.spinner('Please wait while the inputs are being processed...'):
|
| 100 |
-
# Iterate over sentences
|
| 101 |
-
for sentence in sent_tokenized:
|
| 102 |
-
predictions = model.multitask([sentence], max_length=512)
|
| 103 |
-
questions, answers, answers_bis = predictions['questions'], predictions['answers'], predictions[
|
| 104 |
-
'answers_bis']
|
| 105 |
-
|
| 106 |
-
# Build answer dict
|
| 107 |
-
content = {}
|
| 108 |
-
for question, answer, answer_bis in zip(questions[0], answers[0], answers_bis[0]):
|
| 109 |
-
content[question] = {'answer (extracted)': answer, 'answer (generated)': answer_bis}
|
| 110 |
-
res[sentence] = content
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
predictions = model.multitask([
|
| 119 |
questions, answers, answers_bis = predictions['questions'], predictions['answers'], predictions[
|
| 120 |
'answers_bis']
|
| 121 |
|
| 122 |
-
#
|
| 123 |
-
zip = zip(questions[0], answers[0], answers_bis[0])
|
| 124 |
content = {}
|
| 125 |
-
for question, answer, answer_bis in zip:
|
| 126 |
content[question] = {'answer (extracted)': answer, 'answer (generated)': answer_bis}
|
| 127 |
-
|
| 128 |
-
st.write(content)
|
| 129 |
-
|
| 130 |
-
elif task == 'Question Answering':
|
| 131 |
-
|
| 132 |
-
# Input area
|
| 133 |
-
inputs = st.text_area('Context:', value="A few years after the First Crusade, in 1107, the Normans under "
|
| 134 |
-
"the command of Bohemond, Robert\'s son, landed in Valona and "
|
| 135 |
-
"besieged Dyrrachium using the most sophisticated military "
|
| 136 |
-
"equipment of the time, but to no avail. Meanwhile, they occupied "
|
| 137 |
-
"Petrela, the citadel of Mili at the banks of the river Deabolis, "
|
| 138 |
-
"Gllavenica (Ballsh), Kanina and Jericho. This time, "
|
| 139 |
-
"the Albanians sided with the Normans, dissatisfied by the heavy "
|
| 140 |
-
"taxes the Byzantines had imposed upon them. With their help, "
|
| 141 |
-
"the Normans secured the Arbanon passes and opened their way to "
|
| 142 |
-
"Dibra. The lack of supplies, disease and Byzantine resistance "
|
| 143 |
-
"forced Bohemond to retreat from his campaign and sign a peace "
|
| 144 |
-
"treaty with the Byzantines in the city of Deabolis. ", max_chars=2048,
|
| 145 |
-
height=250)
|
| 146 |
-
question = st.text_input('Question:', value="What forced Bohemond to retreat from his campaign? ")
|
| 147 |
-
|
| 148 |
-
# Prediction
|
| 149 |
-
with st.spinner('Please wait while the inputs are being processed...'):
|
| 150 |
-
predictions = model.qa([{'question': question, 'context': inputs}], max_length=512)
|
| 151 |
-
answer = {question: predictions[0]}
|
| 152 |
|
| 153 |
# Answer area
|
| 154 |
-
st.write(
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
# Input area
|
| 159 |
-
inputs = st.text_area('Context (highlight answers with <hl> tokens): ',
|
| 160 |
-
value="A few years after the First Crusade, in <hl> 1107 <hl>, the <hl> Normans <hl> under "
|
| 161 |
-
"the command of <hl> Bohemond <hl>, Robert\'s son, landed in Valona and "
|
| 162 |
-
"besieged Dyrrachium using the most sophisticated military "
|
| 163 |
-
"equipment of the time, but to no avail. Meanwhile, they occupied "
|
| 164 |
-
"Petrela, <hl> the citadel of Mili <hl> at the banks of the river Deabolis, "
|
| 165 |
-
"Gllavenica (Ballsh), Kanina and Jericho. This time, "
|
| 166 |
-
"the Albanians sided with the Normans, dissatisfied by the heavy "
|
| 167 |
-
"taxes the Byzantines had imposed upon them. With their help, "
|
| 168 |
-
"the Normans secured the Arbanon passes and opened their way to "
|
| 169 |
-
"Dibra. The <hl> lack of supplies, disease and Byzantine resistance <hl> "
|
| 170 |
-
"forced Bohemond to retreat from his campaign and sign a peace "
|
| 171 |
-
"treaty with the Byzantines in the city of Deabolis. ", max_chars=2048,
|
| 172 |
-
height=250)
|
| 173 |
-
|
| 174 |
-
# Split by highlights
|
| 175 |
-
hl_index = [i for i in range(len(inputs)) if inputs.startswith('<hl>', i)]
|
| 176 |
-
contexts = []
|
| 177 |
-
answers = []
|
| 178 |
-
|
| 179 |
-
# Build a context for each highlight pair
|
| 180 |
-
for i in range(0, len(hl_index), 2):
|
| 181 |
-
contexts.append(inputs[:hl_index[i]].replace('<hl>', '') +
|
| 182 |
-
inputs[hl_index[i]: hl_index[i + 1] + 4] +
|
| 183 |
-
inputs[hl_index[i + 1] + 4:].replace('<hl>', ''))
|
| 184 |
-
answers.append(inputs[hl_index[i]: hl_index[i + 1] + 4].replace('<hl>', '').strip())
|
| 185 |
-
|
| 186 |
-
# Prediction
|
| 187 |
with st.spinner('Please wait while the inputs are being processed...'):
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
-
# Answer area
|
| 191 |
-
content = {}
|
| 192 |
-
for pred, ans in zip(predictions, answers):
|
| 193 |
-
content[pred] = ans
|
| 194 |
st.write(content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
ids = {'mt5-small': st.secrets['small'],
|
| 55 |
'mt5-base': st.secrets['base']}
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# Download all models from drive
|
| 59 |
+
download_models(ids)
|
| 60 |
+
|
| 61 |
+
# Task selection
|
| 62 |
+
|
| 63 |
+
left, right = st.columns([4, 2])
|
| 64 |
+
task = left.selectbox('Choose the task: ',
|
| 65 |
+
options=['Questions/Answers Pairs Generation', 'Question Answering', 'Question Generation'],
|
| 66 |
+
help='Choose the task you want to try out')
|
| 67 |
+
|
| 68 |
+
# Model selection
|
| 69 |
+
model_path = right.selectbox('', options=[k for k in ids], index=1, help='Model to use. ')
|
| 70 |
+
model = load_model(model_path=f"model/{model_path}.ckpt")
|
| 71 |
+
right.write(model.device)
|
| 72 |
+
|
| 73 |
+
if task == 'Questions/Answers Pairs Generation':
|
| 74 |
+
# Input area
|
| 75 |
+
inputs = st.text_area('Context:', value="A few years after the First Crusade, in 1107, the Normans under "
|
| 76 |
+
"the command of Bohemond, Robert\'s son, landed in Valona and "
|
| 77 |
+
"besieged Dyrrachium using the most sophisticated military "
|
| 78 |
+
"equipment of the time, but to no avail. Meanwhile, they occupied "
|
| 79 |
+
"Petrela, the citadel of Mili at the banks of the river Deabolis, "
|
| 80 |
+
"Gllavenica (Ballsh), Kanina and Jericho. This time, "
|
| 81 |
+
"the Albanians sided with the Normans, dissatisfied by the heavy "
|
| 82 |
+
"taxes the Byzantines had imposed upon them. With their help, "
|
| 83 |
+
"the Normans secured the Arbanon passes and opened their way to "
|
| 84 |
+
"Dibra. The lack of supplies, disease and Byzantine resistance "
|
| 85 |
+
"forced Bohemond to retreat from his campaign and sign a peace "
|
| 86 |
+
"treaty with the Byzantines in the city of Deabolis. ", max_chars=2048,
|
| 87 |
+
height=250)
|
| 88 |
+
split = st.checkbox('Split into sentences', value=True)
|
| 89 |
+
|
| 90 |
+
if split:
|
| 91 |
+
# Split into sentences
|
| 92 |
+
sent_tokenized = nltk.sent_tokenize(inputs)
|
| 93 |
+
res = {}
|
| 94 |
|
| 95 |
+
with st.spinner('Please wait while the inputs are being processed...'):
|
| 96 |
+
# Iterate over sentences
|
| 97 |
+
for sentence in sent_tokenized:
|
| 98 |
+
predictions = model.multitask([sentence], max_length=512)
|
| 99 |
questions, answers, answers_bis = predictions['questions'], predictions['answers'], predictions[
|
| 100 |
'answers_bis']
|
| 101 |
|
| 102 |
+
# Build answer dict
|
|
|
|
| 103 |
content = {}
|
| 104 |
+
for question, answer, answer_bis in zip(questions[0], answers[0], answers_bis[0]):
|
| 105 |
content[question] = {'answer (extracted)': answer, 'answer (generated)': answer_bis}
|
| 106 |
+
res[sentence] = content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
# Answer area
|
| 109 |
+
st.write(res)
|
| 110 |
+
|
| 111 |
+
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
with st.spinner('Please wait while the inputs are being processed...'):
|
| 113 |
+
# Prediction
|
| 114 |
+
predictions = model.multitask([inputs], max_length=512)
|
| 115 |
+
questions, answers, answers_bis = predictions['questions'], predictions['answers'], predictions[
|
| 116 |
+
'answers_bis']
|
| 117 |
+
|
| 118 |
+
# Answer area
|
| 119 |
+
zip = zip(questions[0], answers[0], answers_bis[0])
|
| 120 |
+
content = {}
|
| 121 |
+
for question, answer, answer_bis in zip:
|
| 122 |
+
content[question] = {'answer (extracted)': answer, 'answer (generated)': answer_bis}
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
st.write(content)
|
| 125 |
+
|
| 126 |
+
elif task == 'Question Answering':
|
| 127 |
+
|
| 128 |
+
# Input area
|
| 129 |
+
inputs = st.text_area('Context:', value="A few years after the First Crusade, in 1107, the Normans under "
|
| 130 |
+
"the command of Bohemond, Robert\'s son, landed in Valona and "
|
| 131 |
+
"besieged Dyrrachium using the most sophisticated military "
|
| 132 |
+
"equipment of the time, but to no avail. Meanwhile, they occupied "
|
| 133 |
+
"Petrela, the citadel of Mili at the banks of the river Deabolis, "
|
| 134 |
+
"Gllavenica (Ballsh), Kanina and Jericho. This time, "
|
| 135 |
+
"the Albanians sided with the Normans, dissatisfied by the heavy "
|
| 136 |
+
"taxes the Byzantines had imposed upon them. With their help, "
|
| 137 |
+
"the Normans secured the Arbanon passes and opened their way to "
|
| 138 |
+
"Dibra. The lack of supplies, disease and Byzantine resistance "
|
| 139 |
+
"forced Bohemond to retreat from his campaign and sign a peace "
|
| 140 |
+
"treaty with the Byzantines in the city of Deabolis. ", max_chars=2048,
|
| 141 |
+
height=250)
|
| 142 |
+
question = st.text_input('Question:', value="What forced Bohemond to retreat from his campaign? ")
|
| 143 |
+
|
| 144 |
+
# Prediction
|
| 145 |
+
with st.spinner('Please wait while the inputs are being processed...'):
|
| 146 |
+
predictions = model.qa([{'question': question, 'context': inputs}], max_length=512)
|
| 147 |
+
answer = {question: predictions[0]}
|
| 148 |
+
|
| 149 |
+
# Answer area
|
| 150 |
+
st.write(answer)
|
| 151 |
+
|
| 152 |
+
elif task == 'Question Generation':
|
| 153 |
+
|
| 154 |
+
# Input area
|
| 155 |
+
inputs = st.text_area('Context (highlight answers with <hl> tokens): ',
|
| 156 |
+
value="A few years after the First Crusade, in <hl> 1107 <hl>, the <hl> Normans <hl> under "
|
| 157 |
+
"the command of <hl> Bohemond <hl>, Robert\'s son, landed in Valona and "
|
| 158 |
+
"besieged Dyrrachium using the most sophisticated military "
|
| 159 |
+
"equipment of the time, but to no avail. Meanwhile, they occupied "
|
| 160 |
+
"Petrela, <hl> the citadel of Mili <hl> at the banks of the river Deabolis, "
|
| 161 |
+
"Gllavenica (Ballsh), Kanina and Jericho. This time, "
|
| 162 |
+
"the Albanians sided with the Normans, dissatisfied by the heavy "
|
| 163 |
+
"taxes the Byzantines had imposed upon them. With their help, "
|
| 164 |
+
"the Normans secured the Arbanon passes and opened their way to "
|
| 165 |
+
"Dibra. The <hl> lack of supplies, disease and Byzantine resistance <hl> "
|
| 166 |
+
"forced Bohemond to retreat from his campaign and sign a peace "
|
| 167 |
+
"treaty with the Byzantines in the city of Deabolis. ", max_chars=2048,
|
| 168 |
+
height=250)
|
| 169 |
+
|
| 170 |
+
# Split by highlights
|
| 171 |
+
hl_index = [i for i in range(len(inputs)) if inputs.startswith('<hl>', i)]
|
| 172 |
+
contexts = []
|
| 173 |
+
answers = []
|
| 174 |
+
|
| 175 |
+
# Build a context for each highlight pair
|
| 176 |
+
for i in range(0, len(hl_index), 2):
|
| 177 |
+
contexts.append(inputs[:hl_index[i]].replace('<hl>', '') +
|
| 178 |
+
inputs[hl_index[i]: hl_index[i + 1] + 4] +
|
| 179 |
+
inputs[hl_index[i + 1] + 4:].replace('<hl>', ''))
|
| 180 |
+
answers.append(inputs[hl_index[i]: hl_index[i + 1] + 4].replace('<hl>', '').strip())
|
| 181 |
+
|
| 182 |
+
# Prediction
|
| 183 |
+
with st.spinner('Please wait while the inputs are being processed...'):
|
| 184 |
+
predictions = model.qg(contexts, max_length=512)
|
| 185 |
+
|
| 186 |
+
# Answer area
|
| 187 |
+
content = {}
|
| 188 |
+
for pred, ans in zip(predictions, answers):
|
| 189 |
+
content[pred] = ans
|
| 190 |
+
st.write(content)
|