Spaces:
Running
Running
lingyit1108
commited on
Commit
β’
c81f36b
1
Parent(s):
9b5e886
added logical reasoning
Browse files- database/mock_qna.sqlite +1 -1
- qna_prompting.py +53 -44
- streamlit_app.py +7 -3
database/mock_qna.sqlite
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
size 40960
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b1b4ac6a58713069f3545b7894a99e15587940b08ca42645a5146daf377456d4
|
3 |
size 40960
|
qna_prompting.py
CHANGED
@@ -64,10 +64,11 @@ def get_qna_question(chapter_n: str) -> str:
|
|
64 |
cur = con.cursor()
|
65 |
|
66 |
filter_clause = "WHERE a.id IS NULL" if chapter_n == "Chapter_0" else f"WHERE a.id IS NULL AND chapter='{chapter_n}'"
|
67 |
-
sql_string = """SELECT q.id, question, option_1, option_2, option_3, option_4, q.correct_answer
|
68 |
FROM qna_tbl q LEFT JOIN answer_tbl a
|
69 |
ON q.id = a.id
|
70 |
""" + filter_clause
|
|
|
71 |
|
72 |
res = cur.execute(sql_string)
|
73 |
result = res.fetchone()
|
@@ -79,6 +80,7 @@ def get_qna_question(chapter_n: str) -> str:
|
|
79 |
option_3 = result[4]
|
80 |
option_4 = result[5]
|
81 |
c_answer = result[6]
|
|
|
82 |
|
83 |
qna_str = "As requested, here is the retrieved question: \n" + \
|
84 |
"============================================= \n" + \
|
@@ -90,6 +92,7 @@ def get_qna_question(chapter_n: str) -> str:
|
|
90 |
|
91 |
st.session_state.question_id = id
|
92 |
st.session_state.qna_answer = c_answer
|
|
|
93 |
|
94 |
con.close()
|
95 |
|
@@ -97,49 +100,55 @@ def get_qna_question(chapter_n: str) -> str:
|
|
97 |
|
98 |
def evaluate_qna_answer(user_selected_answer: str) -> str:
|
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 |
return qna_answer_response
|
145 |
|
|
|
64 |
cur = con.cursor()
|
65 |
|
66 |
filter_clause = "WHERE a.id IS NULL" if chapter_n == "Chapter_0" else f"WHERE a.id IS NULL AND chapter='{chapter_n}'"
|
67 |
+
sql_string = """SELECT q.id, question, option_1, option_2, option_3, option_4, q.correct_answer, q.reasoning
|
68 |
FROM qna_tbl q LEFT JOIN answer_tbl a
|
69 |
ON q.id = a.id
|
70 |
""" + filter_clause
|
71 |
+
# sql_string = sql_string + " ORDER BY RANDOM() LIMIT 1"
|
72 |
|
73 |
res = cur.execute(sql_string)
|
74 |
result = res.fetchone()
|
|
|
80 |
option_3 = result[4]
|
81 |
option_4 = result[5]
|
82 |
c_answer = result[6]
|
83 |
+
reasons = result[7]
|
84 |
|
85 |
qna_str = "As requested, here is the retrieved question: \n" + \
|
86 |
"============================================= \n" + \
|
|
|
92 |
|
93 |
st.session_state.question_id = id
|
94 |
st.session_state.qna_answer = c_answer
|
95 |
+
st.session_state.reasons = reasons
|
96 |
|
97 |
con.close()
|
98 |
|
|
|
100 |
|
101 |
def evaluate_qna_answer(user_selected_answer: str) -> str:
|
102 |
|
103 |
+
try:
|
104 |
+
answer_mapping = {
|
105 |
+
"A": 1,
|
106 |
+
"B": 2,
|
107 |
+
"C": 3,
|
108 |
+
"D": 4,
|
109 |
+
"Z": 0
|
110 |
+
}
|
111 |
+
num_mapping = dict((v,k) for k,v in answer_mapping.items())
|
112 |
+
user_answer_numeric = answer_mapping.get(user_selected_answer, 0)
|
113 |
+
|
114 |
+
question_id = st.session_state.question_id
|
115 |
+
qna_answer = st.session_state.qna_answer
|
116 |
+
reasons = st.session_state.reasons
|
117 |
+
|
118 |
+
qna_answer_alphabet = num_mapping.get(int(qna_answer), "ERROR")
|
119 |
+
|
120 |
+
con = sqlite3.connect(db_path)
|
121 |
+
cur = con.cursor()
|
122 |
+
sql_string = f"""INSERT INTO answer_tbl
|
123 |
+
VALUES ({question_id}, {qna_answer}, {user_answer_numeric})
|
124 |
+
"""
|
125 |
+
|
126 |
+
res = cur.execute(sql_string)
|
127 |
+
con.commit()
|
128 |
+
con.close()
|
129 |
+
|
130 |
+
if qna_answer == user_answer_numeric:
|
131 |
+
st.toast("π― yummy yummy, hooray!", icon="π")
|
132 |
+
time.sleep(2)
|
133 |
+
st.toast("π»ππ― You got it right!", icon="π")
|
134 |
+
time.sleep(2)
|
135 |
+
st.toast("π₯ You are amazing! π―π―", icon="πͺ")
|
136 |
+
st.balloons()
|
137 |
+
else:
|
138 |
+
st.toast("πΌ Something doesn't seem right.. π₯π π₯", icon="π")
|
139 |
+
time.sleep(2)
|
140 |
+
st.toast("π₯Ά Are you sure..? π¬π¬", icon="π")
|
141 |
+
time.sleep(2)
|
142 |
+
st.toast("π€π€ Nevertheless, it was a good try!! ποΈββοΈποΈββοΈ", icon="π")
|
143 |
+
st.snow()
|
144 |
+
|
145 |
+
reasoning = "" if "textbook" in reasons else "Rationale is that: " + reasons
|
146 |
+
qna_answer_response = (
|
147 |
+
f"Your selected answer is `{user_selected_answer}`, "
|
148 |
+
f"but the actual answer is `{qna_answer_alphabet}`. " + reasoning
|
149 |
+
)
|
150 |
+
except Exception as e:
|
151 |
+
print(e)
|
152 |
|
153 |
return qna_answer_response
|
154 |
|
streamlit_app.py
CHANGED
@@ -117,9 +117,10 @@ with st.sidebar:
|
|
117 |
os.environ["OPENAI_API_KEY"] = openai_api
|
118 |
|
119 |
st.subheader("Models and parameters")
|
120 |
-
selected_model = st.sidebar.selectbox("Choose an OpenAI model",
|
121 |
-
["gpt-
|
122 |
-
|
|
|
123 |
temperature = st.sidebar.slider("temperature", min_value=0.0, max_value=2.0,
|
124 |
value=0.0, step=0.01)
|
125 |
st.data_editor(
|
@@ -160,6 +161,9 @@ if "question_id" not in st.session_state:
|
|
160 |
if "qna_answer" not in st.session_state:
|
161 |
st.session_state.qna_answer = None
|
162 |
|
|
|
|
|
|
|
163 |
def clear_chat_history():
|
164 |
|
165 |
st.session_state.messages = [{"role": "assistant",
|
|
|
117 |
os.environ["OPENAI_API_KEY"] = openai_api
|
118 |
|
119 |
st.subheader("Models and parameters")
|
120 |
+
selected_model = st.sidebar.selectbox(label="Choose an OpenAI model",
|
121 |
+
options=["gpt-3.5-turbo-0125", "gpt-4-0125-preview"],
|
122 |
+
index=0,
|
123 |
+
key="selected_model")
|
124 |
temperature = st.sidebar.slider("temperature", min_value=0.0, max_value=2.0,
|
125 |
value=0.0, step=0.01)
|
126 |
st.data_editor(
|
|
|
161 |
if "qna_answer" not in st.session_state:
|
162 |
st.session_state.qna_answer = None
|
163 |
|
164 |
+
if "reasons" not in st.session_state:
|
165 |
+
st.session_state.reasons = None
|
166 |
+
|
167 |
def clear_chat_history():
|
168 |
|
169 |
st.session_state.messages = [{"role": "assistant",
|