Imsachinsingh00 commited on
Commit
78cbc5c
Β·
verified Β·
1 Parent(s): c931f25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -28
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 (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))
 
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
+ ```