Spaces:
Running
Running
Create Statistics.py
Browse files- pages/Statistics.py +100 -0
pages/Statistics.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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('HF_TOKEN')
|
8 |
+
os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
|
9 |
+
os.environ['HF_TOKEN'] = hf
|
10 |
+
# Page setup
|
11 |
+
st.set_page_config(page_title="Statistics Mentor Chat", layout="centered")
|
12 |
+
|
13 |
+
# Modern CSS
|
14 |
+
st.markdown("""
|
15 |
+
<style>
|
16 |
+
.main {
|
17 |
+
background: linear-gradient(to right, #3e32a8, #80ffe0);
|
18 |
+
padding: 2rem;
|
19 |
+
font-family: 'Segoe UI', sans-serif;
|
20 |
+
}
|
21 |
+
.stButton>button {
|
22 |
+
background-color: #ffffff10;
|
23 |
+
color: white;
|
24 |
+
font-weight: 600;
|
25 |
+
border-radius: 10px;
|
26 |
+
padding: 0.6rem 1rem;
|
27 |
+
transition: all 0.3s ease;
|
28 |
+
border: 1px solid white;
|
29 |
+
}
|
30 |
+
.stButton>button:hover {
|
31 |
+
background-color: #ffffff30;
|
32 |
+
color: white;
|
33 |
+
border-color: #fff;
|
34 |
+
}
|
35 |
+
h1, h3, p, label {
|
36 |
+
color: white;
|
37 |
+
text-align: center;
|
38 |
+
}
|
39 |
+
.chat-bubble-user {
|
40 |
+
background-color: #ffffff25;
|
41 |
+
padding: 0.75rem 1rem;
|
42 |
+
border-radius: 1rem;
|
43 |
+
margin-bottom: 0.5rem;
|
44 |
+
color: #fff;
|
45 |
+
font-weight: 500;
|
46 |
+
width: fit-content;
|
47 |
+
max-width: 90%;
|
48 |
+
align-self: flex-end;
|
49 |
+
}
|
50 |
+
.chat-bubble-bot {
|
51 |
+
background-color: #ffffff15;
|
52 |
+
padding: 0.75rem 1rem;
|
53 |
+
border-radius: 1rem;
|
54 |
+
margin-bottom: 0.5rem;
|
55 |
+
color: #fff;
|
56 |
+
width: fit-content;
|
57 |
+
max-width: 90%;
|
58 |
+
align-self: flex-start;
|
59 |
+
}
|
60 |
+
.chat-container {
|
61 |
+
display: flex;
|
62 |
+
flex-direction: column;
|
63 |
+
gap: 0.5rem;
|
64 |
+
margin-top: 2rem;
|
65 |
+
}
|
66 |
+
</style>
|
67 |
+
""", unsafe_allow_html=True)
|
68 |
+
|
69 |
+
# Title
|
70 |
+
st.markdown("<h1 style='margin-bottom: 0;'>π Statistics Mentor Chat</h1>", unsafe_allow_html=True)
|
71 |
+
st.markdown("<p style='margin-top: 0.2rem;'>Ask anything about statistics!</p>", unsafe_allow_html=True)
|
72 |
+
|
73 |
+
# Sidebar
|
74 |
+
st.sidebar.title("Mentor Preferences")
|
75 |
+
exp = st.sidebar.selectbox("Select your experience level:", ["Beginner", "Intermediate", "Expert"])
|
76 |
+
|
77 |
+
# Chat form
|
78 |
+
st.markdown("<hr>", unsafe_allow_html=True)
|
79 |
+
with st.form(key="chat_form"):
|
80 |
+
user_input = st.text_input("π¬ Ask a statistics question:")
|
81 |
+
submit = st.form_submit_button("Send")
|
82 |
+
|
83 |
+
# Chat logic
|
84 |
+
if submit and user_input:
|
85 |
+
system_prompt = (
|
86 |
+
f"Act as a statistics mentor with {exp.lower()} expertise. "
|
87 |
+
f"Answer in a friendly tone and within 150 words. "
|
88 |
+
f"If the question is not statistics-related, politely say it's out of scope."
|
89 |
+
)
|
90 |
+
messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
|
91 |
+
result = stats_mentor.invoke(messages)
|
92 |
+
st.session_state[PAGE_KEY].append((user_input, result.content))
|
93 |
+
|
94 |
+
# Display chat history
|
95 |
+
if st.session_state[PAGE_KEY]:
|
96 |
+
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
97 |
+
for user, bot in st.session_state[PAGE_KEY]:
|
98 |
+
st.markdown(f'<div class="chat-bubble-user">π€ <strong>You:</strong> {user}</div>', unsafe_allow_html=True)
|
99 |
+
st.markdown(f'<div class="chat-bubble-bot">π <strong>Mentor:</strong> {bot}</div>', unsafe_allow_html=True)
|
100 |
+
st.markdown('</div>', unsafe_allow_html=True)
|