Spaces:
Sleeping
Sleeping
Commit
·
4ba52b5
1
Parent(s):
a428822
deepseek R1
Browse files
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 |
-
#
|
7 |
load_dotenv()
|
8 |
|
9 |
-
#
|
10 |
client = InferenceClient(
|
11 |
provider="auto",
|
12 |
api_key=os.environ["HUGGINGFACEHUB_API_TOKEN"]
|
13 |
)
|
14 |
|
15 |
-
#
|
16 |
-
st.set_page_config(
|
17 |
-
page_title="🎓 Educational Chatbot",
|
18 |
-
layout="wide"
|
19 |
-
)
|
20 |
st.title("🎓 Educational Chatbot")
|
21 |
|
22 |
-
#
|
23 |
if "history" not in st.session_state:
|
24 |
-
st.session_state.history = []
|
25 |
|
26 |
-
#
|
27 |
-
for sender,
|
28 |
-
|
29 |
-
|
|
|
|
|
30 |
|
31 |
-
#
|
32 |
user_input = st.chat_input("Ask me anything…")
|
33 |
|
34 |
if user_input:
|
35 |
-
#
|
36 |
st.chat_message("user").write(user_input)
|
37 |
st.session_state.history.append(("You", user_input))
|
38 |
|
39 |
-
#
|
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 |
-
#
|
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))
|