rahul7star commited on
Commit
53fa4f7
Β·
verified Β·
1 Parent(s): 011c158

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -12
app.py CHANGED
@@ -158,40 +158,49 @@ def generate_response(user_input, history, mode="chat"):
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)
181
  except Exception as e:
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(
@@ -239,30 +248,32 @@ def build_ui():
239
  ) as demo:
240
 
241
 
242
- # Chatbot (new format)
 
243
  chatbot = gr.Chatbot(
 
244
  height=520,
245
  elem_id="ohamlab",
246
- type="messages", # βœ… modern format
247
- avatar_images=("πŸ§‘β€πŸ’»", "πŸ€–"),
248
  )
249
 
250
  # Input box (full width)
251
  with gr.Row():
252
  msg = gr.Textbox(
253
- placeholder="Type your message here...",
254
  lines=3,
255
  scale=12,
256
  show_label=False,
257
  container=False,
258
  )
259
 
260
- # Buttons (Send + Clear aligned below)
261
  with gr.Row(equal_height=True, variant="compact"):
262
  send = gr.Button("Send", variant="primary", elem_classes=["primary"])
263
  clear = gr.Button("Clear", variant="secondary", elem_classes=["secondary"])
264
 
265
- # Wiring
266
  send.click(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
267
  msg.submit(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
268
  clear.click(reset_chat, outputs=chatbot)
@@ -277,3 +288,4 @@ if __name__ == "__main__":
277
  print("πŸš€ Starting OhamLab Assistant...")
278
  demo = build_ui()
279
  demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
 
 
158
  # ---------------------------
159
  # 7. Gradio Chat UI
160
  # ---------------------------
161
+ import traceback
162
+ import gradio as gr
163
 
164
  # ---------------------------
165
+ # Chat Logic
166
  # ---------------------------
167
  def chat_with_model(user_message, chat_history):
168
+ """
169
+ Maintains full conversational context and returns updated chat history.
170
+ The assistant speaks as 'OhamLab'.
171
+ """
172
  if not user_message:
173
  return chat_history, ""
174
 
175
+ # Maintain persistent history in OpenAI-style structure
176
  history = [
177
  {"role": m["role"], "content": m["content"]}
178
  for m in chat_history
179
  if isinstance(m, dict) and "role" in m
180
  ]
181
 
182
+ # Append user query
183
  history.append({"role": "user", "content": user_message})
184
 
185
  try:
186
  bot_text = generate_response(user_message, history)
187
  except Exception as e:
188
  tb = traceback.format_exc()
189
+ bot_text = f"⚠️ OhamLab encountered an error:\n\n{e}\n\n{tb}"
190
+
191
+ # Append OhamLab reply to chat history
192
+ chat_history.append({"role": "assistant", "content": f"**OhamLab:** {bot_text}"})
193
 
 
194
  return chat_history, ""
195
 
196
 
197
  def reset_chat():
198
+ """Resets the chat session."""
199
  return []
200
 
201
 
202
  # ---------------------------
203
+ # Gradio Chat UI
204
  # ---------------------------
205
  def build_ui():
206
  with gr.Blocks(
 
248
  ) as demo:
249
 
250
 
251
+
252
+ # Chatbot (branded to OhamLab)
253
  chatbot = gr.Chatbot(
254
+ label="πŸ’  OhamLab Conversation", # βœ… replaces "Chatbot" label
255
  height=520,
256
  elem_id="ohamlab",
257
+ type="messages", # βœ… OpenAI-style roles
258
+ avatar_images=("πŸ§‘β€πŸ’»", "πŸ’ "), # user / OhamLab
259
  )
260
 
261
  # Input box (full width)
262
  with gr.Row():
263
  msg = gr.Textbox(
264
+ placeholder="Ask OhamLab anything about R&D, AI, or engineering...",
265
  lines=3,
266
  scale=12,
267
  show_label=False,
268
  container=False,
269
  )
270
 
271
+ # Buttons (Send + Clear)
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
+ # Wire actions
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)
 
288
  print("πŸš€ Starting OhamLab Assistant...")
289
  demo = build_ui()
290
  demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
291
+