Spaces:
Sleeping
Sleeping
Commit
·
5e45b81
1
Parent(s):
83f6cb8
deepseek R1
Browse files
app.py
CHANGED
@@ -3,52 +3,41 @@ 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 |
-
|
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 |
-
#
|
24 |
-
for sender,
|
25 |
-
if sender == "You"
|
26 |
-
|
27 |
-
else:
|
28 |
-
st.chat_message("assistant").write(message)
|
29 |
|
30 |
-
#
|
31 |
user_input = st.chat_input("Ask me anything…")
|
32 |
-
|
33 |
if user_input:
|
34 |
-
#
|
35 |
st.chat_message("user").write(user_input)
|
36 |
st.session_state.history.append(("You", user_input))
|
37 |
|
38 |
-
#
|
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":
|
47 |
)
|
48 |
reply = completion.choices[0].message["content"]
|
49 |
except Exception as e:
|
50 |
reply = f"❌ API Error: {e}"
|
51 |
|
52 |
-
#
|
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 |
load_dotenv()
|
7 |
+
client = InferenceClient(provider="auto", api_key=os.environ["HUGGINGFACEHUB_API_TOKEN"])
|
8 |
|
9 |
+
st.set_page_config(page_title="Educational Chatbot")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
st.title("🎓 Educational Chatbot")
|
11 |
|
|
|
12 |
if "history" not in st.session_state:
|
13 |
st.session_state.history = []
|
14 |
|
15 |
+
# 1) Show existing chat
|
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 |
+
# 2) Ask for new input
|
21 |
user_input = st.chat_input("Ask me anything…")
|
|
|
22 |
if user_input:
|
23 |
+
# a) echo user
|
24 |
st.chat_message("user").write(user_input)
|
25 |
st.session_state.history.append(("You", user_input))
|
26 |
|
27 |
+
# b) typing indicator
|
28 |
placeholder = st.chat_message("assistant")
|
29 |
placeholder.write("⏳ Thinking...")
|
30 |
|
31 |
+
# c) get actual reply
|
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) replace placeholder with real answer
|
42 |
placeholder.write(reply)
|
43 |
st.session_state.history.append(("Bot", reply))
|