Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,129 +7,153 @@ import traceback
|
|
7 |
|
8 |
# Load environment variables
|
9 |
load_dotenv()
|
10 |
-
|
11 |
hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
st.set_page_config(
|
16 |
-
page_title="Interview Prep Bot",
|
17 |
-
page_icon="π§ ",
|
18 |
-
layout="centered"
|
19 |
-
)
|
20 |
st.title("π Interview Preparation Chatbot")
|
21 |
|
22 |
-
#
|
23 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
st.session_state.questions = []
|
25 |
-
|
26 |
-
|
27 |
-
if "score" not in st.session_state:
|
28 |
st.session_state.score = 0
|
29 |
-
|
|
|
30 |
st.session_state.correct_count = 0
|
31 |
-
|
|
|
32 |
st.session_state.incorrect_count = 0
|
33 |
|
34 |
-
|
35 |
-
st.
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
)
|
41 |
st.sidebar.markdown("---")
|
42 |
-
st.sidebar.header("Your Score")
|
43 |
-
st.sidebar.markdown(f"**
|
44 |
st.sidebar.markdown(f"**Correct:** {st.session_state.correct_count}")
|
45 |
st.sidebar.markdown(f"**Incorrect:** {st.session_state.incorrect_count}")
|
46 |
st.sidebar.markdown(f"**Points:** {st.session_state.score}")
|
47 |
|
48 |
-
#
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
102 |
else:
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
st.markdown(f"### Question {idx+1}")
|
111 |
-
st.write(q["question"])
|
112 |
-
opts = q["options"]
|
113 |
-
sel = st.radio(
|
114 |
-
"Choose an answer:",
|
115 |
-
options=list(range(len(opts))),
|
116 |
-
format_func=lambda i: opts[i],
|
117 |
-
index=q["selected"] if q["selected"] is not None else 0,
|
118 |
-
key=f"radio_{idx}",
|
119 |
disabled=q["submitted"]
|
120 |
)
|
121 |
-
st.session_state.
|
|
|
122 |
if not q["submitted"]:
|
123 |
-
if st.button("Submit Answer"
|
124 |
-
|
125 |
-
if
|
126 |
-
st.success("Correct! +10 points")
|
127 |
st.session_state.score += 10
|
128 |
st.session_state.correct_count += 1
|
129 |
else:
|
130 |
-
|
|
|
131 |
st.session_state.score -= 10
|
132 |
st.session_state.incorrect_count += 1
|
133 |
-
|
134 |
-
st.
|
135 |
-
st.
|
|
|
|
7 |
|
8 |
# Load environment variables
|
9 |
load_dotenv()
|
|
|
10 |
hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
11 |
+
|
12 |
+
# Setup Streamlit
|
13 |
+
st.set_page_config(page_title="Interview Prep Bot", page_icon="π§ ", layout="centered")
|
|
|
|
|
|
|
|
|
|
|
14 |
st.title("π Interview Preparation Chatbot")
|
15 |
|
16 |
+
# Token check
|
17 |
+
if not hf_token:
|
18 |
+
st.error("Token not found. Check your .env file.")
|
19 |
+
st.stop()
|
20 |
+
|
21 |
+
model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
22 |
+
try:
|
23 |
+
client = InferenceClient(model=model_id, token=hf_token)
|
24 |
+
st.success("π Connected to Hugging Face Inference API.")
|
25 |
+
except Exception as e:
|
26 |
+
st.error(f"Failed to initialize InferenceClient: {e}")
|
27 |
+
st.stop()
|
28 |
+
|
29 |
+
# Debug reset button (useful once to fix corrupted state)
|
30 |
+
if st.button("π Reset App"):
|
31 |
+
for key in list(st.session_state.keys()):
|
32 |
+
del st.session_state[key]
|
33 |
+
st.rerun()
|
34 |
+
|
35 |
+
# Sidebar topic & stats
|
36 |
+
topics = [
|
37 |
+
"Machine Learning",
|
38 |
+
"Data Structures",
|
39 |
+
"Python",
|
40 |
+
"Generative AI",
|
41 |
+
"Computer Vision",
|
42 |
+
"Deep Learning"
|
43 |
+
]
|
44 |
+
st.sidebar.header("π Select Topic")
|
45 |
+
topic = st.sidebar.selectbox("Topic:", topics)
|
46 |
+
|
47 |
+
# Safely initialize session state
|
48 |
+
if "questions" not in st.session_state or not isinstance(st.session_state.questions, list):
|
49 |
st.session_state.questions = []
|
50 |
+
|
51 |
+
if "score" not in st.session_state or not isinstance(st.session_state.score, int):
|
|
|
52 |
st.session_state.score = 0
|
53 |
+
|
54 |
+
if "correct_count" not in st.session_state or not isinstance(st.session_state.correct_count, int):
|
55 |
st.session_state.correct_count = 0
|
56 |
+
|
57 |
+
if "incorrect_count" not in st.session_state or not isinstance(st.session_state.incorrect_count, int):
|
58 |
st.session_state.incorrect_count = 0
|
59 |
|
60 |
+
if "active_question" not in st.session_state:
|
61 |
+
st.session_state.active_question = None
|
62 |
+
|
63 |
+
if "last_action" not in st.session_state:
|
64 |
+
st.session_state.last_action = ""
|
65 |
+
|
|
|
66 |
st.sidebar.markdown("---")
|
67 |
+
st.sidebar.header("π Your Score")
|
68 |
+
st.sidebar.markdown(f"**Questions:** {len(st.session_state.questions)}")
|
69 |
st.sidebar.markdown(f"**Correct:** {st.session_state.correct_count}")
|
70 |
st.sidebar.markdown(f"**Incorrect:** {st.session_state.incorrect_count}")
|
71 |
st.sidebar.markdown(f"**Points:** {st.session_state.score}")
|
72 |
|
73 |
+
# Fetch unique MCQ
|
74 |
+
def fetch_mcq(topic, past_questions=None, max_retries=3):
|
75 |
+
if past_questions is None:
|
76 |
+
past_questions = set(q["question"] for q in st.session_state.questions)
|
77 |
+
|
78 |
+
prompt_template = (
|
79 |
+
f"Generate a multiple-choice question about {topic}. "
|
80 |
+
"Return only JSON with keys: question (string), options (4 strings), correct_index (0-based integer). "
|
81 |
+
"Make the question unique and different from this list:\n" +
|
82 |
+
json.dumps(list(past_questions))
|
83 |
+
)
|
84 |
+
|
85 |
+
for _ in range(max_retries):
|
86 |
+
try:
|
87 |
+
response = client.chat_completion(
|
88 |
+
model=model_id,
|
89 |
+
messages=[
|
90 |
+
{"role": "system", "content": "You are a helpful MCQ bot that returns JSON only."},
|
91 |
+
{"role": "user", "content": prompt_template}
|
92 |
+
]
|
93 |
+
)
|
94 |
+
content = response.choices[0].message.get("content", "").strip()
|
95 |
+
data = json.loads(content)
|
96 |
+
|
97 |
+
new_question = data.get("question", "").strip()
|
98 |
+
if new_question in past_questions:
|
99 |
+
continue
|
100 |
+
|
101 |
+
return {
|
102 |
+
"question": new_question,
|
103 |
+
"options": data["options"],
|
104 |
+
"correct_index": data["correct_index"],
|
105 |
+
"selected": None,
|
106 |
+
"submitted": False
|
107 |
+
}
|
108 |
+
|
109 |
+
except Exception as e:
|
110 |
+
st.error("β Error fetching MCQ")
|
111 |
+
st.text(traceback.format_exc())
|
112 |
+
return None
|
113 |
+
|
114 |
+
st.warning("β οΈ Couldn't generate a unique question after several tries.")
|
115 |
+
return None
|
116 |
+
|
117 |
+
# Handle logic based on last action
|
118 |
+
if st.session_state.last_action == "submit":
|
119 |
+
new_q = fetch_mcq(topic)
|
120 |
+
if new_q:
|
121 |
+
st.session_state.active_question = new_q
|
122 |
+
st.session_state.last_action = ""
|
123 |
+
|
124 |
+
# Render active question or show start button
|
125 |
+
q = st.session_state.active_question
|
126 |
+
|
127 |
+
if not q:
|
128 |
+
if st.button("π§ Start Interview"):
|
129 |
+
new_q = fetch_mcq(topic)
|
130 |
+
if new_q:
|
131 |
+
st.session_state.active_question = new_q
|
132 |
else:
|
133 |
+
st.markdown(f"### β {q['question']}")
|
134 |
+
choice = st.radio(
|
135 |
+
"Choose your answer:",
|
136 |
+
range(4),
|
137 |
+
format_func=lambda i: q["options"][i],
|
138 |
+
index=q.get("selected", 0),
|
139 |
+
key="answer",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
disabled=q["submitted"]
|
141 |
)
|
142 |
+
st.session_state.active_question["selected"] = choice
|
143 |
+
|
144 |
if not q["submitted"]:
|
145 |
+
if st.button("β
Submit Answer"):
|
146 |
+
q["submitted"] = True
|
147 |
+
if choice == q["correct_index"]:
|
148 |
+
st.success("β
Correct! +10 points")
|
149 |
st.session_state.score += 10
|
150 |
st.session_state.correct_count += 1
|
151 |
else:
|
152 |
+
correct_ans = q["options"][q["correct_index"]]
|
153 |
+
st.error(f"β Incorrect. Correct answer: {correct_ans} (-10 points)")
|
154 |
st.session_state.score -= 10
|
155 |
st.session_state.incorrect_count += 1
|
156 |
+
|
157 |
+
st.session_state.questions.append(q)
|
158 |
+
st.session_state.last_action = "submit"
|
159 |
+
st.rerun() # <== Force rerun after submission
|