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

deepseek R1

Browse files
Files changed (1) hide show
  1. app.py +18 -19
app.py CHANGED
@@ -3,45 +3,44 @@ import streamlit as st
3
  from huggingface_hub import InferenceClient
4
  from dotenv import load_dotenv
5
 
6
- # 1. Load environment variables from .env (if used)
7
  load_dotenv()
8
 
9
- # 2. Instantiate the HF 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(
17
- page_title="🎓 Educational Chatbot",
18
- layout="wide"
19
- )
20
  st.title("🎓 Educational Chatbot")
21
 
22
- # 4. Initialize chat history
23
  if "history" not in st.session_state:
24
- st.session_state.history = [] # list of (sender, message)
25
 
26
- # 5. Render existing chat messages
27
- for sender, msg in st.session_state.history:
28
- role = "user" if sender == "You" else "assistant"
29
- st.chat_message(role).write(msg)
 
 
30
 
31
- # 6. Chat input
32
  user_input = st.chat_input("Ask me anything…")
33
 
34
  if user_input:
35
- # a) Echo user message
36
  st.chat_message("user").write(user_input)
37
  st.session_state.history.append(("You", user_input))
38
 
39
- # b) Show a single assistant placeholder
40
  placeholder = st.chat_message("assistant")
41
- placeholder.write("⏳ Thinking")
42
 
43
- # c) Call HF Inference API
44
  try:
 
45
  completion = client.chat.completions.create(
46
  model="deepseek-ai/DeepSeek-R1",
47
  messages=[{"role": "user", "content": user_input}],
@@ -50,6 +49,6 @@ if user_input:
50
  except Exception as e:
51
  reply = f"❌ API Error: {e}"
52
 
53
- # d) Replace placeholder with real reply
54
  placeholder.write(reply)
55
  st.session_state.history.append(("Bot", reply))
 
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))