Spaces:
Sleeping
Sleeping
Create pages/1_Python
Browse files- pages/1_Python +122 -0
pages/1_Python
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Load HuggingFace token from environment variable
|
7 |
+
hf = os.getenv('Data_science')
|
8 |
+
os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
|
9 |
+
os.environ['HF_TOKEN'] = hf
|
10 |
+
|
11 |
+
# Page config
|
12 |
+
st.set_page_config(page_title="π Python Mentor Chat", page_icon="π¬", layout="centered")
|
13 |
+
|
14 |
+
# Inject custom CSS
|
15 |
+
st.markdown("""
|
16 |
+
<style>
|
17 |
+
.main {
|
18 |
+
background: linear-gradient(135deg, #1f1c2c 0%, #928dab 100%);
|
19 |
+
padding: 2rem;
|
20 |
+
font-family: 'Segoe UI', sans-serif;
|
21 |
+
}
|
22 |
+
h1, h2, h3, h4, p, label, .css-10trblm, .css-q8sbsg {
|
23 |
+
color: #ffffff !important;
|
24 |
+
text-align: center;
|
25 |
+
}
|
26 |
+
.stTextInput>div>div>input {
|
27 |
+
background-color: #ffffff10;
|
28 |
+
color: #ffffff;
|
29 |
+
border: 1px solid #ffffff50;
|
30 |
+
border-radius: 8px;
|
31 |
+
padding: 0.6em;
|
32 |
+
}
|
33 |
+
.stTextInput>div>div>input::placeholder {
|
34 |
+
color: #ffffff90;
|
35 |
+
}
|
36 |
+
.stButton>button {
|
37 |
+
background: rgba(255, 255, 255, 0.1);
|
38 |
+
border: 2px solid rgba(255, 255, 255, 0.3);
|
39 |
+
color: white;
|
40 |
+
font-size: 18px;
|
41 |
+
font-weight: bold;
|
42 |
+
padding: 0.8em 1.2em;
|
43 |
+
border-radius: 12px;
|
44 |
+
width: 100%;
|
45 |
+
transition: all 0.3s ease;
|
46 |
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
|
47 |
+
}
|
48 |
+
.stButton>button:hover {
|
49 |
+
background: rgba(255, 255, 255, 0.3);
|
50 |
+
color: white;
|
51 |
+
border-color: white;
|
52 |
+
}
|
53 |
+
.stSidebar > div:first-child {
|
54 |
+
background: #2c3e50;
|
55 |
+
color: white;
|
56 |
+
border-radius: 0 20px 20px 0;
|
57 |
+
}
|
58 |
+
.stSidebar h1, .stSidebar h2, .stSidebar h3, .stSidebar h4, .stSidebar label, .stSidebar p {
|
59 |
+
color: white !important;
|
60 |
+
}
|
61 |
+
hr {
|
62 |
+
border: 1px solid #ffffff50;
|
63 |
+
margin: 1.5em 0;
|
64 |
+
}
|
65 |
+
</style>
|
66 |
+
""", unsafe_allow_html=True)
|
67 |
+
|
68 |
+
# Title
|
69 |
+
st.title("π Python Mentor Chat")
|
70 |
+
st.markdown("### π¬ Your personal Python guide for Data Science and beyond!")
|
71 |
+
|
72 |
+
# Sidebar configuration
|
73 |
+
st.sidebar.title("π§βπ« Mentor Preferences")
|
74 |
+
experience_label = st.sidebar.selectbox(
|
75 |
+
"π Select your experience level:", ["Beginner", "Intermediate", "Experienced"]
|
76 |
+
)
|
77 |
+
|
78 |
+
# Load the model
|
79 |
+
deep_seek_skeleton = HuggingFaceEndpoint(
|
80 |
+
repo_id='mistralai/Mistral-7B-Instruct-v0.3',
|
81 |
+
provider='novita',
|
82 |
+
temperature=0.7,
|
83 |
+
max_new_tokens=50,
|
84 |
+
task='conversational'
|
85 |
+
)
|
86 |
+
|
87 |
+
deep_seek = ChatHuggingFace(
|
88 |
+
llm=deep_seek_skeleton,
|
89 |
+
repo_id='mistralai/Mistral-7B-Instruct-v0.3',
|
90 |
+
provider='novita',
|
91 |
+
temperature=0.7,
|
92 |
+
max_new_tokens=50,
|
93 |
+
task='conversational'
|
94 |
+
)
|
95 |
+
|
96 |
+
# Chat session history key
|
97 |
+
PAGE_KEY = "python_chat_history"
|
98 |
+
if PAGE_KEY not in st.session_state:
|
99 |
+
st.session_state[PAGE_KEY] = []
|
100 |
+
|
101 |
+
# Chat input form
|
102 |
+
with st.form(key="chat_form"):
|
103 |
+
user_input = st.text_input("π£οΈ Ask your question:", placeholder="e.g. What is a Python decorator?")
|
104 |
+
submit = st.form_submit_button("π Send")
|
105 |
+
|
106 |
+
# Chat logic
|
107 |
+
if submit and user_input:
|
108 |
+
system_prompt = (
|
109 |
+
f"Act as a Python mentor for a {experience_label.lower()} learner. "
|
110 |
+
f"Answer in a helpful, friendly, and concise manner. Max 150 words. "
|
111 |
+
f"If the question is not Python-related, say it's out of scope politely."
|
112 |
+
)
|
113 |
+
messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
|
114 |
+
result = deep_seek.invoke(messages)
|
115 |
+
st.session_state[PAGE_KEY].append((user_input, result.content))
|
116 |
+
|
117 |
+
# Display chat history
|
118 |
+
st.subheader("π Chat History")
|
119 |
+
for user, bot in st.session_state[PAGE_KEY]:
|
120 |
+
st.markdown(f"**π§ You:** {user}")
|
121 |
+
st.markdown(f"**π€ Mentor:** {bot}")
|
122 |
+
st.markdown("---")
|