Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,43 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
# -------------------------------------------------------------------------
|
| 3 |
print("Configuring LLM Interface...")
|
| 4 |
|
|
@@ -26,4 +65,150 @@ User Inquiry: {input}
|
|
| 26 |
|
| 27 |
Lane's Response:"""
|
| 28 |
|
| 29 |
-
prompt = PromptTemplate.from_template(system_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import datetime
|
| 3 |
+
import warnings
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Suppress deprecation warnings for a cleaner console
|
| 7 |
+
warnings.filterwarnings("ignore", category=UserWarning, module="langchain_community")
|
| 8 |
+
|
| 9 |
+
from langchain_chroma import Chroma
|
| 10 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 11 |
+
from langchain_openai import ChatOpenAI
|
| 12 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 13 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 14 |
+
from dotenv import load_dotenv
|
| 15 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
| 16 |
+
from langchain_core.prompts import PromptTemplate
|
| 17 |
+
|
| 18 |
+
# Load Environment Variables
|
| 19 |
+
load_dotenv()
|
| 20 |
+
hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 21 |
+
|
| 22 |
+
# -------------------------------------------------------------------------
|
| 23 |
+
# 1. Setup Vector Store & Embeddings Pipeline
|
| 24 |
+
# -------------------------------------------------------------------------
|
| 25 |
+
print("Loading semantic embedding vectors...")
|
| 26 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 27 |
+
|
| 28 |
+
simplilearn_faq_data = [
|
| 29 |
+
"Simplilearn Refund Policy: Learners are entitled to a full 100% refund within 7 days of course purchase, provided they have not accessed more than 25% of the unlocked asynchronous content or attended any live virtual cohort sessions.",
|
| 30 |
+
"Professional Certification Requirements: To unlock your official Simplilearn digital certificate, you must achieve a minimum score of 80% in the final course assessment and successfully submit all mandatory course projects.",
|
| 31 |
+
"Live Virtual Class Attendance: If a student misses an interactive live session, high-definition cloud recordings are automatically processed and made available on the student LMS dashboard within 24 hours.",
|
| 32 |
+
"Technical Support Availability: Technical assistance for cloud labs, environment resets, or workspace access is available 24/7. Students can raise a ticket via the 'Help and Support' tab or email support@simplilearn.com.",
|
| 33 |
+
"Data Science Bootcamp Prerequisites: This flagship program spans 6 months. It requires basic exposure to high-school level mathematics and rudimentary programming logic (Python familiarity is highly recommended but not mandatory)."
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
vectorstore = Chroma.from_texts(texts=simplilearn_faq_data, embedding=embeddings)
|
| 37 |
+
retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 2})
|
| 38 |
+
|
| 39 |
+
# -------------------------------------------------------------------------
|
| 40 |
+
# 2. Setup Robust LLM Interface via OpenAI-Compatible Endpoint
|
| 41 |
# -------------------------------------------------------------------------
|
| 42 |
print("Configuring LLM Interface...")
|
| 43 |
|
|
|
|
| 65 |
|
| 66 |
Lane's Response:"""
|
| 67 |
|
| 68 |
+
prompt = PromptTemplate.from_template(system_prompt)
|
| 69 |
+
|
| 70 |
+
def format_docs(docs):
|
| 71 |
+
return "\n\n".join(doc.page_content for doc in docs)
|
| 72 |
+
|
| 73 |
+
rag_chain = (
|
| 74 |
+
{
|
| 75 |
+
"context": lambda x: format_docs(retriever.invoke(x["input"])),
|
| 76 |
+
"input": lambda x: x["input"]
|
| 77 |
+
}
|
| 78 |
+
| prompt
|
| 79 |
+
| chatModel
|
| 80 |
+
| StrOutputParser()
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
# -------------------------------------------------------------------------
|
| 84 |
+
# 3. Application Core Logic
|
| 85 |
+
# -------------------------------------------------------------------------
|
| 86 |
+
def process_chat(message, history):
|
| 87 |
+
if not message.strip():
|
| 88 |
+
return "", history
|
| 89 |
+
|
| 90 |
+
if history is None:
|
| 91 |
+
history = []
|
| 92 |
+
|
| 93 |
+
try:
|
| 94 |
+
# Executes the RAG Chain
|
| 95 |
+
bot_response = rag_chain.invoke({"input": message})
|
| 96 |
+
|
| 97 |
+
except Exception as e:
|
| 98 |
+
# Gracefully handle HF timeout/wake-up errors instead of crashing the UI
|
| 99 |
+
bot_response = (
|
| 100 |
+
f"⚠️ **Connection Status:**\n\n"
|
| 101 |
+
f"`{str(e)}`\n\n"
|
| 102 |
+
f"**Note:** The Hugging Face inference model is currently waking up. This takes about 30-60 seconds. Please wait a moment and try sending your message again!"
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
history.append({"role": "user", "content": message})
|
| 106 |
+
history.append({"role": "assistant", "content": bot_response})
|
| 107 |
+
|
| 108 |
+
return "", history
|
| 109 |
+
|
| 110 |
+
def handle_quick_reply(button_text, history):
|
| 111 |
+
return process_chat(button_text, history)
|
| 112 |
+
|
| 113 |
+
# -------------------------------------------------------------------------
|
| 114 |
+
# 4. Gradio Web Interface Layout
|
| 115 |
+
# -------------------------------------------------------------------------
|
| 116 |
+
# Improved CSS: Explicitly setting foreground text colors to prevent invisible text in dark mode
|
| 117 |
+
custom_css = """
|
| 118 |
+
.header-container { background-color: #f0f4f8 !important; padding: 25px; border-radius: 12px; margin-bottom: 20px; border: 1px solid #e2e8f0; }
|
| 119 |
+
.header-container h1, .header-container p { color: #0f172a !important; }
|
| 120 |
+
.meta-container { text-align: right; font-family: 'Georgia', serif; font-style: italic; }
|
| 121 |
+
.meta-container h3 { color: #334155 !important; margin: 4px 0; }
|
| 122 |
+
.sidebar-panel { background-color: #0f766e !important; color: white !important; padding: 25px; border-radius: 12px; text-align: center; }
|
| 123 |
+
.sidebar-panel p, .sidebar-panel h2 { color: white !important; }
|
| 124 |
+
.quick-btn { background-color: #f1f5f9 !important; border: 1px solid #cbd5e1 !important; color: #0f766e !important; font-weight: 600; }
|
| 125 |
+
.quick-btn:hover { background-color: #e2e8f0 !important; }
|
| 126 |
+
"""
|
| 127 |
+
|
| 128 |
+
# Native JS logic to toggle Gradio's standard dark mode class on the body
|
| 129 |
+
toggle_js = """
|
| 130 |
+
function() {
|
| 131 |
+
document.body.classList.toggle('dark');
|
| 132 |
+
}
|
| 133 |
+
"""
|
| 134 |
+
|
| 135 |
+
with gr.Blocks(title="Simplilearn MOOC FAQ Agent") as demo:
|
| 136 |
+
|
| 137 |
+
# TOP HEADER SECTION
|
| 138 |
+
with gr.Row(elem_classes=["header-container"]):
|
| 139 |
+
with gr.Column(scale=3):
|
| 140 |
+
gr.Markdown("""
|
| 141 |
+
# About Us
|
| 142 |
+
At Simplilearn Agentic Hub, we are passionate about the power of structured education and its ability
|
| 143 |
+
to transform careers. We are a leading knowledge processing framework dedicated to helping
|
| 144 |
+
learners unlock the full potential of specialized MOOC educational pathways.
|
| 145 |
+
""")
|
| 146 |
+
with gr.Column(scale=2, elem_classes=["meta-container"]):
|
| 147 |
+
current_date = datetime.datetime.now().strftime("%dth %B %Y | Time: %I:%M%p").lower()
|
| 148 |
+
gr.Markdown(f"### {current_date}")
|
| 149 |
+
mode_btn = gr.Button("☀️ 🌙 ➔ Change Mode (Day / Night)", variant="secondary", size="sm")
|
| 150 |
+
mode_btn.click(fn=None, js=toggle_js)
|
| 151 |
+
|
| 152 |
+
# MAIN WORKSPACE SECTION
|
| 153 |
+
with gr.Row():
|
| 154 |
+
# Chat interface
|
| 155 |
+
with gr.Column(scale=4):
|
| 156 |
+
chatbot = gr.Chatbot(
|
| 157 |
+
label="Lane AI Support Stream",
|
| 158 |
+
value=[{"role": "assistant", "content": "Hello! I'm Lane, your Simplilearn Support Agent. How can I assist you with your courses today?"}]
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
with gr.Row():
|
| 162 |
+
user_input = gr.Textbox(
|
| 163 |
+
show_label=False,
|
| 164 |
+
placeholder="Ask Lane anything regarding Simplilearn policies...",
|
| 165 |
+
scale=5
|
| 166 |
+
)
|
| 167 |
+
submit_btn = gr.Button("Send", variant="primary", scale=1)
|
| 168 |
+
|
| 169 |
+
with gr.Row():
|
| 170 |
+
btn_refund = gr.Button("What is the course refund policy?", elem_classes=["quick-btn"])
|
| 171 |
+
btn_cert = gr.Button("How do I unlock my completion certificate?", elem_classes=["quick-btn"])
|
| 172 |
+
|
| 173 |
+
# Agent Sidebar Identity
|
| 174 |
+
with gr.Column(scale=2, elem_classes=["sidebar-panel"]):
|
| 175 |
+
gr.HTML("""
|
| 176 |
+
<div style='margin-top: 10px;'>
|
| 177 |
+
<img src='https://cdn-icons-png.flaticon.com/512/4712/4712010.png' alt='Lane Avatar' style='width:120px; height:120px; object-fit:cover; margin:0 auto 15px auto; border: 4px solid #ffffff; border-radius: 50%; background-color: white;'>
|
| 178 |
+
<h2 style='margin-bottom: 5px; font-size: 24px; font-weight: bold;'>Lane <span style='background-color:#ffffff; color:#0f766e; padding:2px 8px; border-radius:4px; font-size:14px; vertical-align: middle;'>AI</span></h2>
|
| 179 |
+
<p style='font-style: italic; opacity: 0.9; margin-bottom: 15px;'>Your Agentic Learning Assistant</p>
|
| 180 |
+
<hr style='border-color: rgba(255,255,255,0.2); margin: 15px 0;'>
|
| 181 |
+
<p style='font-size: 14px; text-align: left; line-height: 1.5; opacity: 0.95;'>
|
| 182 |
+
🎓 <b>Secure Session Activated</b><br>
|
| 183 |
+
Operational Node: Simplilearn-Chroma-01<br>
|
| 184 |
+
Processing Hardware: Base CPU Node
|
| 185 |
+
</p>
|
| 186 |
+
</div>
|
| 187 |
+
""")
|
| 188 |
+
|
| 189 |
+
# Event Wiring Architecture
|
| 190 |
+
submit_btn.click(
|
| 191 |
+
fn=process_chat,
|
| 192 |
+
inputs=[user_input, chatbot],
|
| 193 |
+
outputs=[user_input, chatbot]
|
| 194 |
+
)
|
| 195 |
+
user_input.submit(
|
| 196 |
+
fn=process_chat,
|
| 197 |
+
inputs=[user_input, chatbot],
|
| 198 |
+
outputs=[user_input, chatbot]
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
btn_refund.click(
|
| 202 |
+
fn=handle_quick_reply,
|
| 203 |
+
inputs=[btn_refund, chatbot],
|
| 204 |
+
outputs=[user_input, chatbot]
|
| 205 |
+
)
|
| 206 |
+
btn_cert.click(
|
| 207 |
+
fn=handle_quick_reply,
|
| 208 |
+
inputs=[btn_cert, chatbot],
|
| 209 |
+
outputs=[user_input, chatbot]
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
if __name__ == "__main__":
|
| 213 |
+
# Launch with Gradio's Default theme to ensure dark/light mode hooks exist natively
|
| 214 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, css=custom_css, theme=gr.themes.Default())
|