smruthi49 commited on
Commit
48fe0f9
·
1 Parent(s): 14f2b08

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +152 -0
app.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+
5
+ from get_next_question import next_question_level
6
+ lvl = {"easy" : 0, "medium" : 1, "hard" : 2}
7
+ idx = ["easy", "medium", "hard"]
8
+
9
+ # Update Ability Score
10
+ def ability(H,L,R,W):
11
+ if R == 0:
12
+ R = 0.5
13
+ W -= 0.5
14
+ elif W == 0:
15
+ W = 0.5
16
+ R -= 0.5
17
+
18
+ score = (H/L) + np.log(R/W)
19
+ return score
20
+
21
+ def fetch_question(fileName, level, visitedQuestions):
22
+ # Difficulty Level
23
+ if (level > 2 or level < 0):
24
+ return None
25
+
26
+ difficulty_level = ["Easy", "Medium", "Hard"][level]
27
+
28
+ # Fetch Data from Excel
29
+ df = pd.read_excel(fileName)
30
+
31
+ # Filter Based On Difficulty
32
+ df_filtered = df[df["Difficulty Level"] == difficulty_level]
33
+
34
+ # Select a Random Question from Filtered Sample
35
+ df_sample = None
36
+ while (df_sample is None) or (df_sample.index[0] in visitedQuestions):
37
+ df_sample = df_filtered.sample(n=1)
38
+
39
+ # Assign Unique Number
40
+ df_sample["id"] = df_sample.index[0]
41
+
42
+ # Question
43
+ question = df_sample.iloc[0].to_dict()
44
+
45
+ return question
46
+
47
+ def next_level(currentLevel, response):
48
+ st.session_state.next_class.response_to_current_question(response["correct"], currentLevel)
49
+ return lvl[st.session_state.next_class.level()]
50
+
51
+ def start_test(fileName):
52
+ # __init__ Values
53
+ while 'next_class' not in st.session_state:
54
+ st.session_state.next_class = next_question_level({'easy':50,'medium':30, 'hard':20})
55
+
56
+ while 'probability' not in st.session_state:
57
+ st.session_state.probability = st.session_state.next_class.get_probabilty
58
+
59
+ while 'score' not in st.session_state:
60
+ st.session_state.score = 0
61
+
62
+ while 'level' not in st.session_state:
63
+ st.session_state.level = lvl[st.session_state.next_class.level()]
64
+
65
+ while 'visited_question' not in st.session_state:
66
+ st.session_state.visited_question = []
67
+
68
+ while 'question_no' not in st.session_state:
69
+ st.session_state.question_no = 1
70
+
71
+ while 'H' not in st.session_state:
72
+ st.session_state.H = 0
73
+
74
+ while 'L' not in st.session_state:
75
+ st.session_state.L = 0
76
+
77
+ while 'R' not in st.session_state:
78
+ st.session_state.R = 0
79
+
80
+ while 'W' not in st.session_state:
81
+ st.session_state.W = 0
82
+
83
+ while 'question' not in st.session_state:
84
+ st.session_state.question = fetch_question(fileName, st.session_state.level, st.session_state.visited_question)
85
+
86
+ question = st.session_state.question
87
+
88
+ with st.form("button_form"):
89
+ # Difficulty Level
90
+ st.write("Question No", st.session_state.question_no)
91
+ st.write("Difficulty Level : ", question["Difficulty Level"], ", Difficulty Rating : ", question["Difficulty Rating"])
92
+
93
+ # Question
94
+ st.write(question["Question"])
95
+
96
+ # Options
97
+ options = st.radio("Options : ", [question["Option1"], question["Option2"],
98
+ question["Option3"], question["Option4"]])
99
+
100
+ # Submit Button
101
+ submit_button = st.form_submit_button("Next Question")
102
+
103
+ # End Button
104
+ end_test = st.form_submit_button("End Test")
105
+
106
+ if submit_button:
107
+ correct = (options == question["Correct Answer"])
108
+ question["correct"] = correct
109
+
110
+ # Response -> Updated Question with Correct or Not
111
+ response = question
112
+
113
+ # Update Score
114
+ st.session_state.score += int(response["correct"])
115
+
116
+ # Update Next State
117
+ st.session_state.visited_question.append(response["id"])
118
+ st.session_state.level = next_level(idx[st.session_state.level], response)
119
+
120
+ # Next Question
121
+ st.session_state.question = fetch_question(fileName, st.session_state.level, st.session_state.visited_question)
122
+ question = st.session_state.question
123
+
124
+ # Update L, H, R, W
125
+ st.session_state.L = st.session_state.question_no
126
+ st.session_state.H = st.session_state.level + 1
127
+ st.session_state.R += int(response["correct"])
128
+ st.session_state.W += int(not response["correct"])
129
+
130
+ # Update Question No
131
+ st.session_state.question_no += 1
132
+
133
+ # Re-Render the Page
134
+ st.experimental_rerun()
135
+
136
+ if end_test:
137
+ st.write("No of Questions : ", st.session_state.question_no - 1)
138
+ st.write("Ability Score : ", ability(st.session_state.H, st.session_state.L, st.session_state.R, st.session_state.W))
139
+ st.write("Correct Answers : ", st.session_state.R)
140
+ st.write("Wrong Answers : ", st.session_state.W)
141
+
142
+ # Probability and Chart
143
+ probabilty = st.session_state.next_class.get_probabilty()
144
+ st.write(probabilty)
145
+
146
+ data = {"Difficulty Level" : ["Easy", "Medium", "Hard"], "Probabilty" : probabilty.values()}
147
+ chart_data = pd.DataFrame(data)
148
+ st.bar_chart(chart_data, x = "Difficulty Level", y = "Probabilty")
149
+
150
+ st.title("SIH_1460 Quiz")
151
+
152
+ start_test("DSA90.xlsx")