VectorV17 commited on
Commit
0c4c5ce
·
verified ·
1 Parent(s): bb693e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -307
app.py CHANGED
@@ -1,319 +1,144 @@
1
- import os
2
  import gradio as gr
3
- import numpy as np
4
- from datetime import datetime
5
  from huggingface_hub import InferenceClient
6
- from sentence_transformers import SentenceTransformer
7
-
8
- # 1. Document Corpus / Institutional Knowledge Base
9
- SIMPLILEARN_KNOWLEDGE_BASE = [
10
- {
11
- "category": "Refund Policy",
12
- "text": "Simplilearn offers a structured 7-day refund policy for our MOOC and specialized educational pathways. This is fully applicable provided that the learner has not accessed more than 25% of the overall self-paced course content, has not attended any interactive live classes, and has not unlocked or attempted any certification modules."
13
- },
14
- {
15
- "category": "Certificate Requirements",
16
- "text": "To successfully unlock and download your completion certificate, you must strictly fulfill three institutional criteria:\n1. Achieve 100% complete completion of all self-paced video lectures.\n2. Score a minimum grade of 80% or higher on all core module quizzes.\n3. Successfully submit and secure a passing grade from evaluators on the industry-aligned Capstone Project."
17
- },
18
- {
19
- "category": "Course Prerequisites",
20
- "text": "Prerequisites vary by pathway. For our fundamental foundational programs, a basic understanding of mathematics and logic is sufficient. However, for advanced programs (such as the AI Engineer specialized track), we strongly recommend prior familiarity with Python programming, fundamental data structures, and basic statistics."
21
- },
22
- {
23
- "category": "Technical Support & Lab Environments",
24
- "text": "If you are experiencing issues with cloud virtual lab environments, IDE sandboxes, or video playback latency on the Learning Management System (LMS), you can access the Simplilearn Support Help Center 24/7. Alternatively, you can open a high-priority assistance ticket directly through the Support Tab found on your learner dashboard."
25
- }
26
- ]
27
-
28
- # 2. Initialize Lightweight Vector Search Models globally
29
- # Loads a compact, high-efficiency embedding engine (under 90MB) optimal for Basic CPU Spaces
30
- print("Initializing Embedding Engine...")
31
- embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
32
-
33
- # Pre-compute document embeddings at application startup for instant matching
34
- corpus_texts = [f"[{doc['category']}]: {doc['text']}" for doc in SIMPLILEARN_KNOWLEDGE_BASE]
35
- corpus_embeddings = embedding_model.encode(corpus_texts, convert_to_numpy=True)
36
-
37
- # Connect to Serverless Inference client (reads the HF_TOKEN space secret automatically)
38
- hf_token = os.getenv("HF_TOKEN")
39
- client = InferenceClient(model="Qwen/Qwen2.5-7B-Instruct", token=hf_token)
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
- # 3. Vector Search Engine (RAG Retriever)
43
- def retrieve_relevant_context(user_query):
44
  """
45
- Performs real-time local mathematical vector embedding comparison
46
- to extract the most accurate context chunk from the knowledge base.
47
  """
48
- query_vector = embedding_model.encode([user_query], convert_to_numpy=True)
49
-
50
- # Compute fast Cosine Similarity metrics
51
- similarity_scores = np.dot(corpus_embeddings, query_vector.T).flatten() / (
52
- np.linalg.norm(corpus_embeddings, axis=1) * np.linalg.norm(query_vector)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  )
54
 
55
- best_match_idx = np.argmax(similarity_scores)
56
- return corpus_texts[best_match_idx], similarity_scores[best_match_idx]
57
-
58
-
59
- # 4. Agentic RAG Controller Logic (Compatible with Gradio 5.x / 6.0+)
60
- def chat_respond(user_message, chat_history):
61
- # 1. FORCE CONVERT the history to standard Python dictionaries for the Hugging Face API
62
- api_messages = [{"role": "system", "content": SYSTEM_PROMPT}]
63
-
64
- for msg in chat_history:
65
- if hasattr(msg, "role") and hasattr(msg, "content"):
66
- # If it's a Gradio 6 ChatMessage object, extract properties directly
67
- api_messages.append({"role": msg.role, "content": msg.content})
68
- elif isinstance(msg, dict):
69
- # If it's already a dictionary
70
- api_messages.append({"role": msg.get("role", "user"), "content": msg.get("content", "")})
71
- elif isinstance(msg, (list, tuple)) and len(msg) >= 2:
72
- # Fallback wrapper for old-style tuples
73
- api_messages.append({"role": msg[0], "content": msg[1]})
74
-
75
- # Handle case where user_message comes from a MultimodalTextbox component
76
- if isinstance(user_message, dict) and "text" in user_message:
77
- user_text = user_message["text"]
78
- else:
79
- user_text = str(user_message)
80
-
81
- # Append current user message to the API payload
82
- api_messages.append({"role": "user", "content": user_text})
83
-
84
- # 2. Append to chat_history to display it instantly in the Gradio UI
85
- chat_history.append({"role": "user", "content": user_text})
86
- chat_history.append({"role": "assistant", "content": ""})
87
-
88
- # Clear the input box and show the empty assistant block layout immediately
89
- yield "", chat_history
90
-
91
  try:
92
- # 3. Request completion using the strictly cleaned api_messages list
93
- stream_response = client.chat_completion(
94
- messages=api_messages,
95
- max_tokens=1024,
96
- stream=True
97
- )
98
-
99
- running_text = ""
100
- for token_chunk in stream_response:
101
- if token_chunk.choices and token_chunk.choices[0].delta.content:
102
- token = token_chunk.choices[0].delta.content
103
- running_text += token
104
-
105
- # Safely update the final item in history depending on its type state
106
- if isinstance(chat_history[-1], dict):
107
- chat_history[-1]["content"] = running_text
108
- else:
109
- chat_history[-1].content = running_text
110
-
111
- yield "", chat_history
112
-
113
  except Exception as e:
114
- print(f"Error encountered during streaming: {e}")
115
- # Prevent UI from freezing by displaying the error message directly in the chat window
116
- error_display = f"An error occurred: {str(e)}"
117
- if isinstance(chat_history[-1], dict):
118
- chat_history[-1]["content"] = error_display
119
- else:
120
- chat_history[-1].content = error_display
121
- yield "", chat_history
122
-
123
-
124
- # 5. UI Layout Styling Engine
125
- custom_css = """
126
- body, .gradio-container {
127
- background-color: #0b0f17 !important;
128
- color: #ffffff !important;
129
- font-family: 'Inter', sans-serif;
130
- }
131
- .about-card {
132
- background-color: #161c2a !important;
133
- border: 1px solid #232d42 !important;
134
- border-radius: 8px !important;
135
- padding: 20px !important;
136
- margin-bottom: 20px !important;
137
- }
138
- .about-title {
139
- font-size: 24px !important;
140
- font-weight: 700 !important;
141
- color: #ffffff !important;
142
- margin-bottom: 10px !important;
143
- }
144
- .time-stamp {
145
- text-align: right !important;
146
- color: #a0aec0 !important;
147
- font-style: italic !important;
148
- font-size: 14px !important;
149
- }
150
- .mode-btn {
151
- background-color: #2d3748 !important;
152
- color: #ffffff !important;
153
- border: none !important;
154
- border-radius: 4px !important;
155
- padding: 5px 10px !important;
156
- font-size: 13px !important;
157
- cursor: pointer !important;
158
- }
159
- .lane-card {
160
- background-color: #065f51 !important;
161
- color: #ffffff !important;
162
- border-radius: 12px !important;
163
- padding: 25px !important;
164
- text-align: center !important;
165
- height: 100%;
166
- }
167
- .lane-avatar {
168
- width: 100px !important;
169
- height: 100px !important;
170
- background-color: #ffffff !important;
171
- border-radius: 50% !important;
172
- margin: 0 auto 15px auto !important;
173
- display: flex !important;
174
- align-items: center !important;
175
- justify-content: center !important;
176
- font-size: 50px !important;
177
- }
178
- .lane-title {
179
- font-size: 22px !important;
180
- font-weight: bold !important;
181
- margin-bottom: 2px !important;
182
- }
183
- .lane-tag {
184
- background-color: #ffffff !important;
185
- color: #065f51 !important;
186
- font-size: 12px !important;
187
- font-weight: bold !important;
188
- padding: 2px 6px !important;
189
- border-radius: 4px !important;
190
- margin-left: 5px !important;
191
- vertical-align: middle !important;
192
- }
193
- .lane-subtitle {
194
- font-style: italic !important;
195
- color: #cbd5e0 !important;
196
- font-size: 14px !important;
197
- margin-bottom: 15px !important;
198
- }
199
- .lane-divider {
200
- border-top: 1px solid rgba(255, 255, 255, 0.2) !important;
201
- margin: 15px 0 !important;
202
- }
203
- .lane-meta {
204
- text-align: left !important;
205
- font-size: 13px !important;
206
- color: #e2e8f0 !important;
207
- line-height: 1.6 !important;
208
- }
209
- .send-btn {
210
- background-color: #df5413 !important;
211
- color: #ffffff !important;
212
- font-weight: bold !important;
213
- border: none !important;
214
- }
215
- .send-btn:hover {
216
- background-color: #c6430b !important;
217
- }
218
- .suggestion-btn {
219
- background-color: #1e293b !important;
220
- color: #2b9388 !important;
221
- border: 1px solid #334155 !important;
222
- font-weight: 600 !important;
223
- border-radius: 6px !important;
224
- }
225
- .suggestion-btn:hover {
226
- background-color: #334155 !important;
227
- }
228
- .chatbot-container {
229
- border: 1px solid #232d42 !important;
230
- border-radius: 8px !important;
231
- background-color: #0f141c !important;
232
- }
233
- """
234
-
235
- # 6. Build Gradio Layout
236
- with gr.Blocks(title="Intelli_Agent_RAG") as demo:
237
-
238
- # Top Information Section
239
- with gr.Row(elem_classes="about-card"):
240
- with gr.Column(scale=4):
241
- gr.Markdown("### About Us", elem_classes="about-title")
242
- gr.Markdown(
243
- "Welcome to the Simplilearn Agentic Hub. This instance is a live, production-ready "
244
- "**Retrieval-Augmented Generation (RAG)** pipeline. It utilizes semantic vector mapping "
245
- "to answer course inquiries fluidly and dynamically without any pre-scripted static matching."
246
- )
247
- with gr.Column(scale=2, min_width=200):
248
- current_time_str = datetime.now().strftime("%dth %B %Y | time: %I:%M%p").lower()
249
- gr.HTML(f"<div class='time-stamp'>{current_time_str}</div>")
250
- gr.HTML("<div style='text-align: right; margin-top: 10px;'><button class='mode-btn'>☀️ 🌙 -> Change Mode (Day / Night)</button></div>")
251
-
252
- # Interactive Workspace Main Interface
253
- with gr.Row():
254
- # Chat Workspace Column
255
- with gr.Column(scale=3):
256
- chatbot = gr.Chatbot(
257
- value=[{"role": "assistant", "content": "Hello! I am Lane, your RAG-powered Support Agent. Ask me anything about Simplilearn's course pathways, certificates, or lab refund configurations."}],
258
- elem_classes="chatbot-container",
259
- height=420,
260
- show_label=False,
261
- )
262
-
263
- with gr.Row():
264
- msg_input = gr.Textbox(
265
- show_label=False,
266
- placeholder="Type your question here (e.g., 'What happens if I cannot pass the capstone project?')...",
267
- container=False,
268
- scale=5
269
- )
270
- send_button = gr.Button("Send", variant="primary", elem_classes="send-btn", scale=1)
271
-
272
- # Profile Meta Information Status Column
273
- with gr.Column(scale=1, min_width=280):
274
- with gr.Group(elem_classes="lane-card"):
275
- gr.HTML("<div class='lane-avatar'>🤖</div>")
276
- gr.HTML("<div class='lane-title'>Lane <span class='lane-tag'>RAG</span></div>")
277
- gr.HTML("<div class='lane-subtitle'>Intelligent Context-Aware Assistant</div>")
278
- gr.HTML("<div class='lane-divider'></div>")
279
- gr.HTML(
280
- "<div class='lane-meta'>"
281
- "🌐 <b>Vector Index Status: Active</b><br>"
282
- "Embedding Engine: <code>MiniLM-L6-V2</code><br>"
283
- "Cloud LLM Node: <code>Qwen-2.5-7B</code><br>"
284
- "Processing Speed: <code>~35 tokens/sec</code>"
285
- "</div>"
286
- )
287
-
288
- # Direct Action Suggestion Footer Row
289
- with gr.Row():
290
- refund_btn = gr.Button("What is the course refund policy?", elem_classes="suggestion-btn")
291
- cert_btn = gr.Button("How do I unlock my completion certificate?", elem_classes="suggestion-btn")
292
-
293
- # 7. Bind Operational Execution Events
294
- send_button.click(
295
- fn=chat_respond,
296
- inputs=[msg_input, chatbot],
297
- outputs=[msg_input, chatbot]
298
- )
299
- msg_input.submit(
300
- fn=chat_respond,
301
- inputs=[msg_input, chatbot],
302
- outputs=[msg_input, chatbot]
303
- )
304
-
305
- # Connect suggest button actions cleanly to the RAG stream function
306
- refund_btn.click(
307
- fn=lambda history: chat_respond("What is the course refund policy?", history),
308
- inputs=[chatbot],
309
- outputs=[msg_input, chatbot]
310
- )
311
- cert_btn.click(
312
- fn=lambda history: chat_respond("How do I unlock my completion certificate?", history),
313
- inputs=[chatbot],
314
- outputs=[msg_input, chatbot]
315
- )
316
 
317
- # 8. Start Execution Engine (css configuration passed directly here to ensure Gradio 6.0 compatibility)
318
  if __name__ == "__main__":
319
- demo.queue().launch(css=custom_css)
 
 
1
  import gradio as gr
 
 
2
  from huggingface_hub import InferenceClient
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ # Initialize the Hugging Face Inference Client
5
+ # You can replace this model with any other text generation model hosted on Hugging Face
6
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
7
+
8
+ # Your custom AI FAQ Agent Knowledge Base context
9
+ FAQ_CONTEXT = """
10
+ An AI FAQ Agent can be adapted for almost any website where users have questions, need guidance, or require support. If people visit your site and need to find information, an FAQ agent can help. Here is a breakdown of the most common types of websites that benefit from an FAQ Agent, categorized by industry:
11
+
12
+ 1. E-Commerce & Retail
13
+ This is the most popular use case. Customers frequently have the same questions before and after buying.
14
+ - What it answers: Shipping times, return policies, order tracking, product sizing, and payment methods.
15
+ - Examples: Online clothing stores, gadget shops, dropshipping sites.
16
+
17
+ 2. Service-Based & Agency Websites
18
+ If your business provides a service, prospective clients usually need to understand your process, pricing, and capabilities before booking.
19
+ - What it answers: Project timelines, custom quotes, what’s included in a package, and software requirements.
20
+ - Examples: Marketing agencies, consulting firms, web design agencies, photography studios.
21
+
22
+ 3. Software as a Service (SaaS) & Tech Products
23
+ Tech users need quick answers to avoid churn. An FAQ agent can act as a 24/7 first-line technical support rep.
24
+ - What it answers: Subscription tiers, API documentation, integrations, troubleshooting common errors, and feature requests.
25
+ - Examples: Project management tools, CRM software, mobile app landing pages.
26
+
27
+ 4. Educational & E-Learning Platforms
28
+ Students, parents, and educators frequently need help navigating course structures, enrollment, and technical requirements.
29
+
30
+ 5. Healthcare & Medical Websites
31
+ Filters basic inquiries before booking an office appointment.
32
+ - What it answers: Insurance accepted, clinic hours, prescription refill policies, and parking information.
33
+ - Note: Always includes a disclaimer that it does not provide medical advice.
34
+ - Examples: Dental clinics, therapy practices, local medical centers.
35
+
36
+ 6. Hospitality, Travel & Events
37
+ Travelers and event attendees are always looking for specific logistical details.
38
+ - What it answers: Check-in/check-out times, hotel amenities, event agendas, venue rules, and ticket refund policies.
39
+ - Examples: Hotels, Airbnb listings, music festivals, business conferences.
40
+ """
41
 
42
+ def extract_text(content):
 
43
  """
44
+ Safely extracts plain string text from any Gradio message structure,
45
+ handling strings, dictionaries, or custom Gradio ChatMessage objects.
46
  """
47
+ if isinstance(content, str):
48
+ return content
49
+ if isinstance(content, dict):
50
+ if "text" in content:
51
+ return content["text"]
52
+ if "content" in content:
53
+ return content["content"]
54
+ if hasattr(content, "text") and isinstance(content.text, str):
55
+ return content.text
56
+ if hasattr(content, "content") and isinstance(content.content, str):
57
+ return content.content
58
+ return str(content)
59
+
60
+ def respond(
61
+ message,
62
+ history,
63
+ system_message,
64
+ max_tokens,
65
+ temperature,
66
+ top_p,
67
+ ):
68
+ # Inject your AI FAQ knowledge base directly into the active system message
69
+ full_system_prompt = (
70
+ f"{system_message}\n\n"
71
+ f"Use the following FAQ reference context to answer questions accurately:\n"
72
+ f"{FAQ_CONTEXT}"
73
  )
74
 
75
+ messages = [{"role": "system", "content": full_system_prompt}]
76
+
77
+ # 1. Cleanse and parse conversation history safely
78
+ if history:
79
+ for val in history:
80
+ # Safe parsing for Gradio 5/6 ChatMessage objects
81
+ if hasattr(val, "role") and hasattr(val, "content"):
82
+ role = val.role
83
+ content_str = extract_text(val.content)
84
+ if role and content_str:
85
+ messages.append({"role": role, "content": content_str})
86
+ # Safe parsing for native dictionaries
87
+ elif isinstance(val, dict) and "role" in val:
88
+ role = val.get("role")
89
+ content_str = extract_text(val.get("content") or val.get("text"))
90
+ if role and content_str:
91
+ messages.append({"role": role, "content": content_str})
92
+ # Safe parsing for older Gradio tuple logs (user, assistant)
93
+ elif isinstance(val, (list, tuple)) and len(val) >= 2:
94
+ if val[0]:
95
+ messages.append({"role": "user", "content": extract_text(val[0])})
96
+ if val[1]:
97
+ messages.append({"role": "assistant", "content": extract_text(val[1])})
98
+
99
+ # 2. Cleanse and parse current incoming user message
100
+ user_content_str = extract_text(message)
101
+ messages.append({"role": "user", "content": user_content_str})
102
+
103
+ response = ""
104
+
105
+ # 3. Stream responses back reliably from the Inference API
 
 
 
 
 
106
  try:
107
+ for message_chunk in client.chat_completion(
108
+ messages,
109
+ max_tokens=max_tokens,
110
+ stream=True,
111
+ temperature=temperature,
112
+ top_p=top_p,
113
+ ):
114
+ token = message_chunk.choices[0].delta.content
115
+ if token is not None:
116
+ response += token
117
+ yield response
 
 
 
 
 
 
 
 
 
 
118
  except Exception as e:
119
+ yield f"An error occurred while connecting to the model API: {str(e)}"
120
+
121
+ # Setup the customized ChatInterface layout
122
+ demo = gr.ChatInterface(
123
+ respond,
124
+ additional_inputs=[
125
+ gr.Textbox(
126
+ value="You are an expert AI FAQ Agent assistant. Help users understand how websites use FAQ agents across various industries.",
127
+ label="System message"
128
+ ),
129
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
130
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
131
+ gr.Slider(
132
+ minimum=0.1,
133
+ maximum=1.0,
134
+ value=0.95,
135
+ step=0.05,
136
+ label="Top-p (nucleus sampling)",
137
+ ),
138
+ ],
139
+ title="AI FAQ Agent",
140
+ description="Ask queries about tailoring automated customer guidance bots to e-commerce, SaaS, healthcare, and event sites.",
141
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
 
 
143
  if __name__ == "__main__":
144
+ demo.launch()