rahul7star commited on
Commit
05d05b2
Β·
verified Β·
1 Parent(s): 53fa4f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -96
app.py CHANGED
@@ -164,128 +164,84 @@ import gradio as gr
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(
207
- theme=gr.themes.Soft(primary_hue="indigo"),
208
  css="""
209
- #ohamlab {
210
- height: 550px !important;
211
- border-radius: 14px;
212
- box-shadow: 0 2px 12px rgba(0,0,0,0.1);
213
- background: white;
214
- }
215
- .gradio-container {
216
- max-width: 900px !important;
217
- margin: auto;
218
- padding-top: 1.5rem;
219
- }
220
- textarea {
221
- resize: none !important;
222
- border-radius: 12px !important;
223
- border: 1px solid #d1d5db !important;
224
- box-shadow: 0 1px 3px rgba(0,0,0,0.08);
225
- }
226
- button.primary {
227
- background-color: #4f46e5 !important;
228
- color: white !important;
229
- border-radius: 10px !important;
230
- padding: 0.6rem 1.4rem !important;
231
- font-weight: 600;
232
- transition: all 0.2s ease-in-out;
233
- }
234
- button.primary:hover {
235
- background-color: #4338ca !important;
236
- }
237
- button.secondary {
238
- background-color: #f3f4f6 !important;
239
- border-radius: 10px !important;
240
- color: #374151 !important;
241
- font-weight: 500;
242
- transition: all 0.2s ease-in-out;
243
- }
244
- button.secondary:hover {
245
- background-color: #e5e7eb !important;
246
- }
247
- """
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)
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)
291
 
 
164
  # ---------------------------
165
  # Chat Logic
166
  # ---------------------------
167
+ import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
 
169
+ def chat_with_model(message, history):
170
+ if history is None:
171
+ history = []
172
+ # Simulate bot response
173
+ response = f"OhamLab says: {message[::-1]}" # replace with actual model call
174
+ history.append(("user", message))
175
+ history.append(("bot", response))
176
+ return history, ""
177
 
178
  def reset_chat():
 
179
  return []
180
 
 
 
 
 
181
  def build_ui():
182
  with gr.Blocks(
183
+ theme=gr.themes.Soft(),
184
  css="""
185
+ #ohamlab .message.user {
186
+ background-color: #e6f0ff !important;
187
+ border-radius: 14px !important;
188
+ color: #003366 !important;
189
+ }
190
+ #ohamlab .message.bot {
191
+ background-color: #f8f9fa !important;
192
+ border-radius: 14px !important;
193
+ color: #111 !important;
194
+ }
195
+ #ohamlab .chatbot .wrap.svelte-1lcyrj3 > div > div > button {
196
+ display: none !important; /* hide share/delete icons */
197
+ }
198
+ #ohamlab .bot-icon {
199
+ background: linear-gradient(90deg, #0047AB, #007FFF);
200
+ color: white;
201
+ border-radius: 50%;
202
+ padding: 6px 10px;
203
+ font-weight: 600;
204
+ font-size: 14px;
205
+ }
206
+ .message-box {
207
+ width: 100%;
208
+ }
209
+ """,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  ) as demo:
211
+
212
+ gr.Markdown(
213
+ "<h2 style='text-align:center; color:#0047AB;'>πŸ’¬ OhamLab AI Assistant</h2>"
214
+ "<p style='text-align:center; color:gray;'>Your professional research and engineering companion.</p>"
215
+ )
216
 
 
 
 
217
  chatbot = gr.Chatbot(
218
+ label=None,
219
+ height=540,
220
  elem_id="ohamlab",
221
+ bubble_full_width=False,
222
+ avatar_images=[None, None], # disable circular icons
223
  )
224
 
225
+ with gr.Row(equal_height=False):
 
226
  msg = gr.Textbox(
227
+ placeholder="Type your message here...",
228
  lines=3,
 
229
  show_label=False,
230
+ elem_classes="message-box",
231
  )
232
 
233
+ with gr.Row():
234
+ send = gr.Button("Send", variant="primary")
235
+ clear = gr.Button("Clear")
 
236
 
237
+ # event bindings
238
  send.click(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
239
  msg.submit(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, msg])
240
  clear.click(reset_chat, outputs=chatbot)
241
 
242
  return demo
243
 
 
 
 
 
244
  if __name__ == "__main__":
 
245
  demo = build_ui()
246
+ demo.launch(server_name="0.0.0.0", server_port=7860)
247