Spaces:
Sleeping
Sleeping
Update pages/deep_learning.py
Browse files- pages/deep_learning.py +19 -25
pages/deep_learning.py
CHANGED
@@ -3,27 +3,20 @@ import os
|
|
3 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
4 |
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
|
5 |
|
6 |
-
|
7 |
-
|
8 |
hf = os.getenv('Data_science')
|
9 |
os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
|
10 |
os.environ['HF_TOKEN'] = hf
|
11 |
-
|
|
|
12 |
st.set_page_config(page_title="AI Mentor Chat", layout="centered")
|
13 |
st.title("🤖 AI Mentor Chat")
|
14 |
|
15 |
-
# --- Sidebar
|
16 |
st.sidebar.title("Mentor Preferences")
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
# Map experience to label
|
21 |
-
experience_map = {
|
22 |
-
'<1': 'New bie mentor',
|
23 |
-
'1': '1', '2': '2', '3': '3', '4': '4', '5': '5',
|
24 |
-
'5+': 'Professional'
|
25 |
-
}
|
26 |
-
experience_label = experience_map[exp]
|
27 |
|
28 |
# --- Initialize Chat Model ---
|
29 |
deep_seek_skeleton = HuggingFaceEndpoint(
|
@@ -42,31 +35,32 @@ deep_seek = ChatHuggingFace(
|
|
42 |
max_new_tokens=150,
|
43 |
task='conversational'
|
44 |
)
|
45 |
-
|
|
|
|
|
46 |
# --- Session State ---
|
47 |
if PAGE_KEY not in st.session_state:
|
48 |
st.session_state[PAGE_KEY] = []
|
49 |
|
50 |
-
# --- Chat Form ---
|
51 |
with st.form(key="chat_form"):
|
52 |
user_input = st.text_input("Ask your question:")
|
53 |
submit = st.form_submit_button("Send")
|
54 |
|
55 |
-
# --- Chat
|
56 |
if submit and user_input:
|
57 |
-
#
|
58 |
-
system_prompt =
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
# Create message list
|
61 |
messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
|
62 |
-
|
63 |
-
# Get model response
|
64 |
result = deep_seek.invoke(messages)
|
65 |
-
|
66 |
-
# Append to history
|
67 |
st.session_state[PAGE_KEY].append((user_input, result.content))
|
68 |
|
69 |
-
# ---
|
70 |
st.subheader("🗨️ Chat History")
|
71 |
for user, bot in st.session_state[PAGE_KEY]:
|
72 |
st.markdown(f"**You:** {user}")
|
|
|
3 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
4 |
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
|
5 |
|
6 |
+
# Load Hugging Face token
|
|
|
7 |
hf = os.getenv('Data_science')
|
8 |
os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
|
9 |
os.environ['HF_TOKEN'] = hf
|
10 |
+
|
11 |
+
# --- Page Configuration ---
|
12 |
st.set_page_config(page_title="AI Mentor Chat", layout="centered")
|
13 |
st.title("🤖 AI Mentor Chat")
|
14 |
|
15 |
+
# --- Sidebar: Experience Level ---
|
16 |
st.sidebar.title("Mentor Preferences")
|
17 |
+
experience_label = st.sidebar.selectbox(
|
18 |
+
"Select your experience level:", ["Beginner", "Intermediate", "Experienced"]
|
19 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
# --- Initialize Chat Model ---
|
22 |
deep_seek_skeleton = HuggingFaceEndpoint(
|
|
|
35 |
max_new_tokens=150,
|
36 |
task='conversational'
|
37 |
)
|
38 |
+
|
39 |
+
PAGE_KEY = "deep_learning_chat_history"
|
40 |
+
|
41 |
# --- Session State ---
|
42 |
if PAGE_KEY not in st.session_state:
|
43 |
st.session_state[PAGE_KEY] = []
|
44 |
|
45 |
+
# --- Chat Input Form ---
|
46 |
with st.form(key="chat_form"):
|
47 |
user_input = st.text_input("Ask your question:")
|
48 |
submit = st.form_submit_button("Send")
|
49 |
|
50 |
+
# --- Chat Handling ---
|
51 |
if submit and user_input:
|
52 |
+
# System context based on selected experience level
|
53 |
+
system_prompt = (
|
54 |
+
f"Act as a deep learning mentor with {experience_label.lower()} experience. "
|
55 |
+
f"Teach in a friendly manner and keep answers within 150 words. "
|
56 |
+
f"If a question is outside deep learning, politely decline to answer."
|
57 |
+
)
|
58 |
|
|
|
59 |
messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
|
|
|
|
|
60 |
result = deep_seek.invoke(messages)
|
|
|
|
|
61 |
st.session_state[PAGE_KEY].append((user_input, result.content))
|
62 |
|
63 |
+
# --- Chat History Display ---
|
64 |
st.subheader("🗨️ Chat History")
|
65 |
for user, bot in st.session_state[PAGE_KEY]:
|
66 |
st.markdown(f"**You:** {user}")
|