Spaces:
Sleeping
Sleeping
Update pages/Statistics.py
Browse files- pages/Statistics.py +55 -56
pages/Statistics.py
CHANGED
|
@@ -4,102 +4,101 @@ from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
|
| 4 |
from langchain_core.messages import HumanMessage, SystemMessage
|
| 5 |
|
| 6 |
# Set environment variables
|
| 7 |
-
hf = os.getenv('
|
| 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 |
-
#
|
| 14 |
st.markdown("""
|
| 15 |
<style>
|
| 16 |
.main {
|
| 17 |
-
background: linear-gradient(
|
| 18 |
padding: 2rem;
|
| 19 |
font-family: 'Segoe UI', sans-serif;
|
| 20 |
}
|
| 21 |
.stButton>button {
|
| 22 |
-
background
|
|
|
|
| 23 |
color: white;
|
|
|
|
| 24 |
font-weight: 600;
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
}
|
| 30 |
.stButton>button:hover {
|
| 31 |
-
background
|
| 32 |
-
color: white;
|
| 33 |
border-color: #fff;
|
|
|
|
| 34 |
}
|
| 35 |
h1, h3, p, label {
|
| 36 |
-
color:
|
| 37 |
text-align: center;
|
| 38 |
}
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 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.
|
| 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("
|
| 81 |
submit = st.form_submit_button("Send")
|
| 82 |
|
| 83 |
# Chat logic
|
| 84 |
if submit and user_input:
|
| 85 |
-
system_prompt = (
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
# Define chat history key
|
| 94 |
-
PAGE_KEY = "statistics_chat_history"
|
| 95 |
-
if PAGE_KEY not in st.session_state:
|
| 96 |
-
st.session_state[PAGE_KEY] = []
|
| 97 |
-
|
| 98 |
|
| 99 |
# Display chat history
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
|
|
|
| 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',
|
| 61 |
+
temperature=0.7,
|
| 62 |
+
max_new_tokens=110,
|
| 63 |
+
task='conversational'
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
stats_mentor = ChatHuggingFace(
|
| 67 |
+
llm=stats_model_skeleton,
|
| 68 |
+
repo_id='THUDM/GLM-4-32B-0414',
|
| 69 |
+
provider='novita',
|
| 70 |
+
temperature=0.7,
|
| 71 |
+
max_new_tokens=110,
|
| 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 = (f"""Act as a deep learning mentor with {exp} years of experience. Teach in a friendly, approachable manner while following these strict rules:
|
| 88 |
+
1. Only answer questions related to deep learning programming (including libraries, frameworks, and tools in the deep learning ecosystem)
|
| 89 |
+
2. For any non-deep learning query, respond with exactly: "I specialize only in deep learning programming. This appears to be a non-deep learning topic."
|
| 90 |
+
3. Never suggest you can help with non-deep learning topics
|
| 91 |
+
4. Keep explanations clear, practical, and beginner-friendly when appropriate
|
| 92 |
+
5. Include practical examples when explaining concepts
|
| 93 |
+
6. For advanced topics, assume the student has basic deep learning knowledge"""
|
| 94 |
)
|
| 95 |
messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
|
| 96 |
result = stats_mentor.invoke(messages)
|
| 97 |
st.session_state[PAGE_KEY].append((user_input, result.content))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
# Display chat history
|
| 100 |
+
st.subheader("🗨️ Chat History")
|
| 101 |
+
for user, bot in st.session_state[PAGE_KEY]:
|
| 102 |
+
st.markdown(f"**You:** {user}")
|
| 103 |
+
st.markdown(f"**Mentor:** {bot}")
|
| 104 |
+
st.markdown("---")
|
|
|