sunwaee commited on
Commit
bd626a2
1 Parent(s): 014e7da

changed drive links

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