Imsachinsingh00 commited on
Commit
ebd56c6
·
1 Parent(s): 4ba52b5

deepseek R1

Browse files
Files changed (1) hide show
  1. app.py +30 -15
app.py CHANGED
@@ -3,52 +3,67 @@ import streamlit as st
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}],
47
  )
48
  reply = completion.choices[0].message["content"]
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))
 
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))