GabrielSalem commited on
Commit
ed8d6b0
·
verified ·
1 Parent(s): c0bf892

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -7
app.py CHANGED
@@ -236,7 +236,7 @@ def continue_chat(chat_history, user_message, analysis_text):
236
  def build_demo():
237
  with gr.Blocks(title="AURA Chat — Hedge Fund Picks") as demo:
238
 
239
- # Inject custom CSS in a totally safe, backward-compatible way
240
  gr.HTML("""
241
  <style>
242
  .gradio-container { max-width: 1100px; margin: 18px auto; }
@@ -280,29 +280,57 @@ def build_demo():
280
  send_btn = gr.Button("Send")
281
 
282
  # States
283
- analysis_state = gr.State("")
284
- chat_state = gr.State([])
285
-
286
- # Analyze click
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287
  analyze_btn.click(
288
  fn=on_analyze,
289
  inputs=[prompts],
290
  outputs=[analysis_out, error_box, analysis_state, chat_state]
291
  )
292
 
293
- # Chat send
294
  send_btn.click(
295
  fn=on_send,
296
  inputs=[chat_state, user_input, analysis_state],
297
  outputs=[chat_state, user_input]
298
  )
 
 
299
  user_input.submit(
300
  fn=on_send,
301
  inputs=[chat_state, user_input, analysis_state],
302
  outputs=[chat_state, user_input]
303
  )
304
 
305
- # Render chat
306
  chat_state.change(
307
  fn=render_chat,
308
  inputs=[chat_state],
@@ -312,6 +340,7 @@ def build_demo():
312
  return demo
313
 
314
 
 
315
  # -----------------------
316
  # Clean shutdown helper
317
  # -----------------------
 
236
  def build_demo():
237
  with gr.Blocks(title="AURA Chat — Hedge Fund Picks") as demo:
238
 
239
+ # Inject custom CSS safely
240
  gr.HTML("""
241
  <style>
242
  .gradio-container { max-width: 1100px; margin: 18px auto; }
 
280
  send_btn = gr.Button("Send")
281
 
282
  # States
283
+ analysis_state = gr.State("") # holds the analysis text
284
+ chat_state = gr.State([]) # holds list of (user, assistant) tuples
285
+
286
+ # ---- Handler functions (must be defined before wiring) ----
287
+ def on_analyze(prompts_text):
288
+ """
289
+ Called when user clicks Analyze.
290
+ Returns: analysis_out (string), error_box (gr.Markdown value), analysis_state (string), chat_state (list)
291
+ """
292
+ analysis_text, initial_chat = analyze_and_seed_chat(prompts_text)
293
+ if analysis_text.startswith("ERROR"):
294
+ # Show error in the error_box; leave other outputs unchanged
295
+ return "", f"**Error:** {analysis_text}", "", []
296
+ # Success: fill analysis_out, hide error box (empty string), set analysis_state and seed chat_state
297
+ return analysis_text, "", analysis_text, initial_chat
298
+
299
+ def on_send(chat_state_list, user_msg, analysis_text):
300
+ """
301
+ Called when user sends a chat message.
302
+ Returns: updated chat_state, cleared user_input
303
+ """
304
+ if not user_msg or not user_msg.strip():
305
+ return chat_state_list or [], ""
306
+ updated_history = continue_chat(chat_state_list or [], user_msg, analysis_text)
307
+ return updated_history, "" # second value clears the user_input box
308
+
309
+ def render_chat(chat_state_list):
310
+ """Render the chat_state into the Chatbot component."""
311
+ return chat_state_list or []
312
+
313
+ # ---- Wire handlers to UI components ----
314
  analyze_btn.click(
315
  fn=on_analyze,
316
  inputs=[prompts],
317
  outputs=[analysis_out, error_box, analysis_state, chat_state]
318
  )
319
 
 
320
  send_btn.click(
321
  fn=on_send,
322
  inputs=[chat_state, user_input, analysis_state],
323
  outputs=[chat_state, user_input]
324
  )
325
+
326
+ # Allow Enter-key submission from the textbox
327
  user_input.submit(
328
  fn=on_send,
329
  inputs=[chat_state, user_input, analysis_state],
330
  outputs=[chat_state, user_input]
331
  )
332
 
333
+ # Keep Chatbot display updated whenever chat_state changes
334
  chat_state.change(
335
  fn=render_chat,
336
  inputs=[chat_state],
 
340
  return demo
341
 
342
 
343
+
344
  # -----------------------
345
  # Clean shutdown helper
346
  # -----------------------