Akhil4839 commited on
Commit
2c108cc
Β·
verified Β·
1 Parent(s): db219ba

Create 2_Machine_learning.py

Browse files
Files changed (1) hide show
  1. pages/2_Machine_learning.py +123 -0
pages/2_Machine_learning.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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('Data_science')
8
+ os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
9
+ os.environ['HF_TOKEN'] = hf
10
+
11
+ # --- Page Configuration ---
12
+ st.set_page_config(page_title="πŸ€– ML Mentor Chat", page_icon="πŸ“˜", layout="centered")
13
+
14
+ # --- Inject Custom CSS Styling ---
15
+ st.markdown("""
16
+ <style>
17
+ .main {
18
+ background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
19
+ padding: 2rem;
20
+ font-family: 'Segoe UI', sans-serif;
21
+ }
22
+ h1, h2, h3, h4, h5, h6, p, label, .css-10trblm, .css-1v0mbdj, .css-q8sbsg {
23
+ color: #ffffff !important;
24
+ text-align: center;
25
+ }
26
+ .stTextInput > div > div > input {
27
+ background-color: rgba(255, 255, 255, 0.1);
28
+ color: white;
29
+ border: 1px solid rgba(255, 255, 255, 0.5);
30
+ border-radius: 8px;
31
+ padding: 0.5em;
32
+ }
33
+ .stTextInput > div > div > input::placeholder {
34
+ color: rgba(255, 255, 255, 0.6);
35
+ }
36
+ .stButton>button {
37
+ background: rgba(255, 255, 255, 0.15);
38
+ border: 2px solid rgba(255, 255, 255, 0.4);
39
+ color: white;
40
+ font-size: 18px;
41
+ font-weight: bold;
42
+ padding: 0.8em 1.2em;
43
+ border-radius: 10px;
44
+ width: 100%;
45
+ transition: 0.3s ease;
46
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
47
+ }
48
+ .stButton>button:hover {
49
+ background: rgba(255, 255, 255, 0.3);
50
+ border-color: white;
51
+ color: white;
52
+ }
53
+ .stSidebar > div:first-child {
54
+ background: #2c3e50;
55
+ padding: 1rem;
56
+ border-radius: 0 15px 15px 0;
57
+ }
58
+ .stSidebar h1, .stSidebar h2, .stSidebar h3, .stSidebar label, .stSidebar p {
59
+ color: white !important;
60
+ }
61
+ hr {
62
+ border: 1px solid rgba(255, 255, 255, 0.3);
63
+ margin: 2em 0;
64
+ }
65
+ </style>
66
+ """, unsafe_allow_html=True)
67
+
68
+ # --- Page Title ---
69
+ st.title("πŸ€– Machine Learning Mentor Chat")
70
+ st.markdown("### πŸ“˜ Ask anything about ML concepts, tools, or workflows!")
71
+
72
+ # --- Sidebar: Experience Selector ---
73
+ st.sidebar.title("πŸ§‘β€πŸ« Mentor Preferences")
74
+ experience_label = st.sidebar.selectbox(
75
+ "πŸŽ“ Choose your experience level:", ["Beginner", "Intermediate", "Experienced"]
76
+ )
77
+
78
+ # --- Initialize Chat Model ---
79
+ ml_model_skeleton = HuggingFaceEndpoint(
80
+ repo_id='Qwen/Qwen3-14B',
81
+ provider='nebius',
82
+ temperature=0.7,
83
+ max_new_tokens=50,
84
+ task='conversational'
85
+ )
86
+
87
+ ml_mentor = ChatHuggingFace(
88
+ llm=ml_model_skeleton,
89
+ repo_id='Qwen/Qwen3-14B',
90
+ provider='nebius',
91
+ temperature=0.7,
92
+ max_new_tokens=50,
93
+ task='conversational'
94
+ )
95
+
96
+ PAGE_KEY = "ml_chat_history"
97
+
98
+ # --- Session State Initialization ---
99
+ if PAGE_KEY not in st.session_state:
100
+ st.session_state[PAGE_KEY] = []
101
+
102
+ # --- Chat Input Form ---
103
+ with st.form(key="chat_form"):
104
+ user_input = st.text_input("🧠 Ask your ML question:", placeholder="e.g. What is overfitting?")
105
+ submit = st.form_submit_button("πŸš€ Send")
106
+
107
+ # --- Chat Logic ---
108
+ if submit and user_input:
109
+ system_prompt = (
110
+ f"You are a helpful machine learning mentor for a {experience_label.lower()} learner. "
111
+ f"Only answer machine learning-related questions in a concise, friendly tone. "
112
+ f"Keep answers under 150 words. Politely decline off-topic questions."
113
+ )
114
+ messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
115
+ result = ml_mentor.invoke(messages)
116
+ st.session_state[PAGE_KEY].append((user_input, result.content))
117
+
118
+ # --- Chat History ---
119
+ st.subheader("πŸ“œ Chat History")
120
+ for user, bot in st.session_state[PAGE_KEY]:
121
+ st.markdown(f"**πŸ§‘ You:** {user}")
122
+ st.markdown(f"**πŸ€– Mentor:** {bot}")
123
+ st.markdown("---")