Spaces:
Running
Running
Update pages/statistics.py
Browse files- pages/statistics.py +56 -17
pages/statistics.py
CHANGED
@@ -1,22 +1,60 @@
|
|
1 |
import streamlit as st
|
2 |
import os
|
3 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
4 |
-
from langchain_core.messages import HumanMessage,
|
5 |
|
|
|
6 |
hf = os.getenv('Data_science')
|
7 |
os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
|
8 |
os.environ['HF_TOKEN'] = hf
|
9 |
|
10 |
-
#
|
11 |
st.set_page_config(page_title="Statistics Mentor Chat", layout="centered")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
st.title("π Statistics Mentor Chat")
|
13 |
|
14 |
-
#
|
15 |
st.sidebar.title("Mentor Preferences")
|
16 |
-
|
17 |
-
exp = st.sidebar.selectbox("Select your experience level:", exp_options)
|
18 |
|
19 |
-
#
|
20 |
stats_model_skeleton = HuggingFaceEndpoint(
|
21 |
repo_id='THUDM/GLM-4-32B-0414',
|
22 |
provider='novita',
|
@@ -34,29 +72,30 @@ stats_mentor = ChatHuggingFace(
|
|
34 |
task='conversational'
|
35 |
)
|
36 |
|
37 |
-
#
|
38 |
-
|
39 |
-
|
|
|
40 |
|
41 |
-
#
|
42 |
with st.form(key="chat_form"):
|
43 |
user_input = st.text_input("Ask your question:")
|
44 |
submit = st.form_submit_button("Send")
|
45 |
|
46 |
-
#
|
47 |
if submit and user_input:
|
48 |
system_prompt = (
|
49 |
-
f"Act as a statistics mentor with {exp.lower()}
|
50 |
-
f"
|
51 |
-
f"If the question is not
|
52 |
)
|
53 |
messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
|
54 |
result = stats_mentor.invoke(messages)
|
55 |
-
st.session_state.
|
56 |
|
57 |
-
#
|
58 |
st.subheader("π¨οΈ Chat History")
|
59 |
-
for user, bot in st.session_state
|
60 |
st.markdown(f"**You:** {user}")
|
61 |
st.markdown(f"**Mentor:** {bot}")
|
62 |
st.markdown("---")
|
|
|
1 |
import streamlit as st
|
2 |
import os
|
3 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
4 |
+
from langchain_core.messages import HumanMessage, SystemMessage
|
5 |
|
6 |
+
# Set environment variables
|
7 |
hf = os.getenv('Data_science')
|
8 |
os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
|
9 |
os.environ['HF_TOKEN'] = hf
|
10 |
|
11 |
+
# Page setup
|
12 |
st.set_page_config(page_title="Statistics Mentor Chat", layout="centered")
|
13 |
+
|
14 |
+
# Custom style
|
15 |
+
st.markdown("""
|
16 |
+
<style>
|
17 |
+
.main {
|
18 |
+
background: linear-gradient(135deg, #3e32a8 0%, #80ffe0 100%);
|
19 |
+
padding: 2rem;
|
20 |
+
font-family: 'Segoe UI', sans-serif;
|
21 |
+
}
|
22 |
+
.stButton>button {
|
23 |
+
background: #ffffff10;
|
24 |
+
border: 2px solid #ffffff50;
|
25 |
+
color: white;
|
26 |
+
font-size: 18px;
|
27 |
+
font-weight: 600;
|
28 |
+
padding: 0.8em 1.2em;
|
29 |
+
border-radius: 12px;
|
30 |
+
width: 100%;
|
31 |
+
transition: 0.3s ease;
|
32 |
+
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
|
33 |
+
}
|
34 |
+
.stButton>button:hover {
|
35 |
+
background: #ffffff30;
|
36 |
+
border-color: #fff;
|
37 |
+
color: #ffffff;
|
38 |
+
}
|
39 |
+
h1, h3, p, label {
|
40 |
+
color: #ffffff;
|
41 |
+
text-align: center;
|
42 |
+
}
|
43 |
+
hr {
|
44 |
+
border: 1px solid #ffffff50;
|
45 |
+
margin: 2em 0;
|
46 |
+
}
|
47 |
+
</style>
|
48 |
+
""", unsafe_allow_html=True)
|
49 |
+
|
50 |
+
# Title
|
51 |
st.title("π Statistics Mentor Chat")
|
52 |
|
53 |
+
# Sidebar
|
54 |
st.sidebar.title("Mentor Preferences")
|
55 |
+
exp = st.sidebar.selectbox("Select your experience level:", ["Beginner", "Intermediate", "Expert"])
|
|
|
56 |
|
57 |
+
# Model setup
|
58 |
stats_model_skeleton = HuggingFaceEndpoint(
|
59 |
repo_id='THUDM/GLM-4-32B-0414',
|
60 |
provider='novita',
|
|
|
72 |
task='conversational'
|
73 |
)
|
74 |
|
75 |
+
# Session key
|
76 |
+
PAGE_KEY = "chat_history_stats"
|
77 |
+
if PAGE_KEY not in st.session_state:
|
78 |
+
st.session_state[PAGE_KEY] = []
|
79 |
|
80 |
+
# Chat form
|
81 |
with st.form(key="chat_form"):
|
82 |
user_input = st.text_input("Ask your question:")
|
83 |
submit = st.form_submit_button("Send")
|
84 |
|
85 |
+
# Chat logic
|
86 |
if submit and user_input:
|
87 |
system_prompt = (
|
88 |
+
f"Act as a statistics mentor with {exp.lower()} expertise. "
|
89 |
+
f"Answer in a friendly tone and within 150 words. "
|
90 |
+
f"If the question is not statistics-related, politely say it's out of scope."
|
91 |
)
|
92 |
messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
|
93 |
result = stats_mentor.invoke(messages)
|
94 |
+
st.session_state[PAGE_KEY].append((user_input, result.content))
|
95 |
|
96 |
+
# Display chat history
|
97 |
st.subheader("π¨οΈ Chat History")
|
98 |
+
for user, bot in st.session_state[PAGE_KEY]:
|
99 |
st.markdown(f"**You:** {user}")
|
100 |
st.markdown(f"**Mentor:** {bot}")
|
101 |
st.markdown("---")
|