-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
342 lines (271 loc) · 12.7 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import streamlit as st
import plotly.graph_objects as go
import pandas as pd
import os
from tqdm import tqdm
import re
from IPython.display import display, Markdown
from PIL import Image
import os
import numpy as np
#---------Templates for CSS ---------
html_with_css = """
<style>
body {
font-family: Arial, sans-serif;
}
p {
line-height: 1.5;
}
</style>
"""
#----------Settings-----------
page_title = "H2 Physics Question Bank"
page_icon = ":books:"
layout = "centered"
#----------------------------
st.set_page_config(page_title=page_title, page_icon=page_icon, layout=layout)
st.title(page_title + " " + page_icon)
#---- Drop Down Values for Selecting the period -----
@st.cache_data
def load_data(url):
df = pd.read_csv(url)
return df
url = "https://firebasestorage.googleapis.com/v0/b/studyszn.appspot.com/o/H2_physics_vectorised.csv?alt=media"
df = load_data(url)
@st.cache_data
def apply_transformation(data):
data['Embeddings'] = data['Embeddings'].apply(lambda x: np.array(eval(x)), 0)
return data
df = apply_transformation(df)
def display_open_ended_question(content, base_url="https://firebasestorage.googleapis.com/v0/b/studyszn.appspot.com/o/"):
# Use a regular expression to find all placeholders like [image1], [image2], etc.
placeholders = re.findall(r'\[image\d+\]', content)
# Dynamically replace placeholders with image tags
for placeholder in placeholders:
# Extract the image number from the placeholder
image_number = int(re.search(r'\d+', placeholder).group())
# Create the image tag with the specified URL
image_tag = f''
# Replace the placeholder with the image tag
content = content.replace(placeholder, image_tag)
# Create an HTML object and display it
return content
def isOpenEndedQn(df, questionNo):
return (pd.isnull(df.iloc[questionNo]['Option A Image']) and pd.isnull(df.iloc[questionNo]['Option A']))
def isImageMCQ(df, questionNo):
return pd.notnull(df.iloc[questionNo]['Option A Image'])
def isMCQWithoutOptions(df,questionNo):
search_text = "<blockquote>\n<p> </p>\n</blockquote>\n"
option_a_text = str(df.iloc[questionNo]['Option A'])
return search_text in option_a_text
def Image_MCQ_replace_image_tags(row):
text = row['Question']
for Letter in ["A","B","C","D"]:
text = text + "\n" + "[" + row[f'Option {Letter} Image'] + "] "
for i in range(1, 7):
try:
if(pd.isnull(row[f'Qimage {i}'])):
continue
tag = f'[image{i}]'
replacement = "[" + row[f'Qimage {i}'] + "] " #need a space right after!
text = text.replace(tag, replacement)
except KeyError:
pass # Handle the case where the Qimage column doesn't exist
return text
def MCQ_text_Parser(row,text):
pattern = r'<p>(.*?)<\/p>'
for Letter in ["A","B","C","D"]:
option = row[f'Option {Letter}']
matches = re.findall(pattern, option, re.DOTALL)
extracted_content = [match.strip() for match in matches]
text = text + "\n" + "\n" + Letter + "\n" + ": " + extracted_content[0] + "\n"
return text
def replace_image_tags(row):
text = row['Question']
for i in range(1, 7):
try:
if(pd.isnull(row[f'Qimage {i}'])):
continue
tag = f'[image{i}]'
replacement = "[" + row[f'Qimage {i}'] + "] " #need a space right after!
text = text.replace(tag, replacement)
except KeyError:
pass # Handle the case where the Qimage column doesn't exist
return text
def parseQuestion(df, questionNum):
if (isOpenEndedQn(df,questionNum)):
content = replace_image_tags(df.iloc[questionNum])
return (display_open_ended_question(content))
else:
row = df.iloc[questionNum]
#is an mcq question
if (isImageMCQ(df,questionNum)):
return (display_open_ended_question(Image_MCQ_replace_image_tags(row)))
elif (isMCQWithoutOptions(df,questionNum)):
return display_open_ended_question(replace_image_tags(df.iloc[questionNum]))
else:
content = replace_image_tags(df.iloc[questionNum])
return (display_open_ended_question(MCQ_text_Parser(df.iloc[questionNum],content)))
def replace_image_tags_answers(row):
text = row['Answer Open']
#text = text.replace("<p>", "").replace("</p>", "")
for i in range(1, 7):
try:
if(pd.isnull(row[f'Answer Image{i}'])):
continue
tag = f'[image{i}]'
replacement = "[" + row[f'Answer Image{i}'] + "] " #need a space right after!
text = text.replace(tag, replacement)
except KeyError:
pass # Handle the case where the Qimage column doesn't exist
return text
def display_open_ended_answers(content, base_url="https://firebasestorage.googleapis.com/v0/b/studyszn.appspot.com/o/"):
# Use a regular expression to find all placeholders like [image1], [image2], etc.
placeholders = re.findall(r'\[image\d+\]', content)
# Dynamically replace placeholders with HTML <img> tags
for placeholder in placeholders:
# Extract the image number from the placeholder
image_number = int(re.search(r'\d+', placeholder).group())
# Create the HTML <img> tag with the specified URL
img_tag = f'<img src="{base_url}image{image_number}.png?alt=media" alt="image">'
# Replace the placeholder with the HTML <img> tag
content = content.replace(placeholder, img_tag)
# Create an HTML object and display it
return content
def parseAnswer(df, questionNum):
if (isOpenEndedQn(df,questionNum)):
content = replace_image_tags_answers(df.iloc[questionNum])
return (display_open_ended_answers(content))
elif not (isOpenEndedQn(df,questionNum)):
row = df.iloc[questionNum]
ans = "The answer to this MCQ question is: " + str(df.iloc[questionNum]['Answer Option'])
return (ans)
else:
raise Exception("Error!")
def find_most_similar_question(questionnum, dataframe):
tempdf = df.drop(df.iloc[questionnum].name)
dot_products = np.dot(np.stack(tempdf['Embeddings']), dataframe.iloc[questionnum]['Embeddings'])
idx = np.argmax(dot_products) +1
return idx
def find_most_similar_questions(questionnum, dataframe, top_n=5):
# Create a DataFrame without the target question
tempdf = dataframe.drop(dataframe.index[questionnum])
# Calculate dot products with the target question's embedding
dot_products = np.dot(np.stack(tempdf['Embeddings']), dataframe.iloc[questionnum]['Embeddings'])
# Get the indices of the top N most similar questions
top_indices = np.argpartition(dot_products, -top_n)[-top_n:]
# Sort the top indices by dot product values in descending order
top_indices = top_indices[np.argsort(-dot_products[top_indices])]
# Return the top N most similar question indices (add 1 to match question numbering)
most_similar_indices = top_indices + 1
return list(most_similar_indices)
def get_topic(questionnum, dataframe=df):
return dataframe.iloc[questionnum]['topic']
def get_topics(questionnum, dataframe=df):
return dataframe.iloc[questionnum]['related_topics'].strip('[]').replace("'", "")
def find_indices_containing_string(search_string, dataframe=df):
# Use the str.contains() method to check for the presence of the search_string in the 'Question' column
mask = dataframe['Question'].str.contains(search_string, case=False)
# Get the indices where the mask is True
indices = mask[mask].index.tolist()
return indices
"---"
st.header("Search for a question that contains certain keywords")
if "similarQs" not in st.session_state:
st.session_state.similarQs =0
if "current_question" not in st.session_state:
st.session_state.current_question = -1
if "similar_question_indices" not in st.session_state:
st.session_state.similar_question_indices =[]
if "search_list" not in st.session_state:
st.session_state.search_list =[]
if "search_list_mcq" not in st.session_state:
st.session_state.search_list_mcq =[]
if "search_list_oeq" not in st.session_state:
st.session_state.search_list_oeq =[]
st.markdown(
"""
<style>
.stButton button {
width: 100% !important; /* Make the button take up the entire column width */
background-color: green; /* Green background */
color: white; /* White text color */
}
</style>
""",
unsafe_allow_html=True,
)
with st.form("Type keywords that you wish to search for:", clear_on_submit=False):
# Add an input field for the user to enter a question
question = st.text_input("Searching for question that contains keywords...")
# Add a submit button
if st.form_submit_button("Submit"):
try:
questionstring = str(question)
#update state
st.session_state.similarQs = 1
st.session_state.search_list = find_indices_containing_string(questionstring)
except ValueError:
st.error("Invalid input, please key in a valid string")
key = 0
if "button_clicked" not in st.session_state:
st.session_state.button_clicked = False
def callback():
#button was clicked!
st.session_state.button_clicked = True
if len(st.session_state.search_list) >0:
for i in st.session_state.search_list:
if not (isOpenEndedQn(df,i)):
st.session_state.search_list_mcq.append(i)
else:
st.session_state.search_list_oeq.append(i)
st.header("MCQ Questions")
for i in st.session_state.search_list_mcq:
val = int(key)
key +=10
boxexpander = st.expander(f"Question{i+1}")
with boxexpander:
question_output = html_with_css + parseQuestion(df, i)
st.markdown(question_output,unsafe_allow_html=True)
if st.checkbox(f"Show Answer for Question {i+1}", key=val+1):
answer_output = html_with_css + parseAnswer(df, i)
st.markdown(answer_output, unsafe_allow_html=True)
viewsimilar = st.button("View Similar Questions", key = val+2, on_click=callback)
if viewsimilar or st.session_state.button_clicked:
similar_question_indices = find_most_similar_questions(i, df, top_n=5)
tab_labels = [f"Question {i+1}" for i in similar_question_indices]
for j,tab in enumerate(st.tabs(tab_labels)):
with tab:
question_output = html_with_css + parseQuestion(df,similar_question_indices[j])
st.markdown(question_output,unsafe_allow_html=True)
if st.checkbox(f"Show answer for Question {similar_question_indices[j]+1}", key=val+3+j):
answer_output = html_with_css + parseAnswer(df, similar_question_indices[j])
st.markdown(answer_output, unsafe_allow_html=True)
st.divider()
st.header("Open Ended Questions")
for i in st.session_state.search_list_oeq:
val = int(key)
key +=10
boxexpander = st.expander(f"Question{i+1}")
with boxexpander:
question_output = html_with_css + parseQuestion(df, i)
st.markdown(question_output,unsafe_allow_html=True)
if st.checkbox(f"Show Answer for Question {i+1}", key =key+1):
key+=1
answer_output = html_with_css + parseAnswer(df, i)
st.markdown(answer_output, unsafe_allow_html=True)
viewsimilar = st.button("View Similar Questions", key = val+2, on_click=callback)
if viewsimilar or st.session_state.button_clicked:
similar_question_indices = find_most_similar_questions(i, df, top_n=5)
tab_labels = [f"Question {i+1}" for i in similar_question_indices]
for j,tab in enumerate(st.tabs(tab_labels)):
with tab:
question_output = html_with_css + parseQuestion(df,similar_question_indices[j])
st.markdown(question_output,unsafe_allow_html=True)
if st.checkbox(f"Show answer for Question {similar_question_indices[j]+1}", key=val+3+j):
answer_output = html_with_css + parseAnswer(df, similar_question_indices[j])
st.markdown(answer_output, unsafe_allow_html=True)
#rest all indices for next search
st.session_state.search_list_mcq =[]
st.session_state.search_list_oeq =[]