Update src/streamlit_app.py
Browse files- src/streamlit_app.py +69 -66
src/streamlit_app.py
CHANGED
|
@@ -3,101 +3,104 @@ import streamlit as st
|
|
| 3 |
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
|
| 4 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 5 |
|
| 6 |
-
#
|
| 7 |
os.environ['HUGGINGFACEHUB_API_TOKEN'] = os.getenv("key")
|
| 8 |
os.environ['HF_TOKEN'] = os.getenv("key")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
st.set_page_config(page_title="π§
|
| 12 |
-
st.title("π
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
"
|
| 19 |
-
"
|
| 20 |
-
"
|
| 21 |
-
"
|
| 22 |
-
"
|
|
|
|
| 23 |
}
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
if st.button(MENTORS[mentor_keys[i]][0]):
|
| 34 |
-
selected = mentor_keys[i]
|
| 35 |
-
st.session_state["selected_mentor"] = selected
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
if "
|
| 39 |
-
|
| 40 |
|
| 41 |
-
if
|
| 42 |
-
|
| 43 |
-
st.subheader(f"{
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
experience = st.slider("π
Your
|
| 47 |
-
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
# Load the model
|
| 52 |
model = HuggingFaceEndpoint(
|
| 53 |
repo_id=repo_id,
|
| 54 |
provider=provider,
|
| 55 |
temperature=0.5,
|
| 56 |
-
max_new_tokens=
|
| 57 |
task="conversational"
|
| 58 |
)
|
|
|
|
| 59 |
chat_model = ChatHuggingFace(
|
| 60 |
llm=model,
|
| 61 |
repo_id=repo_id,
|
| 62 |
-
provider=provider,
|
| 63 |
temperature=0.5,
|
| 64 |
-
max_new_tokens=
|
| 65 |
task="conversational"
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
st.session_state["history"] = []
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
prompt = ChatPromptTemplate.from_messages([
|
| 75 |
SystemMessagePromptTemplate.from_template(
|
| 76 |
-
f"You are a helpful and
|
| 77 |
),
|
| 78 |
HumanMessagePromptTemplate.from_template("{question}")
|
| 79 |
])
|
| 80 |
-
|
| 81 |
|
| 82 |
with st.spinner("Thinking..."):
|
| 83 |
-
response = chat_model.invoke(
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
)
|
|
|
|
| 3 |
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
|
| 4 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 5 |
|
| 6 |
+
# Load Hugging Face token
|
| 7 |
os.environ['HUGGINGFACEHUB_API_TOKEN'] = os.getenv("key")
|
| 8 |
os.environ['HF_TOKEN'] = os.getenv("key")
|
| 9 |
|
| 10 |
+
# Page config
|
| 11 |
+
st.set_page_config(page_title="π§ AI Mentor Hub", page_icon="π")
|
| 12 |
+
st.title("π AI Mentor Hub - Learn Smarter, Faster!")
|
| 13 |
+
|
| 14 |
+
st.markdown("### π Select your mentor and ask any question:")
|
| 15 |
+
|
| 16 |
+
# Emoji mentor labels
|
| 17 |
+
mentor_labels = {
|
| 18 |
+
"python": "π Python",
|
| 19 |
+
"machine_learning": "π€ Machine Learning",
|
| 20 |
+
"deep_learning": "π§ Deep Learning",
|
| 21 |
+
"stats": "π Statistics",
|
| 22 |
+
"data_analysis": "π§Ύ Data Analysis",
|
| 23 |
+
"sql_powerbi": "ποΈ SQL & Power BI"
|
| 24 |
}
|
| 25 |
|
| 26 |
+
mentor_configs = {
|
| 27 |
+
"python": {"repo_id": "meta-llama/Llama-3.1-8B-Instruct", "provider": "nebius"},
|
| 28 |
+
"machine_learning": {"repo_id": "deepseek-ai/DeepSeek-R1", "provider": "nebius"},
|
| 29 |
+
"deep_learning": {"repo_id": "deepseek-ai/DeepSeek-R1", "provider": "nebius"},
|
| 30 |
+
"stats": {"repo_id": "meta-llama/Llama-3.2-1B-Instruct", "provider": "nebius"},
|
| 31 |
+
"data_analysis": {"repo_id": "meta-llama/Llama-3.3-70B-Instruct", "provider": "nebius"},
|
| 32 |
+
"sql_powerbi": {"repo_id": "meta-llama/Meta-Llama-3-70B-Instruct", "provider": "hyperbolic"}
|
| 33 |
+
}
|
| 34 |
|
| 35 |
+
# Show options
|
| 36 |
+
mentor_choice = st.selectbox("Choose a mentor topic:", [""] + list(mentor_labels.keys()))
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# Storage for conversation
|
| 39 |
+
if "conversation" not in st.session_state:
|
| 40 |
+
st.session_state.conversation = []
|
| 41 |
|
| 42 |
+
if mentor_choice:
|
| 43 |
+
label = mentor_labels[mentor_choice]
|
| 44 |
+
st.subheader(f"{label} Mentor Chat")
|
| 45 |
|
| 46 |
+
# Inputs
|
| 47 |
+
experience = st.slider("π
Your experience (years):", 0, 20, 1)
|
| 48 |
+
question = st.text_input("π¬ Ask your question:")
|
| 49 |
|
| 50 |
+
# Model setup
|
| 51 |
+
config = mentor_configs[mentor_choice]
|
| 52 |
+
repo_id = config["repo_id"]
|
| 53 |
+
provider = config["provider"]
|
| 54 |
|
|
|
|
| 55 |
model = HuggingFaceEndpoint(
|
| 56 |
repo_id=repo_id,
|
| 57 |
provider=provider,
|
| 58 |
temperature=0.5,
|
| 59 |
+
max_new_tokens=150,
|
| 60 |
task="conversational"
|
| 61 |
)
|
| 62 |
+
|
| 63 |
chat_model = ChatHuggingFace(
|
| 64 |
llm=model,
|
| 65 |
repo_id=repo_id,
|
| 66 |
+
provider=provider, # FIXED: use correct provider dynamically
|
| 67 |
temperature=0.5,
|
| 68 |
+
max_new_tokens=150,
|
| 69 |
task="conversational"
|
| 70 |
)
|
| 71 |
|
| 72 |
+
output_box = st.empty()
|
|
|
|
| 73 |
|
| 74 |
+
if st.button("π§ Get Answer"):
|
| 75 |
+
if not question.strip():
|
| 76 |
+
st.warning("β Please enter a question.")
|
| 77 |
+
else:
|
| 78 |
prompt = ChatPromptTemplate.from_messages([
|
| 79 |
SystemMessagePromptTemplate.from_template(
|
| 80 |
+
f"You are a helpful and expert {mentor_choice.replace('_', ' ').title()} mentor. The user has {experience} years of experience. Answer clearly."
|
| 81 |
),
|
| 82 |
HumanMessagePromptTemplate.from_template("{question}")
|
| 83 |
])
|
| 84 |
+
messages = prompt.format_messages(question=question)
|
| 85 |
|
| 86 |
with st.spinner("Thinking..."):
|
| 87 |
+
response = chat_model.invoke(messages)
|
| 88 |
+
|
| 89 |
+
answer = response.content
|
| 90 |
+
output_box.markdown(f"π€ **You:** {question}")
|
| 91 |
+
output_box.markdown(f"π§ **Mentor:** {answer}")
|
| 92 |
+
st.session_state.conversation.append(f"You: {question}")
|
| 93 |
+
st.session_state.conversation.append(f"Mentor: {answer}")
|
| 94 |
+
|
| 95 |
+
if st.button("ποΈ Clear Chat"):
|
| 96 |
+
output_box.empty()
|
| 97 |
+
st.session_state.conversation = []
|
| 98 |
+
|
| 99 |
+
if st.session_state.conversation:
|
| 100 |
+
convo_text = "\n".join(st.session_state.conversation)
|
| 101 |
+
st.download_button(
|
| 102 |
+
"β¬οΈ Download Conversation",
|
| 103 |
+
data=convo_text,
|
| 104 |
+
file_name=f"{mentor_choice}_chat.txt",
|
| 105 |
+
mime="text/plain"
|
| 106 |
+
)
|
|
|