Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
|
| 4 |
+
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 5 |
+
|
| 6 |
+
# Set Hugging Face API token securely
|
| 7 |
+
HF_TOKEN = os.getenv("key")
|
| 8 |
+
os.environ["HUGGINGFACEHUB_API_TOKEN"] = HF_TOKEN
|
| 9 |
+
os.environ["HF_TOKEN"] = HF_TOKEN
|
| 10 |
+
|
| 11 |
+
# ---------- Page Configuration ----------
|
| 12 |
+
st.set_page_config(page_title="Innomatics Online Trainer Bot", page_icon="π§ ", layout="centered")
|
| 13 |
+
|
| 14 |
+
# ---------- Custom CSS ----------
|
| 15 |
+
st.markdown("""
|
| 16 |
+
<style>
|
| 17 |
+
.main {background-color: transparent; padding: 20px;}
|
| 18 |
+
.stButton>button {
|
| 19 |
+
background-color: white; color: #8B0000; border: 2px solid white;
|
| 20 |
+
border-radius: 10px; padding: 10px 20px; font-size: 18px;
|
| 21 |
+
font-weight: bold; width: 100%; transition: 0.3s ease-in-out;
|
| 22 |
+
}
|
| 23 |
+
.stButton>button:hover {
|
| 24 |
+
background-color: #8B0000; color: white; border: 2px solid white;
|
| 25 |
+
}
|
| 26 |
+
h1, h2, h3, p, div, span, label, input, textarea {
|
| 27 |
+
color: black !important;
|
| 28 |
+
}
|
| 29 |
+
</style>
|
| 30 |
+
""", unsafe_allow_html=True)
|
| 31 |
+
|
| 32 |
+
# ---------- UI Header ----------
|
| 33 |
+
st.markdown("<h1 style='text-align: center'>Innomatics AI Mentor Bot π§ </h1>", unsafe_allow_html=True)
|
| 34 |
+
st.markdown("### π Welcome to the Innomatics AI Mentor Bot π€")
|
| 35 |
+
st.markdown("""
|
| 36 |
+
This dashboard provides an AI mentor that gives instant, skill-adapted help
|
| 37 |
+
with Python, SQL, PowerBI, and data science to guide you through module doubts.
|
| 38 |
+
""")
|
| 39 |
+
st.markdown("## In which module do you have doubt?")
|
| 40 |
+
|
| 41 |
+
# ---------- Module Buttons ----------
|
| 42 |
+
modules = {
|
| 43 |
+
"Python": "π",
|
| 44 |
+
"SQL": "ποΈ",
|
| 45 |
+
"PowerBI": "π",
|
| 46 |
+
"Statistics": "π",
|
| 47 |
+
"Machine_Learning": "π€",
|
| 48 |
+
"Deep_Learning": "π§ "
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
cols = st.columns(3)
|
| 52 |
+
for i, (module, emoji) in enumerate(modules.items()):
|
| 53 |
+
if cols[i % 3].button(f"{emoji} {module}", key=f"{module}_btn"):
|
| 54 |
+
st.session_state.mentor_type = module
|
| 55 |
+
st.session_state.mentor_emoji = emoji
|
| 56 |
+
|
| 57 |
+
# ---------- Session State Defaults ----------
|
| 58 |
+
st.session_state.setdefault("mentor_type", None)
|
| 59 |
+
st.session_state.setdefault("mentor_emoji", "π§ ")
|
| 60 |
+
|
| 61 |
+
# ---------- Chat Interface ----------
|
| 62 |
+
if st.session_state.mentor_type:
|
| 63 |
+
mentor = st.session_state.mentor_type
|
| 64 |
+
emoji = st.session_state.mentor_emoji
|
| 65 |
+
|
| 66 |
+
st.subheader(f"{emoji} {mentor.upper()} Mentor Chat")
|
| 67 |
+
experience = st.slider("Your experience (in years):", 0, 20, 1)
|
| 68 |
+
user_input = st.text_input("Ask your question:")
|
| 69 |
+
output_container = st.empty()
|
| 70 |
+
|
| 71 |
+
# Select HuggingFace model based on module
|
| 72 |
+
model_map = {
|
| 73 |
+
"Python": ("meta-llama/Llama-3.1-8B-Instruct", "nebius"),
|
| 74 |
+
"SQL": ("deepseek-ai/DeepSeek-R1", "nebius"),
|
| 75 |
+
"PowerBI": ("deepseek-ai/DeepSeek-R1", "nebius"),
|
| 76 |
+
"Statistics": ("meta-llama/Llama-3.2-1B-Instruct", "nebius"),
|
| 77 |
+
"Machine_Learning": ("meta-llama/Llama-3.3-70B-Instruct", "nebius"),
|
| 78 |
+
"Deep_Learning": ("meta-llama/Meta-Llama-3-70B-Instruct", "hyperbolic")
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
repo_id, provider = model_map.get(mentor, (None, None))
|
| 82 |
+
if repo_id:
|
| 83 |
+
model = HuggingFaceEndpoint(repo_id=repo_id, provider=provider, temperature=0.5, max_new_tokens=150)
|
| 84 |
+
chat_model = ChatHuggingFace(llm=model)
|
| 85 |
+
|
| 86 |
+
col1, col2 = st.columns(2)
|
| 87 |
+
|
| 88 |
+
with col1:
|
| 89 |
+
if st.button("π Ask", key="ask_btn"):
|
| 90 |
+
if user_input:
|
| 91 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 92 |
+
SystemMessagePromptTemplate.from_template(
|
| 93 |
+
f"You are a helpful and experienced {mentor.upper()} mentor {emoji} assisting a learner with {experience} years of experience."
|
| 94 |
+
),
|
| 95 |
+
HumanMessagePromptTemplate.from_template("{question}")
|
| 96 |
+
])
|
| 97 |
+
formatted_prompt = prompt.format_messages(question=user_input)
|
| 98 |
+
|
| 99 |
+
with st.spinner(f"{emoji} Mentor is thinking..."):
|
| 100 |
+
try:
|
| 101 |
+
response = chat_model.invoke(formatted_prompt)
|
| 102 |
+
output_container.markdown(f"**π€ You:** {user_input}")
|
| 103 |
+
output_container.markdown(f"**{emoji} Mentor:** {response.content}")
|
| 104 |
+
except Exception as e:
|
| 105 |
+
output_container.error(f"β An error occurred: {str(e)}")
|
| 106 |
+
else:
|
| 107 |
+
output_container.warning("β οΈ Please enter a question first!")
|
| 108 |
+
|
| 109 |
+
with col2:
|
| 110 |
+
if st.button("π§Ή Clear", key="clear_btn"):
|
| 111 |
+
output_container.empty()
|