Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| from get_next_question import next_question_level | |
| lvl = {"easy" : 0, "medium" : 1, "hard" : 2} | |
| idx = ["easy", "medium", "hard"] | |
| # Limit | |
| limit = 15 | |
| # Update Ability Score | |
| def ability(H,L,R,W): | |
| if R == 0: | |
| R = 0.5 | |
| W -= 0.5 | |
| elif W == 0: | |
| W = 0.5 | |
| R -= 0.5 | |
| score = (H/L) + np.log(R/W) | |
| return score | |
| def fetch_question(fileName, level, visitedQuestions): | |
| # Difficulty Level | |
| if (level > 2 or level < 0): | |
| return None | |
| difficulty_level = ["Easy", "Medium", "Hard"][level] | |
| # Fetch Data from Excel | |
| df = pd.read_excel(fileName) | |
| # Filter Based On Difficulty | |
| df_filtered = df[df["Difficulty Level"] == difficulty_level] | |
| # Select a Random Question from Filtered Sample | |
| df_sample = None | |
| while (df_sample is None) or (df_sample.index[0] in visitedQuestions): | |
| df_sample = df_filtered.sample(n=1) | |
| # Assign Unique Number | |
| df_sample["id"] = df_sample.index[0] | |
| # Question | |
| question = df_sample.iloc[0].to_dict() | |
| return question | |
| def next_level(currentLevel, response): | |
| st.session_state.next_class.response_to_current_question(response["correct"], currentLevel) | |
| return lvl[st.session_state.next_class.level()] | |
| def start_test(fileName): | |
| # __init__ Values | |
| while 'next_class' not in st.session_state: | |
| st.session_state.next_class = next_question_level({'easy':50,'medium':30, 'hard':20}) | |
| while 'probability' not in st.session_state: | |
| st.session_state.probability = st.session_state.next_class.get_probabilty | |
| while 'score' not in st.session_state: | |
| st.session_state.score = 0 | |
| while 'level' not in st.session_state: | |
| st.session_state.level = lvl[st.session_state.next_class.level()] | |
| while 'visited_question' not in st.session_state: | |
| st.session_state.visited_question = [] | |
| while 'question_no' not in st.session_state: | |
| st.session_state.question_no = 1 | |
| while 'H' not in st.session_state: | |
| st.session_state.H = 0 | |
| while 'L' not in st.session_state: | |
| st.session_state.L = 0 | |
| while 'R' not in st.session_state: | |
| st.session_state.R = 0 | |
| while 'W' not in st.session_state: | |
| st.session_state.W = 0 | |
| while 'question' not in st.session_state: | |
| st.session_state.question = fetch_question(fileName, st.session_state.level, st.session_state.visited_question) | |
| question = st.session_state.question | |
| with st.form("button_form"): | |
| # Difficulty Level | |
| st.write("Question No", st.session_state.question_no) | |
| st.write("Difficulty Level : ", question["Difficulty Level"], ", Difficulty Rating : ", question["Difficulty Rating"]) | |
| # Question | |
| st.write(question["Question"]) | |
| # Options | |
| options = st.radio("Options : ", [question["Option1"], question["Option2"], | |
| question["Option3"], question["Option4"]]) | |
| #### | |
| st.write("Correct Answer : ",question["Correct Answer"]) | |
| # Submit Button | |
| submit_button = st.form_submit_button("Next Question", disabled=(st.session_state.question_no >= limit)) | |
| # End Button | |
| end_test = st.form_submit_button("End Test", disabled=(st.session_state.question_no < limit)) | |
| if submit_button: | |
| correct = (options == question["Correct Answer"]) | |
| question["correct"] = correct | |
| # Response -> Updated Question with Correct or Not | |
| response = question | |
| # Update Score | |
| st.session_state.score += int(response["correct"]) | |
| # Update L, H, R, W | |
| st.session_state.L = st.session_state.question_no | |
| st.session_state.H = st.session_state.level + 1 | |
| st.session_state.R += int(response["correct"]) | |
| st.session_state.W += int(not response["correct"]) | |
| # Update Question No | |
| st.session_state.question_no += 1 | |
| # Update Next State | |
| st.session_state.visited_question.append(response["id"]) | |
| st.session_state.level = next_level(idx[st.session_state.level], response) | |
| # Next Question | |
| st.session_state.question = fetch_question(fileName, st.session_state.level, st.session_state.visited_question) | |
| question = st.session_state.question | |
| # Re-Render the Page | |
| st.experimental_rerun() | |
| if end_test: | |
| correct = (options == question["Correct Answer"]) | |
| question["correct"] = correct | |
| # Response -> Updated Question with Correct or Not | |
| response = question | |
| # Update Score | |
| st.session_state.score += int(response["correct"]) | |
| # Update L, H, R, W | |
| st.session_state.L = st.session_state.question_no | |
| st.session_state.H = st.session_state.level + 1 | |
| st.session_state.R += int(response["correct"]) | |
| st.session_state.W += int(not response["correct"]) | |
| # Display Values | |
| st.write("No of Questions : ", st.session_state.question_no) | |
| st.write("Ability Score : ", ability(st.session_state.H, st.session_state.L, st.session_state.R, st.session_state.W)) | |
| st.write("Correct Answers : ", st.session_state.R) | |
| st.write("Wrong Answers : ", st.session_state.W) | |
| # Reset Values | |
| st.session_state.next_class = next_question_level({'easy':50,'medium':30, 'hard':20}) | |
| st.session_state.probability = st.session_state.next_class.get_probabilty | |
| st.session_state.score = 0 | |
| st.session_state.level = lvl[st.session_state.next_class.level()] | |
| st.session_state.visited_question = [] | |
| st.session_state.question_no = 1 | |
| st.session_state.H = 0 | |
| st.session_state.L = 0 | |
| st.session_state.R = 0 | |
| st.session_state.W = 0 | |
| st.session_state.question = fetch_question(fileName, st.session_state.level, st.session_state.visited_question) | |
| # Re-Test Button | |
| re_test = st.button("Attempt Again") | |
| if re_test: | |
| st.session_state.clicked = False | |
| # Re-Render the Page | |
| st.experimental_rerun() | |
| # Probability and Chart | |
| probabilty = st.session_state.next_class.get_probabilty() | |
| data = {"Difficulty Level" : ["1 - Easy", "2 - Medium", "3 - Hard"], "Probabilty" : probabilty.values()} | |
| chart_data = pd.DataFrame(data) | |
| st.bar_chart(chart_data, x = "Difficulty Level", y = "Probabilty") | |
| st.title("Adaptive Quiz") | |
| start_test("DSA90.xlsx") |