Spaces:
Sleeping
Sleeping
Commit
·
a428822
1
Parent(s):
5e45b81
deepseek R1
Browse files
app.py
CHANGED
@@ -3,41 +3,53 @@ import streamlit as st
|
|
3 |
from huggingface_hub import InferenceClient
|
4 |
from dotenv import load_dotenv
|
5 |
|
|
|
6 |
load_dotenv()
|
7 |
-
client = InferenceClient(provider="auto", api_key=os.environ["HUGGINGFACEHUB_API_TOKEN"])
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
st.title("🎓 Educational Chatbot")
|
11 |
|
|
|
12 |
if "history" not in st.session_state:
|
13 |
-
st.session_state.history = []
|
14 |
|
15 |
-
#
|
16 |
for sender, msg in st.session_state.history:
|
17 |
role = "user" if sender == "You" else "assistant"
|
18 |
st.chat_message(role).write(msg)
|
19 |
|
20 |
-
#
|
21 |
user_input = st.chat_input("Ask me anything…")
|
|
|
22 |
if user_input:
|
23 |
-
# a)
|
24 |
st.chat_message("user").write(user_input)
|
25 |
st.session_state.history.append(("You", user_input))
|
26 |
|
27 |
-
# b)
|
28 |
placeholder = st.chat_message("assistant")
|
29 |
-
placeholder.write("⏳ Thinking
|
30 |
|
31 |
-
# c)
|
32 |
try:
|
33 |
completion = client.chat.completions.create(
|
34 |
model="deepseek-ai/DeepSeek-R1",
|
35 |
-
messages=[{"role":"user","content":user_input}],
|
36 |
)
|
37 |
reply = completion.choices[0].message["content"]
|
38 |
except Exception as e:
|
39 |
reply = f"❌ API Error: {e}"
|
40 |
|
41 |
-
# d)
|
42 |
placeholder.write(reply)
|
43 |
st.session_state.history.append(("Bot", reply))
|
|
|
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}],
|
48 |
)
|
49 |
reply = completion.choices[0].message["content"]
|
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))
|