rahul7star commited on
Commit
ea63c73
·
verified ·
1 Parent(s): 5d5b7f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -64
app.py CHANGED
@@ -158,105 +158,158 @@ def generate_response(user_input, history, mode="chat"):
158
  # ---------------------------
159
  # 7. Gradio Chat UI
160
  # ---------------------------
 
161
  import gradio as gr
162
- from typing import List, Tuple
163
 
164
- # --- Core Chat Logic ---
165
- def chat_with_model(message: str, history: List[Tuple[str, str]]):
166
- if history is None:
167
- history = []
168
 
169
- system_message = "You are OhamLab AI, a professional assistant for research and engineering."
170
 
171
- # Build structured message history
172
- messages = [{"role": "system", "content": system_message}]
173
- for user_msg, bot_msg in history:
174
- if user_msg:
175
- messages.append({"role": "user", "content": user_msg})
176
- if bot_msg:
177
- messages.append({"role": "assistant", "content": bot_msg})
178
 
179
- # Add current user input
180
- messages.append({"role": "user", "content": message})
 
 
 
 
 
181
 
182
- # Placeholder: Replace with your model inference (RAG, etc.)
183
- bot_reply = f"OhamLab: {message[::-1]}" # simulate intelligent response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
 
185
- # Append to history (user msg, bot reply)
186
- history.append((message, bot_reply))
187
  return history, ""
188
 
189
 
190
  def reset_chat():
 
191
  return []
192
 
193
 
194
- # --- Professional OhamLab Chat UI ---
 
 
 
195
  def build_ui():
196
  with gr.Blocks(
197
- theme=gr.themes.Soft(),
198
  css="""
199
- /* Hide share/delete icons */
200
- #ohamlab .wrap.svelte-1lcyrj3 > div > div > button {
201
- display: none !important;
202
- }
203
-
204
- /* Chat alignment and bubble styling */
205
- #ohamlab .message.user {
206
- background-color: #0047AB !important;
207
- color: white !important;
208
- border-radius: 16px !important;
209
- align-self: flex-end !important;
210
- text-align: right !important;
211
- margin-left: 20%;
212
- }
213
-
214
- #ohamlab .message.bot {
215
- background-color: #f2f2f2 !important;
216
- color: #111 !important;
217
- border-radius: 16px !important;
218
- align-self: flex-start !important;
219
- text-align: left !important;
220
- margin-right: 20%;
221
- }
222
-
223
- /* General layout tweaks */
224
- .message-box {
225
- width: 100%;
226
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
  """,
228
  ) as demo:
 
229
  gr.Markdown(
230
  """
231
- <div style='text-align:center;'>
232
- <h2 style='color:#0047AB;'>🤖 OhamLab AI Assistant</h2>
233
- <p style='color:gray;'>Your trusted partner in advanced R&D and AI innovation.</p>
 
 
234
  </div>
235
  """
236
  )
237
 
 
238
  chatbot = gr.Chatbot(
239
- label="💠 OhamLabAI",
 
240
  elem_id="ohamlab",
241
-
242
- height=540,
243
- bubble_full_width=False,
244
- show_copy_button=False,
245
- avatar_images=[None, None],
246
  )
247
 
248
- with gr.Row(equal_height=False):
 
249
  msg = gr.Textbox(
250
- placeholder="Type your message here...",
251
  lines=3,
252
  show_label=False,
253
- elem_classes="message-box",
 
254
  )
255
 
256
- with gr.Row():
257
- send = gr.Button("Send", variant="primary")
258
- clear = gr.Button("Clear")
 
259
 
 
260
  send.click(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
261
  msg.submit(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
262
  clear.click(reset_chat, outputs=chatbot)
@@ -264,7 +317,11 @@ def build_ui():
264
  return demo
265
 
266
 
 
 
 
267
  if __name__ == "__main__":
 
268
  demo = build_ui()
269
  demo.launch(server_name="0.0.0.0", server_port=7860)
270
 
 
158
  # ---------------------------
159
  # 7. Gradio Chat UI
160
  # ---------------------------
161
+ import traceback
162
  import gradio as gr
 
163
 
164
+ # ---------------------------
165
+ # Chat Logic
166
+ # ---------------------------
 
167
 
 
168
 
 
 
 
 
 
 
 
169
 
170
+ def chat_with_model(user_message, chat_history):
171
+ """
172
+ Maintains full conversational context and returns updated chat history.
173
+ The assistant speaks as 'OhamLab'.
174
+ """
175
+ if not user_message:
176
+ return chat_history, ""
177
 
178
+ if chat_history is None:
179
+ chat_history = []
180
+
181
+ # Convert Gradio message list (dict-based) to usable context
182
+ history = [
183
+ {"role": m["role"], "content": m["content"]}
184
+ for m in chat_history
185
+ if isinstance(m, dict) and "role" in m
186
+ ]
187
+
188
+ # Append current user message
189
+ history.append({"role": "user", "content": user_message})
190
+
191
+ try:
192
+ bot_reply = generate_response(user_message, history)
193
+ except Exception as e:
194
+ tb = traceback.format_exc()
195
+ bot_reply = f"⚠️ OhamLab encountered an error:\n\n{e}\n\n{tb}"
196
+
197
+ # Add OhamLab's response as assistant role
198
+ history.append({"role": "assistant", "content": bot_reply})
199
 
 
 
200
  return history, ""
201
 
202
 
203
  def reset_chat():
204
+ """Resets the chat session."""
205
  return []
206
 
207
 
208
+ # ---------------------------
209
+ # Gradio Chat UI
210
+ # ---------------------------
211
+
212
  def build_ui():
213
  with gr.Blocks(
214
+ theme=gr.themes.Soft(primary_hue="indigo"),
215
  css="""
216
+ /* --- Hide share/delete icons --- */
217
+ #ohamlab .wrap.svelte-1lcyrj3 > div > div > button {
218
+ display: none !important;
219
+ }
220
+
221
+ /* --- User (Right) Message Bubble --- */
222
+ #ohamlab .message.user {
223
+ background-color: #4f46e5 !important;
224
+ color: white !important;
225
+ border-radius: 14px !important;
226
+ align-self: flex-end !important;
227
+ text-align: right !important;
228
+ margin-left: 25%;
229
+ }
230
+
231
+ /* --- OhamLab (Left) Message Bubble --- */
232
+ #ohamlab .message.assistant {
233
+ background-color: #f8f9fa !important;
234
+ color: #111 !important;
235
+ border-radius: 14px !important;
236
+ align-self: flex-start !important;
237
+ text-align: left !important;
238
+ margin-right: 25%;
239
+ }
240
+
241
+ /* --- Overall Container --- */
242
+ .gradio-container {
243
+ max-width: 900px !important;
244
+ margin: auto;
245
+ padding-top: 1.5rem;
246
+ }
247
+ textarea {
248
+ resize: none !important;
249
+ border-radius: 12px !important;
250
+ border: 1px solid #d1d5db !important;
251
+ box-shadow: 0 1px 3px rgba(0,0,0,0.08);
252
+ }
253
+ button.primary {
254
+ background-color: #4f46e5 !important;
255
+ color: white !important;
256
+ border-radius: 10px !important;
257
+ padding: 0.6rem 1.4rem !important;
258
+ font-weight: 600;
259
+ transition: all 0.2s ease-in-out;
260
+ }
261
+ button.primary:hover {
262
+ background-color: #4338ca !important;
263
+ }
264
+ button.secondary {
265
+ background-color: #f3f4f6 !important;
266
+ border-radius: 10px !important;
267
+ color: #374151 !important;
268
+ font-weight: 500;
269
+ transition: all 0.2s ease-in-out;
270
+ }
271
+ button.secondary:hover {
272
+ background-color: #e5e7eb !important;
273
+ }
274
  """,
275
  ) as demo:
276
+ # Header
277
  gr.Markdown(
278
  """
279
+ <div style="text-align:center; margin-bottom: 1rem;">
280
+ <h2 style="font-weight:700; font-size:1.8rem;">💠 OhamLab AI Assistant</h2>
281
+ <p style="color:#6b7280; font-size:0.95rem;">
282
+ Your intelligent R&D and engineering companion — powered by OhamLab contextual knowledge.
283
+ </p>
284
  </div>
285
  """
286
  )
287
 
288
+ # Chatbot area
289
  chatbot = gr.Chatbot(
290
+ label="💠 OhamLab Conversation",
291
+ height=520,
292
  elem_id="ohamlab",
293
+ type="messages",
294
+ avatar_images=("🧑‍💻", "💠"), # (user, bot)
 
 
 
295
  )
296
 
297
+ # Input box (full width)
298
+ with gr.Row():
299
  msg = gr.Textbox(
300
+ placeholder="Ask OhamLab anything about R&D, AI, or engineering...",
301
  lines=3,
302
  show_label=False,
303
+ scale=12,
304
+ container=False,
305
  )
306
 
307
+ # Buttons (Send + Clear)
308
+ with gr.Row(equal_height=True, variant="compact"):
309
+ send = gr.Button("Send", variant="primary", elem_classes=["primary"])
310
+ clear = gr.Button("Clear", variant="secondary", elem_classes=["secondary"])
311
 
312
+ # Wiring
313
  send.click(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
314
  msg.submit(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
315
  clear.click(reset_chat, outputs=chatbot)
 
317
  return demo
318
 
319
 
320
+ # ---------------------------
321
+ # Entrypoint
322
+ # ---------------------------
323
  if __name__ == "__main__":
324
+ print("🚀 Starting OhamLab Assistant...")
325
  demo = build_ui()
326
  demo.launch(server_name="0.0.0.0", server_port=7860)
327