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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -16
app.py CHANGED
@@ -3,9 +3,11 @@ import json
3
  import streamlit as st
4
  from huggingface_hub import InferenceClient
5
  from dotenv import load_dotenv
 
6
 
7
  # Load environment variables
8
  load_dotenv()
 
9
  hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
10
  client = InferenceClient(provider="auto", api_key=hf_token)
11
 
@@ -33,8 +35,8 @@ 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
- ["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")
@@ -43,13 +45,14 @@ st.sidebar.markdown(f"**Correct:** {st.session_state.correct_count}")
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",
50
  "content": (
51
  f"You are an expert interviewer. Generate a multiple-choice question on the topic of {topic}. "
52
- "Respond with a JSON object: {\"question\": str, \"options\": [str, ...], \"correct_index\": int}."
53
  )
54
  }
55
  try:
@@ -57,26 +60,34 @@ def fetch_question(topic):
57
  model="mistralai/Mistral-7B-Instruct-v0.1",
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
 
3
  import streamlit as st
4
  from huggingface_hub import InferenceClient
5
  from dotenv import load_dotenv
6
+ import traceback
7
 
8
  # Load environment variables
9
  load_dotenv()
10
+
11
  hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
12
  client = InferenceClient(provider="auto", api_key=hf_token)
13
 
 
35
  st.sidebar.header("Practice Topic")
36
  st.session_state.topic = st.sidebar.selectbox(
37
  "Select a topic:",
38
+ ["Machine Learning", "Data Structures", "Python", "Generative AI", "Computer Vision", "Deep Learning"],
39
+ index=["Machine Learning", "Data Structures", "Python", "Generative AI", "Computer Vision", "Deep Learning"].index(st.session_state.topic)
40
  )
41
  st.sidebar.markdown("---")
42
  st.sidebar.header("Your Score")
 
45
  st.sidebar.markdown(f"**Incorrect:** {st.session_state.incorrect_count}")
46
  st.sidebar.markdown(f"**Points:** {st.session_state.score}")
47
 
48
+ # Function to fetch an MCQ question with enhanced debug logging
49
+
50
  def fetch_question(topic):
51
  prompt = {
52
  "role": "system",
53
  "content": (
54
  f"You are an expert interviewer. Generate a multiple-choice question on the topic of {topic}. "
55
+ "Respond ONLY with a valid JSON object: {\"question\": str, \"options\": [str,...], \"correct_index\": int}."
56
  )
57
  }
58
  try:
 
60
  model="mistralai/Mistral-7B-Instruct-v0.1",
61
  messages=[prompt]
62
  )
63
+ # Debug: show full response for tracing
64
+ st.write("**[DEBUG] Full response object:**")
65
+ st.json(response.to_dict())
66
+ content = response.choices[0].message.get("content", "").strip()
67
+ st.write("**[DEBUG] Raw content:**")
68
  st.code(content)
69
+ except Exception as e:
70
+ st.error(f"Error during API call: {e}")
71
+ st.text(traceback.format_exc())
72
+ return None
73
+ # Attempt JSON parsing
74
+ try:
75
  data = json.loads(content)
76
+ except json.JSONDecodeError as jde:
77
+ st.error(f"JSON decode error: {jde}")
78
+ st.write("**[DEBUG] Content that failed JSON parsing:**")
79
+ st.code(content)
80
+ return None
81
  except Exception as e:
82
+ st.error(f"Unexpected parsing error: {e}")
83
+ st.text(traceback.format_exc())
 
 
 
 
 
84
  return None
85
  # Validate structure
86
  question = data.get("question")
87
  options = data.get("options")
88
  correct_index = data.get("correct_index")
89
+ if not question or not isinstance(options, list) or not isinstance(correct_index, int):
90
+ st.error("Invalid question structure: missing keys or wrong types.")
91
  st.write("**[DEBUG] Parsed JSON:**")
92
  st.json(data)
93
  return None