Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,81 @@
|
|
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
from huggingface_hub import InferenceClient
|
4 |
from dotenv import load_dotenv
|
5 |
|
6 |
-
# Load environment variables
|
7 |
load_dotenv()
|
8 |
|
9 |
-
# Initialize
|
10 |
-
|
11 |
-
client = InferenceClient(provider="auto", api_key=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
st.set_page_config(page_title="Interview Prep Bot", layout="wide")
|
14 |
st.title("π Interview Preparation Chatbot")
|
15 |
|
16 |
-
#
|
17 |
if "history" not in st.session_state:
|
18 |
st.session_state.history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
#
|
21 |
-
for sender,
|
22 |
role = "user" if sender == "You" else "assistant"
|
23 |
-
st.chat_message(role).write(
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
if text:
|
28 |
-
# record user
|
29 |
-
st.session_state.history.append(("You", text))
|
30 |
-
st.chat_message("user").write(text)
|
31 |
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
33 |
placeholder = st.chat_message("assistant")
|
34 |
placeholder.write("β³ Thinking...")
|
35 |
|
36 |
-
#
|
37 |
messages = []
|
38 |
-
for
|
39 |
-
|
40 |
-
messages.append({"role": role, "content":
|
41 |
|
42 |
-
#
|
43 |
try:
|
44 |
-
|
45 |
model="mistralai/Mistral-7B-Instruct-v0.1",
|
46 |
-
messages=messages
|
47 |
)
|
48 |
-
|
49 |
except Exception as e:
|
50 |
-
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
|
53 |
-
placeholder.write(reply)
|
54 |
-
st.session_state.history.append(("Bot", reply))
|
|
|
1 |
+
# streamlit_app.py
|
2 |
import os
|
3 |
import streamlit as st
|
4 |
from huggingface_hub import InferenceClient
|
5 |
from dotenv import load_dotenv
|
6 |
|
7 |
+
# Load environment variables for local development
|
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 |
+
|
14 |
+
# Streamlit configuration
|
15 |
+
st.set_page_config(
|
16 |
+
page_title="Interview Prep Bot",
|
17 |
+
page_icon="π§ ",
|
18 |
+
layout="centered"
|
19 |
+
)
|
20 |
|
|
|
21 |
st.title("π Interview Preparation Chatbot")
|
22 |
|
23 |
+
# Session state for conversation history and selected topic
|
24 |
if "history" not in st.session_state:
|
25 |
st.session_state.history = []
|
26 |
+
if "topic" not in st.session_state:
|
27 |
+
st.session_state.topic = "Machine Learning"
|
28 |
+
|
29 |
+
# Sidebar: Topic selection
|
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 |
+
# Display existing chat history
|
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 |
+
if user_input:
|
53 |
+
# Append user message
|
54 |
+
st.session_state.history.append(("You", user_input))
|
55 |
+
st.chat_message("user").write(user_input)
|
56 |
+
|
57 |
+
# Placeholder for bot response
|
58 |
placeholder = st.chat_message("assistant")
|
59 |
placeholder.write("β³ Thinking...")
|
60 |
|
61 |
+
# Build messages for the API call
|
62 |
messages = []
|
63 |
+
for role, text in [("user", msg) if s == "You" else ("assistant", msg)
|
64 |
+
for s, msg in st.session_state.history]:
|
65 |
+
messages.append({"role": role, "content": text})
|
66 |
|
67 |
+
# Call the Inference API
|
68 |
try:
|
69 |
+
response = client.chat.completions.create(
|
70 |
model="mistralai/Mistral-7B-Instruct-v0.1",
|
71 |
+
messages=messages
|
72 |
)
|
73 |
+
bot_reply = response.choices[0].message["content"].strip()
|
74 |
except Exception as e:
|
75 |
+
bot_reply = f"β API Error: {e}"
|
76 |
+
|
77 |
+
# Display and store bot reply
|
78 |
+
placeholder.write(bot_reply)
|
79 |
+
st.session_state.history.append(("Bot", bot_reply))
|
80 |
|
81 |
+
```
|
|
|
|