VectorV17 commited on
Commit
7bbda4d
·
verified ·
1 Parent(s): e5f0bd4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -27
app.py CHANGED
@@ -90,36 +90,48 @@ def get_current_time_string():
90
  return now.strftime("%dth %B %Y | Time: %I:%M%p").lower()
91
 
92
  def chat_interface(user_message, history):
93
- if not user_message.strip():
94
  return "", history
95
 
96
- # Generate RAG response
97
- ai_response = rag_chain.invoke(user_message)
 
 
 
 
 
 
 
98
 
99
- # Append structured history dictionary mapping (Gradio 5/6 standard native format)
100
  history.append({"role": "user", "content": user_message})
101
  history.append({"role": "assistant", "content": ai_response})
102
 
 
103
  return "", history
104
 
105
  def handle_quick_reply(button_text, history):
 
106
  return chat_interface(button_text, history)
107
 
108
  # -------------------------------------------------------------------------
109
  # 4. Gradio Web Interface Layout
110
  # -------------------------------------------------------------------------
 
111
  custom_css = """
112
- .header-container { background-color: #f7f9fc; padding: 20px; border-radius: 8px; margin-bottom: 15px; }
113
- .meta-container { text-align: right; font-family: 'Georgia', serif; font-style: italic; color: #3b5998; }
114
- .sidebar-panel { background-color: #004d40; color: white; padding: 25px; border-radius: 8px; text-align: center; height: 100%; }
115
- .sidebar-panel img { border-radius: 50%; max-width: 150px; margin: 0 auto 15px auto; }
 
 
116
  .quick-btn { background-color: #e0f2f1 !important; border: 1px solid #b2dfdb !important; color: #004d40 !important; font-weight: 600; }
 
117
  """
118
 
119
- # Fixed: Removed 'css' parameter from constructor to satisfy Gradio 6 specifications
120
  with gr.Blocks(title="Simplilearn MOOC FAQ Agent") as demo:
121
 
122
- # TOP SECTION
123
  with gr.Row(elem_classes=["header-container"]):
124
  with gr.Column(scale=3):
125
  gr.Markdown("""
@@ -129,13 +141,13 @@ with gr.Blocks(title="Simplilearn MOOC FAQ Agent") as demo:
129
  learners unlock the full potential of specialized MOOC educational pathways.
130
  """)
131
  with gr.Column(scale=2, elem_classes=["meta-container"]):
132
- time_display = gr.Markdown(value=f"### {get_current_time_string()}")
 
133
  gr.Markdown("### ☀️ 🌙 ➔ Change Mode (Day / Night)")
134
 
135
- # MAIN ROW SECTION
136
  with gr.Row():
137
  with gr.Column(scale=4):
138
- # Fixed: Removed 'type="messages"' parameter since it's now default
139
  chatbot = gr.Chatbot(
140
  label="Lane AI Conversation",
141
  value=[{"role": "assistant", "content": "Hello! I'm Lane, your friendly AI Agent. How can I help you today?"}]
@@ -156,25 +168,40 @@ with gr.Blocks(title="Simplilearn MOOC FAQ Agent") as demo:
156
  # RIGHT SIDEBAR PANEL
157
  with gr.Column(scale=2, elem_classes=["sidebar-panel"]):
158
  gr.HTML("""
159
- <div style='margin-top: 20px;'>
160
- <img src='https://cdn-icons-png.flaticon.com/512/4712/4712010.png' alt='Lane Avatar' style='width:130px; height:130px; object-fit:cover; margin-bottom:15px; border: 4px solid #ffffff;'>
161
- <h2 style='color: white; margin-bottom: 5px;'>Lane <span style='background-color:#ffffff; color:#004d40; padding:2px 6px; border-radius:4px; font-size:14px;'>AI</span></h2>
162
- <p style='color: #e0f2f1; font-style: italic;'>Your Agentic AI Assistant</p>
163
- <hr style='border-color: rgba(255,255,255,0.2); margin: 20px 0;'>
164
- <p style='font-size: 14px; text-align: justify; color: #e0f2f1;'>
165
  Lane runs an optimized RAG vector-space framework pipeline. It targets relevant local enterprise facts instantly without remote workflow friction.
166
  </p>
167
  </div>
168
  """)
169
 
170
- # Event Connections
171
- submit_btn.click(chat_interface, inputs=[user_input, chatbot], outputs=[user_input, chatbot])
172
- user_input.submit(chat_interface, inputs=[user_input, chatbot], outputs=[user_input, chatbot])
 
 
 
 
 
 
 
 
173
 
174
- btn_refund.click(handle_quick_reply, inputs=[btn_refund, chatbot], outputs=[user_input, chatbot])
175
- btn_cert.click(handle_quick_reply, inputs=[btn_cert, chatbot], outputs=[user_input, chatbot])
176
-
177
- # Run configuration
 
 
 
 
 
 
 
 
178
  if __name__ == "__main__":
179
- # Fixed: Injected custom_css string here inside the launch method call to silence user warnings
180
  demo.launch(server_name="0.0.0.0", server_port=7860, css=custom_css)
 
90
  return now.strftime("%dth %B %Y | Time: %I:%M%p").lower()
91
 
92
  def chat_interface(user_message, history):
93
+ if not user_message or not user_message.strip():
94
  return "", history
95
 
96
+ # Initialize history list if empty
97
+ if history is None:
98
+ history = []
99
+
100
+ try:
101
+ # Generate RAG response safely
102
+ ai_response = rag_chain.invoke(user_message)
103
+ except Exception as e:
104
+ ai_response = f"Connect Error: Unable to fetch live context pipeline. Details: {str(e)}"
105
 
106
+ # Correctly append to state list
107
  history.append({"role": "user", "content": user_message})
108
  history.append({"role": "assistant", "content": ai_response})
109
 
110
+ # Always return pair matching the defined output interfaces [user_input, chatbot]
111
  return "", history
112
 
113
  def handle_quick_reply(button_text, history):
114
+ # Route text string immediately into processing frame
115
  return chat_interface(button_text, history)
116
 
117
  # -------------------------------------------------------------------------
118
  # 4. Gradio Web Interface Layout
119
  # -------------------------------------------------------------------------
120
+ # Force text color constraints (`color: #1f2937 !important;`) on header metadata to resist dark modes
121
  custom_css = """
122
+ .header-container { background-color: #f7f9fc !important; padding: 25px; border-radius: 12px; margin-bottom: 20px; border: 1px solid #e5e7eb; }
123
+ .header-container h1, .header-container p, .header-container h3 { color: #1f2937 !important; }
124
+ .meta-container { text-align: right; font-family: 'Georgia', serif; font-style: italic; color: #2563eb !important; }
125
+ .meta-container h3 { color: #2563eb !important; margin: 4px 0; }
126
+ .sidebar-panel { background-color: #004d40 !important; color: white !important; padding: 25px; border-radius: 12px; text-align: center; }
127
+ .sidebar-panel p, .sidebar-panel h2 { color: white !important; }
128
  .quick-btn { background-color: #e0f2f1 !important; border: 1px solid #b2dfdb !important; color: #004d40 !important; font-weight: 600; }
129
+ .quick-btn:hover { background-color: #b2dfdb !important; }
130
  """
131
 
 
132
  with gr.Blocks(title="Simplilearn MOOC FAQ Agent") as demo:
133
 
134
+ # TOP SECTION: Header banner split into Info Branding and Dynamic Meta Controls
135
  with gr.Row(elem_classes=["header-container"]):
136
  with gr.Column(scale=3):
137
  gr.Markdown("""
 
141
  learners unlock the full potential of specialized MOOC educational pathways.
142
  """)
143
  with gr.Column(scale=2, elem_classes=["meta-container"]):
144
+ # Display localized real-time clock indicator directly inside standard text markdown wrapper
145
+ gr.Markdown(f"### {get_current_time_string()}")
146
  gr.Markdown("### ☀️ 🌙 ➔ Change Mode (Day / Night)")
147
 
148
+ # MAIN WORKSPACE SECTION
149
  with gr.Row():
150
  with gr.Column(scale=4):
 
151
  chatbot = gr.Chatbot(
152
  label="Lane AI Conversation",
153
  value=[{"role": "assistant", "content": "Hello! I'm Lane, your friendly AI Agent. How can I help you today?"}]
 
168
  # RIGHT SIDEBAR PANEL
169
  with gr.Column(scale=2, elem_classes=["sidebar-panel"]):
170
  gr.HTML("""
171
+ <div style='margin-top: 10px;'>
172
+ <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%;'>
173
+ <h2 style='margin-bottom: 5px; font-size: 24px; font-weight: bold;'>Lane <span style='background-color:#ffffff; color:#004d40; padding:2px 8px; border-radius:4px; font-size:14px; vertical-align: middle;'>AI</span></h2>
174
+ <p style='font-style: italic; opacity: 0.9; margin-bottom: 15px;'>Your Agentic AI Assistant</p>
175
+ <hr style='border-color: rgba(255,255,255,0.2); margin: 15px 0;'>
176
+ <p style='font-size: 14px; text-align: justify; line-height: 1.5; opacity: 0.95;'>
177
  Lane runs an optimized RAG vector-space framework pipeline. It targets relevant local enterprise facts instantly without remote workflow friction.
178
  </p>
179
  </div>
180
  """)
181
 
182
+ # Event Wiring Architecture (Ensures clean pipeline state processing without UI freezing)
183
+ submit_btn.click(
184
+ fn=chat_interface,
185
+ inputs=[user_input, chatbot],
186
+ outputs=[user_input, chatbot]
187
+ )
188
+ user_input.submit(
189
+ fn=chat_interface,
190
+ inputs=[user_input, chatbot],
191
+ outputs=[user_input, chatbot]
192
+ )
193
 
194
+ btn_refund.click(
195
+ fn=handle_quick_reply,
196
+ inputs=[btn_refund, chatbot],
197
+ outputs=[user_input, chatbot]
198
+ )
199
+ btn_cert.click(
200
+ fn=handle_quick_reply,
201
+ inputs=[btn_cert, chatbot],
202
+ outputs=[user_input, chatbot]
203
+ )
204
+
205
+ # Run execution thread routing
206
  if __name__ == "__main__":
 
207
  demo.launch(server_name="0.0.0.0", server_port=7860, css=custom_css)