Spaces:
Sleeping
Sleeping
Create Deep_Learning.py
Browse files- pages/Deep_Learning.py +118 -0
pages/Deep_Learning.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 environment variables for Hugging Face token
|
7 |
+
hf = os.getenv('HF_TOKEN')
|
8 |
+
os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
|
9 |
+
os.environ['HF_TOKEN'] = hf
|
10 |
+
|
11 |
+
# Page config
|
12 |
+
st.set_page_config(page_title="Deep Learning Mentor Chat", layout="centered")
|
13 |
+
|
14 |
+
# Custom CSS for modern chat UI
|
15 |
+
st.markdown("""
|
16 |
+
<style>
|
17 |
+
.main {
|
18 |
+
background: linear-gradient(135deg, #2c3e50, #4ca1af);
|
19 |
+
color: white;
|
20 |
+
font-family: 'Segoe UI', sans-serif;
|
21 |
+
padding: 2rem;
|
22 |
+
}
|
23 |
+
.stButton>button {
|
24 |
+
background-color: #1abc9c;
|
25 |
+
color: white;
|
26 |
+
font-weight: bold;
|
27 |
+
padding: 0.6rem 1.2rem;
|
28 |
+
border-radius: 10px;
|
29 |
+
border: none;
|
30 |
+
width: 100%;
|
31 |
+
transition: 0.3s ease;
|
32 |
+
}
|
33 |
+
.stButton>button:hover {
|
34 |
+
background-color: #16a085;
|
35 |
+
color: #fff;
|
36 |
+
}
|
37 |
+
h1, h3, p, label, .css-10trblm, .css-1v0mbdj {
|
38 |
+
color: white !important;
|
39 |
+
text-align: center;
|
40 |
+
}
|
41 |
+
.chat-container {
|
42 |
+
margin-top: 1.5rem;
|
43 |
+
display: flex;
|
44 |
+
flex-direction: column;
|
45 |
+
gap: 1rem;
|
46 |
+
}
|
47 |
+
.chat-user {
|
48 |
+
background-color: #34495e;
|
49 |
+
padding: 0.8rem;
|
50 |
+
border-radius: 12px;
|
51 |
+
align-self: flex-end;
|
52 |
+
max-width: 80%;
|
53 |
+
font-weight: 500;
|
54 |
+
color: white;
|
55 |
+
}
|
56 |
+
.chat-bot {
|
57 |
+
background-color: #2ecc71;
|
58 |
+
padding: 0.8rem;
|
59 |
+
border-radius: 12px;
|
60 |
+
align-self: flex-start;
|
61 |
+
max-width: 80%;
|
62 |
+
color: white;
|
63 |
+
}
|
64 |
+
hr {
|
65 |
+
border-top: 1px solid #ffffff50;
|
66 |
+
margin: 2rem 0;
|
67 |
+
}
|
68 |
+
</style>
|
69 |
+
""", unsafe_allow_html=True)
|
70 |
+
|
71 |
+
# App title
|
72 |
+
st.markdown("<h1>π€ Deep Learning Mentor Chat</h1>", unsafe_allow_html=True)
|
73 |
+
st.markdown("<p>Learn Deep Learning with personalized AI mentorship</p>", unsafe_allow_html=True)
|
74 |
+
|
75 |
+
# Sidebar for experience level
|
76 |
+
st.sidebar.title("π Select Your Level")
|
77 |
+
exp = st.sidebar.selectbox("Experience Level", ["Beginner", "Intermediate", "Expert"])
|
78 |
+
|
79 |
+
# Load Deep Learning model
|
80 |
+
mentor_llm = HuggingFaceEndpoint(
|
81 |
+
repo_id='Qwen/Qwen3-32B',
|
82 |
+
provider='sambanova',
|
83 |
+
temperature=0.7,
|
84 |
+
max_new_tokens=150,
|
85 |
+
task='conversational'
|
86 |
+
)
|
87 |
+
|
88 |
+
deep_mentor = ChatHuggingFace(llm=mentor_llm)
|
89 |
+
|
90 |
+
# Session key for conversation
|
91 |
+
PAGE_KEY = "deep_learning_chat_history"
|
92 |
+
if PAGE_KEY not in st.session_state:
|
93 |
+
st.session_state[PAGE_KEY] = []
|
94 |
+
|
95 |
+
# Chat input form
|
96 |
+
st.markdown("<hr>", unsafe_allow_html=True)
|
97 |
+
with st.form(key="chat_form"):
|
98 |
+
user_input = st.text_input("π¬ Ask your deep learning question:")
|
99 |
+
submit = st.form_submit_button("Send")
|
100 |
+
|
101 |
+
# Handle chat submission
|
102 |
+
if submit and user_input:
|
103 |
+
system_prompt = (
|
104 |
+
f"You are a deep learning mentor with {exp.lower()} expertise. "
|
105 |
+
f"Only answer deep learning-related questions in a friendly, helpful tone under 150 words. "
|
106 |
+
f"If a question is off-topic, politely say it's out of scope."
|
107 |
+
)
|
108 |
+
messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
|
109 |
+
result = deep_mentor.invoke(messages)
|
110 |
+
st.session_state[PAGE_KEY].append((user_input, result.content))
|
111 |
+
|
112 |
+
# Chat history display with bubble styling
|
113 |
+
if st.session_state[PAGE_KEY]:
|
114 |
+
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
115 |
+
for user, bot in st.session_state[PAGE_KEY]:
|
116 |
+
st.markdown(f'<div class="chat-user">π€ <strong>You:</strong> {user}</div>', unsafe_allow_html=True)
|
117 |
+
st.markdown(f'<div class="chat-bot">π§βπ« <strong>Mentor:</strong> {bot}</div>', unsafe_allow_html=True)
|
118 |
+
st.markdown('</div>', unsafe_allow_html=True)
|