Spaces:
Running
Running
import streamlit as st | |
import os | |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace | |
from langchain_core.messages import HumanMessage, SystemMessage | |
hf = os.getenv('HF_TOKEN') | |
os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf | |
os.environ['HF_TOKEN'] = hf | |
# Page config | |
st.set_page_config(page_title="Python Mentor Chat", layout="centered") | |
# Inject home page CSS style | |
# st.markdown(""" | |
# <style> | |
# .main { | |
# background: linear-gradient(135deg, #430089 0%, #82ffa1 100%); | |
# padding: 2rem; | |
# font-family: 'Segoe UI', sans-serif; | |
# } | |
# .stButton>button { | |
# background: #ffffff10; | |
# border: 2px solid #ffffff50; | |
# color: white; | |
# font-size: 18px; | |
# font-weight: 600; | |
# padding: 0.8em 1.2em; | |
# border-radius: 12px; | |
# width: 100%; | |
# transition: 0.3s ease; | |
# box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15); | |
# } | |
# .stButton>button:hover { | |
# background: #ffffff30; | |
# border-color: #fff; | |
# color: #ffffff; | |
# } | |
# h1, h3, p, label { | |
# color: #ffffff; | |
# text-align: center; | |
# } | |
# hr { | |
# border: 1px solid #ffffff50; | |
# margin: 2em 0; | |
# } | |
# .css-1aumxhk { | |
# color: white; | |
# } | |
# </style> | |
# """, unsafe_allow_html=True) | |
st.markdown(""" | |
<style> | |
.main { | |
background: linear-gradient(to right, #1f4037, #99f2c8); | |
padding: 2rem; | |
font-family: 'Segoe UI', sans-serif; | |
} | |
.stButton>button { | |
background-color: #ffffff10; | |
color: white; | |
font-weight: 600; | |
border-radius: 10px; | |
padding: 0.6rem 1rem; | |
transition: all 0.3s ease; | |
border: 1px solid white; | |
} | |
.stButton>button:hover { | |
background-color: #ffffff30; | |
color: white; | |
border-color: #fff; | |
} | |
h1, h3, p, label { | |
color: white; | |
text-align: center; | |
} | |
.chat-bubble-user { | |
background-color: #ffffff25; | |
padding: 0.75rem 1rem; | |
border-radius: 1rem; | |
margin-bottom: 0.5rem; | |
color: #fff; | |
font-weight: 500; | |
text-align: left; | |
width: fit-content; | |
max-width: 90%; | |
align-self: flex-end; | |
} | |
.chat-bubble-bot { | |
background-color: #ffffff15; | |
padding: 0.75rem 1rem; | |
border-radius: 1rem; | |
margin-bottom: 0.5rem; | |
color: #fff; | |
text-align: left; | |
width: fit-content; | |
max-width: 90%; | |
align-self: flex-start; | |
} | |
.chat-container { | |
display: flex; | |
flex-direction: column; | |
gap: 0.5rem; | |
margin-top: 2rem; | |
} | |
</style> | |
""", unsafe_allow_html=True) | |
# Page title | |
# Title | |
st.title("π Python Mentor Chat") | |
# Sidebar | |
st.sidebar.title("Mentor Preferences") | |
experience_label = st.sidebar.selectbox( | |
"Select your experience level:", ["Beginner", "Intermediate", "Experienced"] | |
) | |
# Initialize model | |
deep_seek_skeleton = HuggingFaceEndpoint( | |
repo_id='mistralai/Mistral-7B-Instruct-v0.3', | |
provider='novita', | |
temperature=0.7, | |
max_new_tokens=50, | |
task='conversational' | |
) | |
deep_seek = ChatHuggingFace( | |
llm=deep_seek_skeleton, | |
repo_id='mistralai/Mistral-7B-Instruct-v0.3', | |
provider='novita', | |
temperature=0.7, | |
max_new_tokens=150, | |
task='conversational' | |
) | |
PAGE_KEY = "python_chat_history" | |
if PAGE_KEY not in st.session_state: | |
st.session_state[PAGE_KEY] = [] | |
# Chat input form | |
with st.form(key="chat_form"): | |
user_input = st.text_input("Ask your question:") | |
submit = st.form_submit_button("Send") | |
# Chat logic | |
if submit and user_input: | |
# system_prompt = ( | |
# f"Act as a python mentor with {experience_label.lower()} experience. " | |
# f"Teach in a friendly manner and keep answers within 150 words. " | |
# f"If a question is not about python, politely mention it's out of scope." | |
# ) | |
system_prompt = f"""Act as a Python mentor with {experience_label} years of experience. Teach in a friendly, approachable manner while following these strict rules: | |
1. Only answer questions related to Python programming (including libraries, frameworks, and tools in the Python ecosystem) | |
2. For any non-Python query, respond with exactly: "I specialize only in Python programming. This appears to be a non-Python topic." | |
3. Never suggest you can help with non-Python topics | |
4. Keep explanations clear, practical, and beginner-friendly when appropriate | |
5. Include practical examples when explaining concepts | |
6. For advanced topics, assume the student has basic Python knowledge""" | |
messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)] | |
result = deep_seek.invoke(messages) | |
st.session_state[PAGE_KEY].append((user_input, result.content)) | |
# Chat history | |
st.subheader("π¨οΈ Chat History") | |
# for user, bot in st.session_state[PAGE_KEY]: | |
# st.markdown(f"**You:** {user}") | |
# st.markdown(f"**Mentor:** {bot}") | |
# st.markdown("---") | |
if st.session_state[PAGE_KEY]: | |
st.markdown('<div class="chat-container">', unsafe_allow_html=True) | |
for user, bot in st.session_state[PAGE_KEY]: | |
st.markdown(f'<div class="chat-bubble-user">π€ <strong>You:</strong> {user}</div>', unsafe_allow_html=True) | |
st.markdown(f'<div class="chat-bubble-bot">π§βπ« <strong>Mentor:</strong> {bot}</div>', unsafe_allow_html=True) | |
st.markdown('</div>', unsafe_allow_html=True) | |