VectorV17 commited on
Commit
8eb7a3c
·
verified ·
1 Parent(s): cd1de85

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -194
app.py CHANGED
@@ -1,62 +1,20 @@
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
-
43
  print("Configuring LLM Interface...")
44
 
45
- # Safety check for the token
46
  if not hf_token:
47
  print("⚠️ WARNING: HF_TOKEN is missing. The app will fail to generate responses.")
48
 
49
- # Using the native HF endpoint which understands "model waking up" statuses
50
  chatModel = HuggingFaceEndpoint(
51
- repo_id="mistralai/Mistral-7B-Instruct-v0.3", # More reliable free-tier model
52
  huggingfacehub_api_token=hf_token,
53
  temperature=0.3,
54
  max_new_tokens=400,
55
- task="text-generation",
56
- model_kwargs={"wait_for_model": True} # Explicitly tells the API to wait during cold starts
57
  )
58
 
59
- # Adjusting prompt for a standard text generation model instead of a chat model
60
  system_prompt = """You are the Simplilearn MOOC Support Agent, an advanced, polite, and professional AI learning assistant named Lane.
61
  Answer the student's inquiry accurately using ONLY the retrieved context below.
62
  If the answer cannot be found in the context, politely inform them that you only handle Simplilearn academic protocols and advise them to contact support@simplilearn.com.
@@ -68,150 +26,4 @@ User Inquiry: {input}
68
 
69
  Lane's Response:"""
70
 
71
- prompt = PromptTemplate.from_template(system_prompt)
72
-
73
- def format_docs(docs):
74
- return "\n\n".join(doc.page_content for doc in docs)
75
-
76
- rag_chain = (
77
- {
78
- "context": lambda x: format_docs(retriever.invoke(x["input"])),
79
- "input": lambda x: x["input"]
80
- }
81
- | prompt
82
- | chatModel
83
- | StrOutputParser()
84
- )
85
-
86
- # -------------------------------------------------------------------------
87
- # 3. Application Core Logic
88
- # -------------------------------------------------------------------------
89
- def process_chat(message, history):
90
- if not message.strip():
91
- return "", history
92
-
93
- if history is None:
94
- history = []
95
-
96
- try:
97
- # Executes the RAG Chain
98
- bot_response = rag_chain.invoke({"input": message})
99
-
100
- except Exception as e:
101
- # Gracefully handle HF timeout/wake-up errors instead of crashing the UI
102
- bot_response = (
103
- f"⚠️ **Connection Status:**\n\n"
104
- f"`{str(e)}`\n\n"
105
- 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!"
106
- )
107
-
108
- history.append({"role": "user", "content": message})
109
- history.append({"role": "assistant", "content": bot_response})
110
-
111
- return "", history
112
-
113
- def handle_quick_reply(button_text, history):
114
- return process_chat(button_text, history)
115
-
116
- # -------------------------------------------------------------------------
117
- # 4. Gradio Web Interface Layout
118
- # -------------------------------------------------------------------------
119
- # Improved CSS: Explicitly setting foreground text colors to prevent invisible text in dark mode
120
- custom_css = """
121
- .header-container { background-color: #f0f4f8 !important; padding: 25px; border-radius: 12px; margin-bottom: 20px; border: 1px solid #e2e8f0; }
122
- .header-container h1, .header-container p { color: #0f172a !important; }
123
- .meta-container { text-align: right; font-family: 'Georgia', serif; font-style: italic; }
124
- .meta-container h3 { color: #334155 !important; margin: 4px 0; }
125
- .sidebar-panel { background-color: #0f766e !important; color: white !important; padding: 25px; border-radius: 12px; text-align: center; }
126
- .sidebar-panel p, .sidebar-panel h2 { color: white !important; }
127
- .quick-btn { background-color: #f1f5f9 !important; border: 1px solid #cbd5e1 !important; color: #0f766e !important; font-weight: 600; }
128
- .quick-btn:hover { background-color: #e2e8f0 !important; }
129
- """
130
-
131
- # Native JS logic to toggle Gradio's standard dark mode class on the body
132
- toggle_js = """
133
- function() {
134
- document.body.classList.toggle('dark');
135
- }
136
- """
137
-
138
- with gr.Blocks(title="Simplilearn MOOC FAQ Agent") as demo:
139
-
140
- # TOP HEADER SECTION
141
- with gr.Row(elem_classes=["header-container"]):
142
- with gr.Column(scale=3):
143
- gr.Markdown("""
144
- # About Us
145
- At Simplilearn Agentic Hub, we are passionate about the power of structured education and its ability
146
- to transform careers. We are a leading knowledge processing framework dedicated to helping
147
- learners unlock the full potential of specialized MOOC educational pathways.
148
- """)
149
- with gr.Column(scale=2, elem_classes=["meta-container"]):
150
- current_date = datetime.datetime.now().strftime("%dth %B %Y | Time: %I:%M%p").lower()
151
- gr.Markdown(f"### {current_date}")
152
- mode_btn = gr.Button("☀️ 🌙 ➔ Change Mode (Day / Night)", variant="secondary", size="sm")
153
- mode_btn.click(fn=None, js=toggle_js)
154
-
155
- # MAIN WORKSPACE SECTION
156
- with gr.Row():
157
- # Chat interface
158
- with gr.Column(scale=4):
159
- chatbot = gr.Chatbot(
160
- label="Lane AI Support Stream",
161
- value=[{"role": "assistant", "content": "Hello! I'm Lane, your Simplilearn Support Agent. How can I assist you with your courses today?"}]
162
- )
163
-
164
- with gr.Row():
165
- user_input = gr.Textbox(
166
- show_label=False,
167
- placeholder="Ask Lane anything regarding Simplilearn policies...",
168
- scale=5
169
- )
170
- submit_btn = gr.Button("Send", variant="primary", scale=1)
171
-
172
- with gr.Row():
173
- btn_refund = gr.Button("What is the course refund policy?", elem_classes=["quick-btn"])
174
- btn_cert = gr.Button("How do I unlock my completion certificate?", elem_classes=["quick-btn"])
175
-
176
- # Agent Sidebar Identity
177
- with gr.Column(scale=2, elem_classes=["sidebar-panel"]):
178
- gr.HTML("""
179
- <div style='margin-top: 10px;'>
180
- <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;'>
181
- <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>
182
- <p style='font-style: italic; opacity: 0.9; margin-bottom: 15px;'>Your Agentic Learning Assistant</p>
183
- <hr style='border-color: rgba(255,255,255,0.2); margin: 15px 0;'>
184
- <p style='font-size: 14px; text-align: left; line-height: 1.5; opacity: 0.95;'>
185
- 🎓 <b>Secure Session Activated</b><br>
186
- Operational Node: Simplilearn-Chroma-01<br>
187
- Processing Hardware: Base CPU Node
188
- </p>
189
- </div>
190
- """)
191
-
192
- # Event Wiring Architecture
193
- submit_btn.click(
194
- fn=process_chat,
195
- inputs=[user_input, chatbot],
196
- outputs=[user_input, chatbot]
197
- )
198
- user_input.submit(
199
- fn=process_chat,
200
- inputs=[user_input, chatbot],
201
- outputs=[user_input, chatbot]
202
- )
203
-
204
- btn_refund.click(
205
- fn=handle_quick_reply,
206
- inputs=[btn_refund, chatbot],
207
- outputs=[user_input, chatbot]
208
- )
209
- btn_cert.click(
210
- fn=handle_quick_reply,
211
- inputs=[btn_cert, chatbot],
212
- outputs=[user_input, chatbot]
213
- )
214
-
215
- if __name__ == "__main__":
216
- # Launch with Gradio's Default theme to ensure dark/light mode hooks exist natively
217
- demo.launch(server_name="0.0.0.0", server_port=7860, css=custom_css, theme=gr.themes.Default())
 
1
+ # 2. Setup Robust LLM Interface via Native Hugging Face Endpoint
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  # -------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  print("Configuring LLM Interface...")
4
 
 
5
  if not hf_token:
6
  print("⚠️ WARNING: HF_TOKEN is missing. The app will fail to generate responses.")
7
 
8
+ # CORRECT CONFIGURATION: Pass cold-start directive via headers
9
  chatModel = HuggingFaceEndpoint(
10
+ repo_id="mistralai/Mistral-7B-Instruct-v0.3",
11
  huggingfacehub_api_token=hf_token,
12
  temperature=0.3,
13
  max_new_tokens=400,
14
+ headers={"X-Wait-For-Model": "true"} # Fixes the TypeError and handles cold starts gracefully
 
15
  )
16
 
17
+ # Use a clean text template matching the non-chat model endpoint structure
18
  system_prompt = """You are the Simplilearn MOOC Support Agent, an advanced, polite, and professional AI learning assistant named Lane.
19
  Answer the student's inquiry accurately using ONLY the retrieved context below.
20
  If the answer cannot be found in the context, politely inform them that you only handle Simplilearn academic protocols and advise them to contact support@simplilearn.com.
 
26
 
27
  Lane's Response:"""
28
 
29
+ prompt = PromptTemplate.from_template(system_prompt)