Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
-
# streamlit_app.py
|
2 |
import os
|
|
|
3 |
import streamlit as st
|
4 |
from huggingface_hub import InferenceClient
|
5 |
from dotenv import load_dotenv
|
@@ -17,64 +17,99 @@ st.set_page_config(
|
|
17 |
page_icon="🧠",
|
18 |
layout="centered"
|
19 |
)
|
20 |
-
|
21 |
st.title("🎓 Interview Preparation Chatbot")
|
22 |
|
23 |
-
# Session state
|
24 |
-
if "
|
25 |
-
st.session_state.
|
26 |
if "topic" not in st.session_state:
|
27 |
st.session_state.topic = "Machine Learning"
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
# Sidebar: Topic
|
30 |
st.sidebar.header("Practice Topic")
|
31 |
st.session_state.topic = st.sidebar.selectbox(
|
32 |
"Select a topic:",
|
33 |
-
[
|
34 |
-
"Machine Learning",
|
35 |
-
"Data Structures",
|
36 |
-
"Python",
|
37 |
-
"Generative AI",
|
38 |
-
"Computer Vision",
|
39 |
-
"Deep Learning",
|
40 |
-
],
|
41 |
index=["Machine Learning","Data Structures","Python","Generative AI","Computer Vision","Deep Learning"].index(st.session_state.topic)
|
42 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
#
|
45 |
-
for sender, message in st.session_state.history:
|
46 |
-
role = "user" if sender == "You" else "assistant"
|
47 |
-
st.chat_message(role).write(message)
|
48 |
-
|
49 |
-
# User input
|
50 |
-
user_input = st.chat_input("Ask me anything about " + st.session_state.topic + "…")
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
messages.append({"role": role, "content": text})
|
66 |
|
67 |
-
#
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
76 |
|
77 |
-
#
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import json
|
3 |
import streamlit as st
|
4 |
from huggingface_hub import InferenceClient
|
5 |
from dotenv import load_dotenv
|
|
|
17 |
page_icon="🧠",
|
18 |
layout="centered"
|
19 |
)
|
|
|
20 |
st.title("🎓 Interview Preparation Chatbot")
|
21 |
|
22 |
+
# Session state initialization
|
23 |
+
if "questions" not in st.session_state:
|
24 |
+
st.session_state.questions = []
|
25 |
if "topic" not in st.session_state:
|
26 |
st.session_state.topic = "Machine Learning"
|
27 |
+
if "score" not in st.session_state:
|
28 |
+
st.session_state.score = 0
|
29 |
+
if "correct_count" not in st.session_state:
|
30 |
+
st.session_state.correct_count = 0
|
31 |
+
if "incorrect_count" not in st.session_state:
|
32 |
+
st.session_state.incorrect_count = 0
|
33 |
|
34 |
+
# Sidebar: Topic and Score display
|
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")
|
43 |
+
st.sidebar.markdown(f"**Total:** {len(st.session_state.questions)}")
|
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 |
+
# Function to fetch a new MCQ question via HF Inference API
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
def fetch_question(topic):
|
51 |
+
prompt = {
|
52 |
+
"role": "system",
|
53 |
+
"content": (
|
54 |
+
"You are an expert interviewer. "
|
55 |
+
"Generate a multiple-choice question on the topic of {topic}. "
|
56 |
+
"Respond with a JSON object: {\"question\": str, \"options\": [str, ...], \"correct_index\": int}"
|
57 |
+
).format(topic=topic)
|
58 |
+
}
|
59 |
+
response = client.chat.completions.create(
|
60 |
+
model="mistralai/Mistral-7B-Instruct-v0.1",
|
61 |
+
messages=[prompt]
|
62 |
+
)
|
63 |
+
content = response.choices[0].message["content"].strip()
|
64 |
+
try:
|
65 |
+
data = json.loads(content)
|
66 |
+
return data
|
67 |
+
except json.JSONDecodeError:
|
68 |
+
st.error("Failed to parse question. Please try again.")
|
69 |
+
return None
|
70 |
|
71 |
+
# Buttons for initial and next questions
|
72 |
+
if not st.session_state.questions:
|
73 |
+
if st.button("Get Question"):
|
74 |
+
q = fetch_question(st.session_state.topic)
|
75 |
+
if q:
|
76 |
+
st.session_state.questions.append({**q, "selected": None, "submitted": False})
|
77 |
+
else:
|
78 |
+
if st.button("Next Question"):
|
79 |
+
q = fetch_question(st.session_state.topic)
|
80 |
+
if q:
|
81 |
+
st.session_state.questions.append({**q, "selected": None, "submitted": False})
|
82 |
|
83 |
+
# Display questions and capture answers
|
84 |
+
for idx, q in enumerate(st.session_state.questions):
|
85 |
+
st.markdown(f"### Question {idx+1}")
|
86 |
+
st.write(q["question"])
|
|
|
87 |
|
88 |
+
# Radio options
|
89 |
+
opts = q["options"]
|
90 |
+
sel = st.radio(
|
91 |
+
"Choose an answer:",
|
92 |
+
options=list(range(len(opts))),
|
93 |
+
format_func=lambda i: opts[i],
|
94 |
+
index=q["selected"] if q["selected"] is not None else 0,
|
95 |
+
key=f"radio_{idx}",
|
96 |
+
disabled=q["submitted"]
|
97 |
+
)
|
98 |
+
st.session_state.questions[idx]["selected"] = sel
|
99 |
|
100 |
+
# Submit button
|
101 |
+
if not q["submitted"]:
|
102 |
+
if st.button("Submit Answer", key=f"submit_{idx}"):
|
103 |
+
st.session_state.questions[idx]["submitted"] = True
|
104 |
+
if sel == q["correct_index"]:
|
105 |
+
st.success("Correct!")
|
106 |
+
st.session_state.score += 10
|
107 |
+
st.session_state.correct_count += 1
|
108 |
+
else:
|
109 |
+
st.error(f"Incorrect! Correct answer: {opts[q['correct_index']]}.")
|
110 |
+
st.session_state.score -= 10
|
111 |
+
st.session_state.incorrect_count += 1
|
112 |
|
113 |
+
# Footer info
|
114 |
+
st.markdown("---")
|
115 |
+
st.markdown("*Points: +10 for correct, -10 for incorrect.*")
|