rahul7star commited on
Commit
9618fee
Β·
verified Β·
1 Parent(s): 2d56f37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -11
app.py CHANGED
@@ -158,13 +158,23 @@ def generate_response(user_input, history, mode="chat"):
158
  # ---------------------------
159
  # 7. Gradio Chat UI
160
  # ---------------------------
 
 
 
 
 
161
  def chat_with_model(user_message, chat_history):
162
  if not user_message:
163
  return chat_history, ""
164
 
165
- # Maintain chat history for coherence
166
- history = [{"role": "assistant" if i % 2 else "user", "content": msg}
167
- for i, (msg, _) in enumerate(chat_history)]
 
 
 
 
 
168
 
169
  try:
170
  bot_text = generate_response(user_message, history)
@@ -172,14 +182,17 @@ def chat_with_model(user_message, chat_history):
172
  tb = traceback.format_exc()
173
  bot_text = f"⚠️ Error: {e}\n\n{tb}"
174
 
175
- chat_history.append((user_message, bot_text))
176
  return chat_history, ""
177
 
 
178
  def reset_chat():
179
  return []
180
 
181
- import gradio as gr
182
 
 
 
 
183
  def build_ui():
184
  with gr.Blocks(
185
  theme=gr.themes.Soft(primary_hue="indigo"),
@@ -225,17 +238,27 @@ def build_ui():
225
  """
226
  ) as demo:
227
 
 
228
  gr.Markdown(
229
  """
230
  <div style="text-align:center; margin-bottom: 1rem;">
231
  <h2 style="font-weight:700; font-size:1.8rem;">πŸ’  OhamLab AI Assistant</h2>
232
- <p style="color:#6b7280; font-size:0.95rem;">Your intelligent R&D and engineering companion powered by contextual knowledge.</p>
 
 
233
  </div>
234
  """
235
  )
236
 
237
- chatbot = gr.Chatbot(height=520, elem_id="ohamlab", type="tuples")
 
 
 
 
 
 
238
 
 
239
  with gr.Row():
240
  msg = gr.Textbox(
241
  placeholder="Type your message here...",
@@ -245,20 +268,23 @@ def build_ui():
245
  container=False,
246
  )
247
 
248
- with gr.Row(equal_height=True):
 
249
  send = gr.Button("Send", variant="primary", elem_classes=["primary"])
250
  clear = gr.Button("Clear", variant="secondary", elem_classes=["secondary"])
251
 
252
- # Wire events
253
  send.click(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
254
  msg.submit(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
255
  clear.click(reset_chat, outputs=chatbot)
256
 
257
  return demo
258
 
 
259
  # ---------------------------
260
  # Entrypoint
261
  # ---------------------------
262
  if __name__ == "__main__":
263
- print("πŸš€ Starting OhamLab ")
264
- build_ui()
 
 
158
  # ---------------------------
159
  # 7. Gradio Chat UI
160
  # ---------------------------
161
+
162
+
163
+ # ---------------------------
164
+ # 7. Chat Logic
165
+ # ---------------------------
166
  def chat_with_model(user_message, chat_history):
167
  if not user_message:
168
  return chat_history, ""
169
 
170
+ # Convert Gradio's new message format to your history list
171
+ history = [
172
+ {"role": m["role"], "content": m["content"]}
173
+ for m in chat_history
174
+ if isinstance(m, dict) and "role" in m
175
+ ]
176
+
177
+ history.append({"role": "user", "content": user_message})
178
 
179
  try:
180
  bot_text = generate_response(user_message, history)
 
182
  tb = traceback.format_exc()
183
  bot_text = f"⚠️ Error: {e}\n\n{tb}"
184
 
185
+ chat_history.append({"role": "assistant", "content": bot_text})
186
  return chat_history, ""
187
 
188
+
189
  def reset_chat():
190
  return []
191
 
 
192
 
193
+ # ---------------------------
194
+ # 8. Gradio Chat UI
195
+ # ---------------------------
196
  def build_ui():
197
  with gr.Blocks(
198
  theme=gr.themes.Soft(primary_hue="indigo"),
 
238
  """
239
  ) as demo:
240
 
241
+ # Header
242
  gr.Markdown(
243
  """
244
  <div style="text-align:center; margin-bottom: 1rem;">
245
  <h2 style="font-weight:700; font-size:1.8rem;">πŸ’  OhamLab AI Assistant</h2>
246
+ <p style="color:#6b7280; font-size:0.95rem;">
247
+ Your intelligent R&D and engineering companion β€” powered by OhamLab contextual knowledge.
248
+ </p>
249
  </div>
250
  """
251
  )
252
 
253
+ # Chatbot (new format)
254
+ chatbot = gr.Chatbot(
255
+ height=520,
256
+ elem_id="ohamlab",
257
+ type="messages", # βœ… modern format
258
+ avatar_images=("πŸ§‘β€πŸ’»", "πŸ€–"),
259
+ )
260
 
261
+ # Input box (full width)
262
  with gr.Row():
263
  msg = gr.Textbox(
264
  placeholder="Type your message here...",
 
268
  container=False,
269
  )
270
 
271
+ # Buttons (Send + Clear aligned below)
272
+ with gr.Row(equal_height=True, variant="compact"):
273
  send = gr.Button("Send", variant="primary", elem_classes=["primary"])
274
  clear = gr.Button("Clear", variant="secondary", elem_classes=["secondary"])
275
 
276
+ # Wiring
277
  send.click(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
278
  msg.submit(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
279
  clear.click(reset_chat, outputs=chatbot)
280
 
281
  return demo
282
 
283
+
284
  # ---------------------------
285
  # Entrypoint
286
  # ---------------------------
287
  if __name__ == "__main__":
288
+ print("πŸš€ Starting OhamLab Assistant...")
289
+ demo = build_ui()
290
+ demo.launch(server_name="0.0.0.0", server_port=7860, share=False)