Spaces:
Sleeping
Sleeping
Commit
·
6d313cf
1
Parent(s):
ebd56c6
deepseek R1
Browse files
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 |
-
#
|
7 |
load_dotenv()
|
8 |
|
9 |
-
#
|
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 |
-
|
22 |
-
st.session_state.history = []
|
23 |
|
24 |
def build_messages():
|
25 |
-
|
26 |
-
|
27 |
-
|
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 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
37 |
if sender == "You":
|
38 |
-
st.chat_message("user").write(
|
39 |
else:
|
40 |
-
st.chat_message("assistant").write(
|
41 |
|
42 |
-
#
|
43 |
user_input = st.chat_input("Ask me anything…")
|
44 |
|
45 |
if user_input:
|
46 |
-
#
|
47 |
st.session_state.history.append(("You", user_input))
|
48 |
st.chat_message("user").write(user_input)
|
49 |
|
50 |
-
#
|
51 |
placeholder = st.chat_message("assistant")
|
52 |
placeholder.write("⏳ Thinking...")
|
53 |
|
54 |
-
#
|
55 |
-
full_messages = build_messages()
|
56 |
-
|
57 |
-
# 10. Call the HF Inference API with the full conversation
|
58 |
try:
|
59 |
-
|
60 |
model="deepseek-ai/DeepSeek-R1",
|
61 |
-
messages=
|
62 |
)
|
63 |
-
|
|
|
|
|
64 |
except Exception as e:
|
65 |
reply = f"❌ API Error: {e}"
|
66 |
|
67 |
-
#
|
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))
|