Spaces:
Sleeping
Sleeping
Rename pages to pages/python.py
Browse files- pages +0 -0
- pages/python.py +103 -0
pages
DELETED
|
File without changes
|
pages/python.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
hf = os.getenv('Data_science')
|
| 7 |
+
os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
|
| 8 |
+
os.environ['HF_TOKEN'] = hf
|
| 9 |
+
|
| 10 |
+
# Page config
|
| 11 |
+
st.set_page_config(page_title="Python Mentor Chat", layout="centered")
|
| 12 |
+
|
| 13 |
+
# Inject home page CSS style
|
| 14 |
+
st.markdown("""
|
| 15 |
+
<style>
|
| 16 |
+
.main {
|
| 17 |
+
background: linear-gradient(135deg, #430089 0%, #82ffa1 100%);
|
| 18 |
+
padding: 2rem;
|
| 19 |
+
font-family: 'Segoe UI', sans-serif;
|
| 20 |
+
}
|
| 21 |
+
.stButton>button {
|
| 22 |
+
background: #ffffff10;
|
| 23 |
+
border: 2px solid #ffffff50;
|
| 24 |
+
color: white;
|
| 25 |
+
font-size: 18px;
|
| 26 |
+
font-weight: 600;
|
| 27 |
+
padding: 0.8em 1.2em;
|
| 28 |
+
border-radius: 12px;
|
| 29 |
+
width: 100%;
|
| 30 |
+
transition: 0.3s ease;
|
| 31 |
+
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
|
| 32 |
+
}
|
| 33 |
+
.stButton>button:hover {
|
| 34 |
+
background: #ffffff30;
|
| 35 |
+
border-color: #fff;
|
| 36 |
+
color: #ffffff;
|
| 37 |
+
}
|
| 38 |
+
h1, h3, p, label {
|
| 39 |
+
color: #ffffff;
|
| 40 |
+
text-align: center;
|
| 41 |
+
}
|
| 42 |
+
hr {
|
| 43 |
+
border: 1px solid #ffffff50;
|
| 44 |
+
margin: 2em 0;
|
| 45 |
+
}
|
| 46 |
+
.css-1aumxhk {
|
| 47 |
+
color: white;
|
| 48 |
+
}
|
| 49 |
+
</style>
|
| 50 |
+
""", unsafe_allow_html=True)
|
| 51 |
+
|
| 52 |
+
# Title
|
| 53 |
+
st.title("🐍 Python Mentor Chat")
|
| 54 |
+
|
| 55 |
+
# Sidebar
|
| 56 |
+
st.sidebar.title("Mentor Preferences")
|
| 57 |
+
experience_label = st.sidebar.selectbox(
|
| 58 |
+
"Select your experience level:", ["Beginner", "Intermediate", "Experienced"]
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# Initialize model
|
| 62 |
+
deep_seek_skeleton = HuggingFaceEndpoint(
|
| 63 |
+
repo_id='mistralai/Mistral-7B-Instruct-v0.3',
|
| 64 |
+
provider='novita',
|
| 65 |
+
temperature=0.7,
|
| 66 |
+
max_new_tokens=50,
|
| 67 |
+
task='conversational'
|
| 68 |
+
)
|
| 69 |
+
deep_seek = ChatHuggingFace(
|
| 70 |
+
llm=deep_seek_skeleton,
|
| 71 |
+
repo_id='mistralai/Mistral-7B-Instruct-v0.3',
|
| 72 |
+
provider='novita',
|
| 73 |
+
temperature=0.7,
|
| 74 |
+
max_new_tokens=50,
|
| 75 |
+
task='conversational'
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
PAGE_KEY = "python_chat_history"
|
| 79 |
+
if PAGE_KEY not in st.session_state:
|
| 80 |
+
st.session_state[PAGE_KEY] = []
|
| 81 |
+
|
| 82 |
+
# Chat input form
|
| 83 |
+
with st.form(key="chat_form"):
|
| 84 |
+
user_input = st.text_input("Ask your question:")
|
| 85 |
+
submit = st.form_submit_button("Send")
|
| 86 |
+
|
| 87 |
+
# Chat logic
|
| 88 |
+
if submit and user_input:
|
| 89 |
+
system_prompt = (
|
| 90 |
+
f"Act as a python mentor with {experience_label.lower()} experience. "
|
| 91 |
+
f"Teach in a friendly manner and keep answers within 150 words. "
|
| 92 |
+
f"If a question is not about python, politely mention it's out of scope."
|
| 93 |
+
)
|
| 94 |
+
messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
|
| 95 |
+
result = deep_seek.invoke(messages)
|
| 96 |
+
st.session_state[PAGE_KEY].append((user_input, result.content))
|
| 97 |
+
|
| 98 |
+
# Chat history
|
| 99 |
+
st.subheader("🗨️ Chat History")
|
| 100 |
+
for user, bot in st.session_state[PAGE_KEY]:
|
| 101 |
+
st.markdown(f"**You:** {user}")
|
| 102 |
+
st.markdown(f"**Mentor:** {bot}")
|
| 103 |
+
st.markdown("---")
|