Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,10 +4,8 @@ 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 |
-
|
10 |
-
# Initialize the Hugging Face Inference client
|
11 |
hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
12 |
client = InferenceClient(provider="auto", api_key=hf_token)
|
13 |
|
@@ -19,7 +17,7 @@ st.set_page_config(
|
|
19 |
)
|
20 |
st.title("🎓 Interview Preparation Chatbot")
|
21 |
|
22 |
-
#
|
23 |
if "questions" not in st.session_state:
|
24 |
st.session_state.questions = []
|
25 |
if "topic" not in st.session_state:
|
@@ -31,13 +29,28 @@ if "correct_count" not in st.session_state:
|
|
31 |
if "incorrect_count" not in st.session_state:
|
32 |
st.session_state.incorrect_count = 0
|
33 |
|
34 |
-
# Sidebar: Topic and
|
35 |
st.sidebar.header("Practice Topic")
|
36 |
st.session_state.topic = st.sidebar.selectbox(
|
37 |
"Select a topic:",
|
38 |
-
[
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
)
|
|
|
41 |
st.sidebar.markdown("---")
|
42 |
st.sidebar.header("Your Score")
|
43 |
st.sidebar.markdown(f"**Total:** {len(st.session_state.questions)}")
|
@@ -45,30 +58,36 @@ 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
|
49 |
|
50 |
def fetch_question(topic):
|
51 |
prompt = {
|
52 |
"role": "system",
|
53 |
"content": (
|
54 |
-
"You are an expert interviewer. "
|
55 |
-
"
|
56 |
-
|
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 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
return None
|
|
|
70 |
|
71 |
-
# Buttons
|
72 |
if not st.session_state.questions:
|
73 |
if st.button("Get Question"):
|
74 |
q = fetch_question(st.session_state.topic)
|
@@ -97,19 +116,19 @@ for idx, q in enumerate(st.session_state.questions):
|
|
97 |
)
|
98 |
st.session_state.questions[idx]["selected"] = sel
|
99 |
|
100 |
-
# Submit
|
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
|
110 |
st.session_state.score -= 10
|
111 |
st.session_state.incorrect_count += 1
|
112 |
|
113 |
-
# Footer
|
114 |
st.markdown("---")
|
115 |
-
st.markdown("*
|
|
|
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 |
|
|
|
17 |
)
|
18 |
st.title("🎓 Interview Preparation Chatbot")
|
19 |
|
20 |
+
# Initialize session state
|
21 |
if "questions" not in st.session_state:
|
22 |
st.session_state.questions = []
|
23 |
if "topic" not in st.session_state:
|
|
|
29 |
if "incorrect_count" not in st.session_state:
|
30 |
st.session_state.incorrect_count = 0
|
31 |
|
32 |
+
# Sidebar: Topic selection and scoring
|
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 |
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",
|
66 |
"content": (
|
67 |
+
f"You are an expert interviewer. Generate a multiple-choice question on the topic of {topic}. "
|
68 |
+
"Respond with a JSON object: {\"question\": str, \"options\": [str, ...], \"correct_index\": int}."
|
69 |
+
)
|
|
|
70 |
}
|
|
|
|
|
|
|
|
|
|
|
71 |
try:
|
72 |
+
response = client.chat.completions.create(
|
73 |
+
model="mistralai/Mistral-7B-Instruct-v0.1",
|
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 |
|
90 |
+
# Buttons to get or advance questions
|
91 |
if not st.session_state.questions:
|
92 |
if st.button("Get Question"):
|
93 |
q = fetch_question(st.session_state.topic)
|
|
|
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
|
123 |
if sel == q["correct_index"]:
|
124 |
+
st.success("Correct! +10 points")
|
125 |
st.session_state.score += 10
|
126 |
st.session_state.correct_count += 1
|
127 |
else:
|
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*")
|