Spaces:
Sleeping
Sleeping
Create machine_learning.py
Browse files- pages/machine_learning.py +117 -0
pages/machine_learning.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 Hugging Face tokens
|
| 7 |
+
hf = os.getenv('DataScience_Chatbot')
|
| 8 |
+
os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
|
| 9 |
+
os.environ['HF_TOKEN'] = hf
|
| 10 |
+
|
| 11 |
+
# --- Page Configuration ---
|
| 12 |
+
st.set_page_config(page_title="Machine Learning Mentor", layout="wide")
|
| 13 |
+
|
| 14 |
+
# --- Custom CSS ---
|
| 15 |
+
st.markdown("""
|
| 16 |
+
<style>
|
| 17 |
+
body {
|
| 18 |
+
font-family: 'Segoe UI', sans-serif;
|
| 19 |
+
background: linear-gradient(135deg, #2c003e, #0f9b8e);
|
| 20 |
+
}
|
| 21 |
+
.main {
|
| 22 |
+
padding: 2rem;
|
| 23 |
+
}
|
| 24 |
+
.stTextInput>div>div>input {
|
| 25 |
+
color: #ffffff !important;
|
| 26 |
+
background-color: #00000010;
|
| 27 |
+
border: 1px solid #ffffff30;
|
| 28 |
+
}
|
| 29 |
+
.chat-bubble {
|
| 30 |
+
padding: 1rem;
|
| 31 |
+
margin: 0.5rem 0;
|
| 32 |
+
border-radius: 12px;
|
| 33 |
+
max-width: 80%;
|
| 34 |
+
}
|
| 35 |
+
.user-bubble {
|
| 36 |
+
background-color: #4b0082;
|
| 37 |
+
color: white;
|
| 38 |
+
margin-left: auto;
|
| 39 |
+
text-align: right;
|
| 40 |
+
}
|
| 41 |
+
.mentor-bubble {
|
| 42 |
+
background-color: #00bfa6;
|
| 43 |
+
color: white;
|
| 44 |
+
margin-right: auto;
|
| 45 |
+
}
|
| 46 |
+
.stButton>button {
|
| 47 |
+
background: #ffffff20;
|
| 48 |
+
border: 1px solid #ffffff50;
|
| 49 |
+
color: white;
|
| 50 |
+
font-weight: bold;
|
| 51 |
+
border-radius: 10px;
|
| 52 |
+
transition: 0.3s ease;
|
| 53 |
+
}
|
| 54 |
+
.stButton>button:hover {
|
| 55 |
+
background: #ffffff40;
|
| 56 |
+
}
|
| 57 |
+
</style>
|
| 58 |
+
""", unsafe_allow_html=True)
|
| 59 |
+
|
| 60 |
+
# --- Title ---
|
| 61 |
+
st.title("🤖 Machine Learning Mentor")
|
| 62 |
+
|
| 63 |
+
# --- Sidebar Preferences ---
|
| 64 |
+
st.sidebar.title("Mentor Preferences")
|
| 65 |
+
experience_label = st.sidebar.selectbox("Select your experience level:", ["Beginner", "Intermediate", "Experienced"])
|
| 66 |
+
|
| 67 |
+
# --- Initialize Model ---
|
| 68 |
+
ml_model_skeleton = HuggingFaceEndpoint(
|
| 69 |
+
repo_id='Qwen/Qwen3-14B',
|
| 70 |
+
provider='nebius',
|
| 71 |
+
temperature=0.7,
|
| 72 |
+
max_new_tokens=50,
|
| 73 |
+
task='conversational'
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
ml_mentor = ChatHuggingFace(
|
| 77 |
+
llm=ml_model_skeleton,
|
| 78 |
+
repo_id='Qwen/Qwen3-14B',
|
| 79 |
+
provider='nebius',
|
| 80 |
+
temperature=0.7,
|
| 81 |
+
max_new_tokens=50,
|
| 82 |
+
task='conversational'
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
PAGE_KEY = "ml_chat_history"
|
| 86 |
+
if PAGE_KEY not in st.session_state:
|
| 87 |
+
st.session_state[PAGE_KEY] = []
|
| 88 |
+
|
| 89 |
+
# --- Layout ---
|
| 90 |
+
col1, col2 = st.columns([3, 1])
|
| 91 |
+
|
| 92 |
+
# --- Chat Section ---
|
| 93 |
+
with col1:
|
| 94 |
+
with st.form(key="chat_form"):
|
| 95 |
+
user_input = st.text_input("Ask your question:")
|
| 96 |
+
submit = st.form_submit_button("Send")
|
| 97 |
+
|
| 98 |
+
if submit and user_input:
|
| 99 |
+
system_prompt = (
|
| 100 |
+
f"You are a machine learning mentor with {experience_label.lower()} experience. "
|
| 101 |
+
f"Answer only machine learning questions in a friendly tone and within 150 words. "
|
| 102 |
+
f"Politely inform if the question is out of scope."
|
| 103 |
+
)
|
| 104 |
+
messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
|
| 105 |
+
result = ml_mentor.invoke(messages)
|
| 106 |
+
st.session_state[PAGE_KEY].append((user_input, result.content))
|
| 107 |
+
|
| 108 |
+
st.subheader("🗨️ Chat History")
|
| 109 |
+
for user, bot in st.session_state[PAGE_KEY]:
|
| 110 |
+
st.markdown(f'<div class="chat-bubble user-bubble">{user}</div>', unsafe_allow_html=True)
|
| 111 |
+
st.markdown(f'<div class="chat-bubble mentor-bubble">{bot}</div>', unsafe_allow_html=True)
|
| 112 |
+
|
| 113 |
+
# --- Mentor Tips Sidebar ---
|
| 114 |
+
with col2:
|
| 115 |
+
st.markdown("### 💡 Tips from Mentor")
|
| 116 |
+
st.info("Try asking about:\n- Regression vs Classification\n- Overfitting examples\n- Feature scaling\n- Model evaluation techniques")
|
| 117 |
+
|