Spaces:
Running
Running
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 (for local development)
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
# Initialize InferenceClient
|
10 |
+
token = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
11 |
+
client = InferenceClient(provider="auto", api_key=token)
|
12 |
+
|
13 |
+
st.set_page_config(page_title="Interview Prep Bot", layout="wide")
|
14 |
+
st.title("🎓 Interview Preparation Chatbot")
|
15 |
+
|
16 |
+
# Initialize history
|
17 |
+
if "history" not in st.session_state:
|
18 |
+
st.session_state.history = []
|
19 |
+
|
20 |
+
# Render chat history
|
21 |
+
for sender, msg in st.session_state.history:
|
22 |
+
role = "user" if sender == "You" else "assistant"
|
23 |
+
st.chat_message(role).write(msg)
|
24 |
+
|
25 |
+
# Prompt input
|
26 |
+
text = st.chat_input("Ask me anything about interview prep…")
|
27 |
+
if text:
|
28 |
+
# record user
|
29 |
+
st.session_state.history.append(("You", text))
|
30 |
+
st.chat_message("user").write(text)
|
31 |
+
|
32 |
+
# placeholder
|
33 |
+
placeholder = st.chat_message("assistant")
|
34 |
+
placeholder.write("⏳ Thinking...")
|
35 |
+
|
36 |
+
# build messages for API
|
37 |
+
messages = []
|
38 |
+
for s, m in st.session_state.history:
|
39 |
+
role = "user" if s == "You" else "assistant"
|
40 |
+
messages.append({"role": role, "content": m})
|
41 |
+
|
42 |
+
# call HF chat completion
|
43 |
+
try:
|
44 |
+
completion = client.chat.completions.create(
|
45 |
+
model="mistralai/Mistral-7B-Instruct-v0.1",
|
46 |
+
messages=messages,
|
47 |
+
)
|
48 |
+
reply = completion.choices[0].message["content"].strip()
|
49 |
+
except Exception as e:
|
50 |
+
reply = f"❌ API Error: {e}"
|
51 |
+
|
52 |
+
# display and store reply
|
53 |
+
placeholder.write(reply)
|
54 |
+
st.session_state.history.append(("Bot", reply))
|