Imsachinsingh00 commited on
Commit
ab5d533
·
verified ·
1 Parent(s): 1a89091

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -25
app.py CHANGED
@@ -33,24 +33,9 @@ if "incorrect_count" not in st.session_state:
33
  st.sidebar.header("Practice Topic")
34
  st.session_state.topic = st.sidebar.selectbox(
35
  "Select a topic:",
36
- [
37
- "Machine Learning",
38
- "Data Structures",
39
- "Python",
40
- "Generative AI",
41
- "Computer Vision",
42
- "Deep Learning",
43
- ],
44
- index=[
45
- "Machine Learning",
46
- "Data Structures",
47
- "Python",
48
- "Generative AI",
49
- "Computer Vision",
50
- "Deep Learning",
51
- ].index(st.session_state.topic)
52
  )
53
-
54
  st.sidebar.markdown("---")
55
  st.sidebar.header("Your Score")
56
  st.sidebar.markdown(f"**Total:** {len(st.session_state.questions)}")
@@ -58,8 +43,7 @@ st.sidebar.markdown(f"**Correct:** {st.session_state.correct_count}")
58
  st.sidebar.markdown(f"**Incorrect:** {st.session_state.incorrect_count}")
59
  st.sidebar.markdown(f"**Points:** {st.session_state.score}")
60
 
61
- # Function to fetch an MCQ question
62
-
63
  def fetch_question(topic):
64
  prompt = {
65
  "role": "system",
@@ -74,16 +58,27 @@ def fetch_question(topic):
74
  messages=[prompt]
75
  )
76
  content = response.choices[0].message["content"].strip()
 
 
 
77
  data = json.loads(content)
78
  except Exception as e:
79
  st.error(f"Failed to fetch or parse question: {e}")
 
 
 
 
 
 
80
  return None
81
  # Validate structure
82
  question = data.get("question")
83
  options = data.get("options")
84
  correct_index = data.get("correct_index")
85
  if not question or not isinstance(options, list) or correct_index is None:
86
- st.error(f"Invalid question structure. Response: {content}")
 
 
87
  return None
88
  return {"question": question, "options": options, "correct_index": correct_index}
89
 
@@ -103,8 +98,6 @@ else:
103
  for idx, q in enumerate(st.session_state.questions):
104
  st.markdown(f"### Question {idx+1}")
105
  st.write(q["question"])
106
-
107
- # Radio options
108
  opts = q["options"]
109
  sel = st.radio(
110
  "Choose an answer:",
@@ -115,8 +108,6 @@ for idx, q in enumerate(st.session_state.questions):
115
  disabled=q["submitted"]
116
  )
117
  st.session_state.questions[idx]["selected"] = sel
118
-
119
- # Submit answer
120
  if not q["submitted"]:
121
  if st.button("Submit Answer", key=f"submit_{idx}"):
122
  st.session_state.questions[idx]["submitted"] = True
@@ -128,7 +119,6 @@ for idx, q in enumerate(st.session_state.questions):
128
  st.error(f"Incorrect! Correct: {opts[q['correct_index']]} (-10 points)")
129
  st.session_state.score -= 10
130
  st.session_state.incorrect_count += 1
131
-
132
  # Footer
133
  st.markdown("---")
134
  st.markdown("*Correct: +10 pts | Incorrect: -10 pts*")
 
33
  st.sidebar.header("Practice Topic")
34
  st.session_state.topic = st.sidebar.selectbox(
35
  "Select a topic:",
36
+ ["Machine Learning","Data Structures","Python","Generative AI","Computer Vision","Deep Learning"],
37
+ index=["Machine Learning","Data Structures","Python","Generative AI","Computer Vision","Deep Learning"].index(st.session_state.topic)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  )
 
39
  st.sidebar.markdown("---")
40
  st.sidebar.header("Your Score")
41
  st.sidebar.markdown(f"**Total:** {len(st.session_state.questions)}")
 
43
  st.sidebar.markdown(f"**Incorrect:** {st.session_state.incorrect_count}")
44
  st.sidebar.markdown(f"**Points:** {st.session_state.score}")
45
 
46
+ # Function to fetch an MCQ question with debug logging
 
47
  def fetch_question(topic):
48
  prompt = {
49
  "role": "system",
 
58
  messages=[prompt]
59
  )
60
  content = response.choices[0].message["content"].strip()
61
+ # Debug: show raw response
62
+ st.write("**[DEBUG] Raw response:**")
63
+ st.code(content)
64
  data = json.loads(content)
65
  except Exception as e:
66
  st.error(f"Failed to fetch or parse question: {e}")
67
+ # If content exists, display it for debugging
68
+ try:
69
+ st.write("**[DEBUG] Last content before error:**")
70
+ st.code(content)
71
+ except Exception:
72
+ pass
73
  return None
74
  # Validate structure
75
  question = data.get("question")
76
  options = data.get("options")
77
  correct_index = data.get("correct_index")
78
  if not question or not isinstance(options, list) or correct_index is None:
79
+ st.error("Invalid question structure.")
80
+ st.write("**[DEBUG] Parsed JSON:**")
81
+ st.json(data)
82
  return None
83
  return {"question": question, "options": options, "correct_index": correct_index}
84
 
 
98
  for idx, q in enumerate(st.session_state.questions):
99
  st.markdown(f"### Question {idx+1}")
100
  st.write(q["question"])
 
 
101
  opts = q["options"]
102
  sel = st.radio(
103
  "Choose an answer:",
 
108
  disabled=q["submitted"]
109
  )
110
  st.session_state.questions[idx]["selected"] = sel
 
 
111
  if not q["submitted"]:
112
  if st.button("Submit Answer", key=f"submit_{idx}"):
113
  st.session_state.questions[idx]["submitted"] = True
 
119
  st.error(f"Incorrect! Correct: {opts[q['correct_index']]} (-10 points)")
120
  st.session_state.score -= 10
121
  st.session_state.incorrect_count += 1
 
122
  # Footer
123
  st.markdown("---")
124
  st.markdown("*Correct: +10 pts | Incorrect: -10 pts*")