Imsachinsingh00 commited on
Commit
83f6cb8
·
1 Parent(s): 92cf3e7

deepseek R1

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. app.py +25 -12
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
app.py CHANGED
@@ -3,27 +3,44 @@ import streamlit as st
3
  from huggingface_hub import InferenceClient
4
  from dotenv import load_dotenv
5
 
6
- # 1. Load .env (if using)
7
  load_dotenv()
8
 
9
- # 2. Instantiate the InferenceClient once
10
  client = InferenceClient(
11
  provider="auto",
12
  api_key=os.environ["HUGGINGFACEHUB_API_TOKEN"]
13
  )
14
 
15
- st.set_page_config(page_title="Educational Chatbot")
 
16
  st.title("🎓 Educational Chatbot")
17
 
 
18
  if "history" not in st.session_state:
19
  st.session_state.history = []
20
 
21
- # 3. Chat input
22
- user_input = st.text_input("Ask me anything:")
 
 
 
 
 
 
 
23
 
24
  if user_input:
 
 
 
 
 
 
 
 
25
  try:
26
- # 4. Make the chat-completion API call
27
  completion = client.chat.completions.create(
28
  model="deepseek-ai/DeepSeek-R1",
29
  messages=[{"role": "user", "content": user_input}],
@@ -32,10 +49,6 @@ if user_input:
32
  except Exception as e:
33
  reply = f"❌ API Error: {e}"
34
 
35
- # 5. Append to session history
36
- st.session_state.history.append(("You", user_input))
37
  st.session_state.history.append(("Bot", reply))
38
-
39
- # 6. Render chat history
40
- for sender, msg in reversed(st.session_state.history):
41
- st.markdown(f"**{sender}:** {msg}")
 
3
  from huggingface_hub import InferenceClient
4
  from dotenv import load_dotenv
5
 
6
+ # Load .env (if you’re using one)
7
  load_dotenv()
8
 
9
+ # Instantiate once
10
  client = InferenceClient(
11
  provider="auto",
12
  api_key=os.environ["HUGGINGFACEHUB_API_TOKEN"]
13
  )
14
 
15
+ # Page config
16
+ st.set_page_config(page_title="Educational Chatbot", layout="wide")
17
  st.title("🎓 Educational Chatbot")
18
 
19
+ # Initialize history
20
  if "history" not in st.session_state:
21
  st.session_state.history = []
22
 
23
+ # Render existing messages
24
+ for sender, message in st.session_state.history:
25
+ if sender == "You":
26
+ st.chat_message("user").write(message)
27
+ else:
28
+ st.chat_message("assistant").write(message)
29
+
30
+ # Input box
31
+ user_input = st.chat_input("Ask me anything…")
32
 
33
  if user_input:
34
+ # Display user message immediately
35
+ st.chat_message("user").write(user_input)
36
+ st.session_state.history.append(("You", user_input))
37
+
38
+ # Bot placeholder
39
+ placeholder = st.chat_message("assistant")
40
+ placeholder.write("⏳ Thinking...")
41
+
42
  try:
43
+ # Call HF Inference API
44
  completion = client.chat.completions.create(
45
  model="deepseek-ai/DeepSeek-R1",
46
  messages=[{"role": "user", "content": user_input}],
 
49
  except Exception as e:
50
  reply = f"❌ API Error: {e}"
51
 
52
+ # Update placeholder with real response
53
+ placeholder.write(reply)
54
  st.session_state.history.append(("Bot", reply))