Imsachinsingh00 commited on
Commit
6d313cf
·
1 Parent(s): ebd56c6

deepseek R1

Browse files
Files changed (1) hide show
  1. app.py +26 -30
app.py CHANGED
@@ -1,69 +1,65 @@
1
  import os
 
2
  import streamlit as st
3
  from huggingface_hub import InferenceClient
4
  from dotenv import load_dotenv
5
 
6
- # 1. Load your HF token from .env
7
  load_dotenv()
8
 
9
- # 2. Instantiate the Hugging Face Inference client once
10
  client = InferenceClient(
11
  provider="auto",
12
  api_key=os.environ["HUGGINGFACEHUB_API_TOKEN"]
13
  )
14
 
15
- # 3. Streamlit page config
16
  st.set_page_config(page_title="Educational Chatbot", layout="wide")
17
  st.title("🎓 Educational Chatbot")
18
 
19
- # 4. Initialize chat history in session state
20
  if "history" not in st.session_state:
21
- # history is a list of (sender, message) tuples
22
- st.session_state.history = []
23
 
24
  def build_messages():
25
- """
26
- Convert session_state.history into the list of {"role", "content"} dicts
27
- that the Hugging Face chat API expects.
28
- """
29
- msgs = []
30
- for sender, text in st.session_state.history:
31
- role = "user" if sender == "You" else "assistant"
32
- msgs.append({"role": role, "content": text})
33
- return msgs
34
 
35
- # 5. Render the existing chat as chat bubbles
36
- for sender, text in st.session_state.history:
 
 
 
 
37
  if sender == "You":
38
- st.chat_message("user").write(text)
39
  else:
40
- st.chat_message("assistant").write(text)
41
 
42
- # 6. Get new user input
43
  user_input = st.chat_input("Ask me anything…")
44
 
45
  if user_input:
46
- # 7. Immediately record & display the user message
47
  st.session_state.history.append(("You", user_input))
48
  st.chat_message("user").write(user_input)
49
 
50
- # 8. Show a placeholder for the assistant response
51
  placeholder = st.chat_message("assistant")
52
  placeholder.write("⏳ Thinking...")
53
 
54
- # 9. Build the full message history to send
55
- full_messages = build_messages()
56
-
57
- # 10. Call the HF Inference API with the full conversation
58
  try:
59
- completion = client.chat.completions.create(
60
  model="deepseek-ai/DeepSeek-R1",
61
- messages=full_messages,
62
  )
63
- reply = completion.choices[0].message["content"]
 
 
64
  except Exception as e:
65
  reply = f"❌ API Error: {e}"
66
 
67
- # 11. Update the placeholder and session history
68
  placeholder.write(reply)
69
  st.session_state.history.append(("Bot", reply))
 
1
  import os
2
+ import re
3
  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
+ # Instantiate the HF Inference client
11
  client = InferenceClient(
12
  provider="auto",
13
  api_key=os.environ["HUGGINGFACEHUB_API_TOKEN"]
14
  )
15
 
 
16
  st.set_page_config(page_title="Educational Chatbot", layout="wide")
17
  st.title("🎓 Educational Chatbot")
18
 
 
19
  if "history" not in st.session_state:
20
+ st.session_state.history = [] # list of (sender, message)
 
21
 
22
  def build_messages():
23
+ return [
24
+ {"role": "user" if s == "You" else "assistant", "content": m}
25
+ for s, m in st.session_state.history
26
+ ]
 
 
 
 
 
27
 
28
+ def clean_think_tags(text: str) -> str:
29
+ # remove <think>...</think> blocks
30
+ return re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL).strip()
31
+
32
+ # Render chat history
33
+ for sender, msg in st.session_state.history:
34
  if sender == "You":
35
+ st.chat_message("user").write(msg)
36
  else:
37
+ st.chat_message("assistant").write(msg)
38
 
39
+ # Input
40
  user_input = st.chat_input("Ask me anything…")
41
 
42
  if user_input:
43
+ # show user turn
44
  st.session_state.history.append(("You", user_input))
45
  st.chat_message("user").write(user_input)
46
 
47
+ # placeholder for assistant
48
  placeholder = st.chat_message("assistant")
49
  placeholder.write("⏳ Thinking...")
50
 
51
+ # call HF chat endpoint with entire history
 
 
 
52
  try:
53
+ response = client.chat.completions.create(
54
  model="deepseek-ai/DeepSeek-R1",
55
+ messages=build_messages()
56
  )
57
+ raw = response.choices[0].message["content"]
58
+ # clean out think tags
59
+ reply = clean_think_tags(raw)
60
  except Exception as e:
61
  reply = f"❌ API Error: {e}"
62
 
63
+ # display and store cleaned reply
64
  placeholder.write(reply)
65
  st.session_state.history.append(("Bot", reply))