Spaces:
Sleeping
Sleeping
import streamlit as st | |
import os | |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace | |
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage | |
hf = os.getenv('Data_science') | |
os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf | |
os.environ['HF_TOKEN'] = hf | |
# --- Config --- | |
st.set_page_config(page_title="AI Mentor Chat", layout="centered") | |
st.title("π€ AI Mentor Chat") | |
# --- Sidebar for selections --- | |
st.sidebar.title("Mentor Preferences") | |
exp1 = ['<1', '1', '2', '3', '4', '5', '5+'] | |
exp = st.sidebar.selectbox("Select experience:", exp1) | |
# Map experience to label | |
experience_map = { | |
'<1': 'New bie mentor', | |
'1': '1', '2': '2', '3': '3', '4': '4', '5': '5', | |
'5+': 'Professional' | |
} | |
experience_label = experience_map[exp] | |
# --- Initialize Chat Model --- | |
deep_seek_skeleton = HuggingFaceEndpoint( | |
repo_id='meta-llama/Llama-3.2-3B-Instruct', | |
provider='sambanova', | |
temperature=0.7, | |
max_new_tokens=150, | |
task='conversational' | |
) | |
deep_seek = ChatHuggingFace( | |
llm=deep_seek_skeleton, | |
repo_id='meta-llama/Llama-3.2-3B-Instruct', | |
provider='sambanova', | |
temperature=0.7, | |
max_new_tokens=150, | |
task='conversational' | |
) | |
# --- Session State --- | |
if "chat_history" not in st.session_state: | |
st.session_state.chat_history = [] | |
# --- Chat Form --- | |
with st.form(key="chat_form"): | |
user_input = st.text_input("Ask your question:") | |
submit = st.form_submit_button("Send") | |
# --- Chat Logic --- | |
if submit and user_input: | |
# Add system context | |
system_prompt = f"Act as a python mentor who has {experience_label} years of experience who teaches in a very friendly manner, if question is not from python dont answer just tell that this is out of topic query from python" | |
# Create message list | |
messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)] | |
# Get model response | |
result = deep_seek.invoke(messages) | |
# Append to history | |
st.session_state.chat_history.append((user_input, result.content)) | |
# --- Display Chat History --- | |
st.subheader("π¨οΈ Chat History") | |
for i, (user, bot) in enumerate(st.session_state.chat_history): | |
st.markdown(f"**You:** {user}") | |
st.markdown(f"**Mentor:** {bot}") | |
st.markdown("---") |