Spaces:
Sleeping
Sleeping
Update pages/deep_learning.py
Browse files- pages/deep_learning.py +73 -1
pages/deep_learning.py
CHANGED
|
@@ -1,2 +1,74 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 4 |
+
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
hf = os.getenv('hf')
|
| 9 |
+
os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
|
| 10 |
+
os.environ['HF_TOKEN'] = hf
|
| 11 |
+
# --- Config ---
|
| 12 |
+
st.set_page_config(page_title="AI Mentor Chat", layout="centered")
|
| 13 |
+
st.title("🤖 AI Mentor Chat")
|
| 14 |
+
|
| 15 |
+
# --- Sidebar for selections ---
|
| 16 |
+
st.sidebar.title("Mentor Preferences")
|
| 17 |
+
exp1 = ['<1', '1', '2', '3', '4', '5', '5+']
|
| 18 |
+
exp = st.sidebar.selectbox("Select experience:", exp1)
|
| 19 |
+
|
| 20 |
+
# Map experience to label
|
| 21 |
+
experience_map = {
|
| 22 |
+
'<1': 'New bie mentor',
|
| 23 |
+
'1': '1', '2': '2', '3': '3', '4': '4', '5': '5',
|
| 24 |
+
'5+': 'Professional'
|
| 25 |
+
}
|
| 26 |
+
experience_label = experience_map[exp]
|
| 27 |
+
|
| 28 |
+
# --- Initialize Chat Model ---
|
| 29 |
+
deep_seek_skeleton = HuggingFaceEndpoint(
|
| 30 |
+
repo_id='Qwen/Qwen3-32B',
|
| 31 |
+
provider='sambanova',
|
| 32 |
+
temperature=0.7,
|
| 33 |
+
max_new_tokens=150,
|
| 34 |
+
task='conversational'
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
deep_seek = ChatHuggingFace(
|
| 38 |
+
llm=deep_seek_skeleton,
|
| 39 |
+
repo_id='Qwen/Qwen3-32B',
|
| 40 |
+
provider='sambanova',
|
| 41 |
+
temperature=0.7,
|
| 42 |
+
max_new_tokens=150,
|
| 43 |
+
task='conversational'
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# --- Session State ---
|
| 47 |
+
if "chat_history" not in st.session_state:
|
| 48 |
+
st.session_state.chat_history = []
|
| 49 |
+
|
| 50 |
+
# --- Chat Form ---
|
| 51 |
+
with st.form(key="chat_form"):
|
| 52 |
+
user_input = st.text_input("Ask your question:")
|
| 53 |
+
submit = st.form_submit_button("Send")
|
| 54 |
+
|
| 55 |
+
# --- Chat Logic ---
|
| 56 |
+
if submit and user_input:
|
| 57 |
+
# Add system context
|
| 58 |
+
system_prompt = f"Act as a deep learning mentor who has {experience_label} years of experience who teaches in a very friendly manner and also tells everything in within 150 words. If any question is asked other than python tell politely that this is out of the topic question"
|
| 59 |
+
|
| 60 |
+
# Create message list
|
| 61 |
+
messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
|
| 62 |
+
|
| 63 |
+
# Get model response
|
| 64 |
+
result = deep_seek.invoke(messages)
|
| 65 |
+
|
| 66 |
+
# Append to history
|
| 67 |
+
st.session_state.chat_history.append((user_input, result.content))
|
| 68 |
+
|
| 69 |
+
# --- Display Chat History ---
|
| 70 |
+
st.subheader("🗨️ Chat History")
|
| 71 |
+
for i, (user, bot) in enumerate(st.session_state.chat_history):
|
| 72 |
+
st.markdown(f"**You:** {user}")
|
| 73 |
+
st.markdown(f"**Mentor:** {bot}")
|
| 74 |
+
st.markdown("---")
|