broadfield-dev commited on
Commit
9c45826
Β·
verified Β·
1 Parent(s): 4544171

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -123
app.py CHANGED
@@ -398,121 +398,50 @@ def ui_upload_memories_action_fn(uploaded_file_obj, progress=gr.Progress()):
398
  msg = f"Memories Upload: Processed {total_to_process}. Added: {added_count}, Format Errors: {format_error_count}, Save Errors: {save_error_count}."
399
  logger.info(msg); return msg
400
 
401
- # app.py
402
- # ... (all other Python code: imports, helper functions, logic functions remain the same) ...
 
 
 
 
 
 
 
 
403
 
404
- custom_theme = gr.themes.Base(primary_hue="teal", secondary_hue="purple", neutral_hue="zinc", text_size="md", spacing_size="md", radius_size="sm", font=["System UI", "sans-serif"])
405
  custom_css = """
406
- body { background: linear-gradient(to bottom right, #2c3e50, #34495e); color: #ecf0f1; min-height: 100vh; margin:0; padding:0; font-family: 'System UI', 'sans-serif';}
407
- .gradio-container { background: transparent !important; max-width: 100% !important; padding: 1rem !important; box-sizing: border-box;}
408
- #component-1 {gap:0!important;}
409
- /* Styling for gr.Group which replaces gr.Box */
410
- .gr-group {
411
- background-color: rgba(44, 62, 80, 0.8) !important; /* Matched to your .gr-box style */
412
- border: 1px solid rgba(189, 195, 199, 0.2) !important;
413
- border-radius: 8px !important;
414
- box-shadow: 0 2px 4px rgba(0,0,0,0.2);
415
- padding: 1em; /* Add padding inside the group */
416
- margin-bottom: 1rem; /* Space below groups */
417
- }
418
-
419
- .gr-panel, .gr-tabs, .gr-accordion { /* .gr-panel might be an older class, but keep if it targets something */
420
- background-color: rgba(44, 62, 80, 0.85) !important;
421
- border: 1px solid rgba(189, 195, 199, 0.2) !important;
422
- border-radius: 8px !important;
423
- box-shadow: 0 4px 6px rgba(0,0,0,0.1);
424
- }
425
- .gr-tabitem {
426
- background-color: rgba(52, 73, 94, 0.75) !important;
427
- border-radius: 6px !important;
428
- padding: 1em !important;
429
- border: 1px solid rgba(189, 195, 199, 0.1) !important;
430
- margin-bottom: 5px !important; /* Added from previous for consistency */
431
- }
432
- .gr-accordion {
433
- background-color: rgba(52, 73, 94, 0.7) !important;
434
- border-radius: 6px !important;
435
- border: 1px solid rgba(189, 195, 199, 0.15) !important;
436
- }
437
-
438
- .gr-textbox, .gr-dropdown, .gr-button, .gr-code, .gr-chat-message, .gr-json, .gr-file input[type="file"] {
439
- border: 1px solid rgba(189, 195, 199, 0.3) !important;
440
- background-color: rgba(52, 73, 94, 0.9) !important;
441
- color: #ecf0f1 !important;
442
- border-radius: 6px !important;
443
- }
444
- .gr-file {
445
- background-color: rgba(52, 73, 94, 0.9) !important;
446
- border-radius: 6px !important; padding: 0.5em;
447
- }
448
  .gr-file > .label-text { color: #bdc3c7 !important;}
449
  .gr-textarea textarea, .gr-textbox input { color: #ecf0f1 !important; }
450
-
451
  .gr-button.gr-button-primary { background-color: #1abc9c !important; color: white !important; border-color: #16a085 !important; }
452
  .gr-button.gr-button-secondary { background-color: #9b59b6 !important; color: white !important; border-color: #8e44ad !important; }
453
  .gr-button.gr-button-stop { background-color: #e74c3c !important; color: white !important; border-color: #c0392b !important; }
454
-
455
- .gr-markdown { padding: 5px; /* Reduced padding for markdown within styled boxes */ }
456
- .gr-markdown h1, .gr-markdown h2, .gr-markdown h3 {
457
- color: #1abc9c !important;
458
- border-bottom: 1px solid rgba(189, 195, 199, 0.2) !important;
459
- padding-bottom: 0.3em;
460
- margin-top:0.5em; margin-bottom: 0.5em;
461
- }
462
- .gr-markdown p, .gr-markdown li { color: #ecf0f1 !important; }
463
- .gr-markdown pre code {
464
- background-color: rgba(30, 40, 50, 0.95) !important;
465
- border: 1px solid rgba(189, 195, 199, 0.3) !important;
466
- color: #ecf0f1;
467
- border-radius: 4px; padding: 0.8em;
468
- }
469
-
470
- .gr-chatbot {
471
- background-color: rgba(44, 62, 80, 0.7) !important;
472
- border: 1px solid rgba(189, 195, 199, 0.2) !important;
473
- }
474
- .gr-chatbot .message {
475
- background-color: rgba(52, 73, 94, 0.9) !important;
476
- color: #ecf0f1 !important;
477
- border: 1px solid rgba(189, 195, 199, 0.2) !important;
478
- box-shadow: 0 1px 2px rgba(0,0,0,0.1) !important;
479
- }
480
- .gr-chatbot .message.user {
481
- background-color: rgba(46, 204, 113, 0.8) !important;
482
- color: #2c3e50 !important;
483
- }
484
-
485
- .gr-input-label > .label-text, .gr-dropdown-label > .label-text, .gr-checkbox-label > .label-text {
486
- color: #bdc3c7 !important;
487
- font-size: 0.9rem !important;
488
- }
489
  .gr-info { color: #bdc3c7 !important; font-size: 0.85rem !important; }
490
-
491
- .status-bar { /* Used with elem_classes="status-bar" on the gr.Row */
492
- background-color: rgba(44, 62, 80, 0.8) !important;
493
- padding: 8px;
494
- border-radius: 6px;
495
- margin-bottom: 1rem;
496
- }
497
-
498
- .tabnav button { /* Style for tab buttons */
499
- background-color: rgba(52, 73, 94, 0.8) !important;
500
- color: #ecf0f1 !important;
501
- border-bottom: 2px solid transparent !important;
502
- border-top-left-radius: 6px !important;
503
- border-top-right-radius: 6px !important;
504
- margin-right: 2px !important;
505
- }
506
- .tabnav button.selected {
507
- background-color: rgba(44, 62, 80, 0.95) !important;
508
- color: #1abc9c !important;
509
- border-bottom: 2px solid #1abc9c !important;
510
- }
511
  """
512
 
513
- with gr.Blocks(theme=custom_theme, title="AI Research Mega Agent v5.4") as demo:
 
514
 
515
- gr.Markdown("# πŸš€ AI Research Mega Agent Dashboard")
 
516
 
517
  avail_provs = get_available_providers()
518
  def_prov = avail_provs[0] if avail_provs else None
@@ -526,37 +455,37 @@ with gr.Blocks(theme=custom_theme, title="AI Research Mega Agent v5.4") as demo:
526
  with gr.Column(scale=2): gr.Textbox(label="SQLite Path", value=f"{MEMORY_SQLITE_PATH}", interactive=False)
527
  elif MEMORY_STORAGE_BACKEND == "HF_DATASET":
528
  with gr.Column(scale=2): gr.Textbox(label="HF Repos", value=f"M: {MEMORY_HF_MEM_REPO}, R: {MEMORY_HF_RULES_REPO}", interactive=False)
529
-
530
- with gr.Row(equal_height=False): # Main layout row for sidebar and tabs
531
- with gr.Column(scale=1, min_width=350): # Sidebar for configuration
532
  gr.Markdown("## βš™οΈ Configuration")
533
- with gr.Group(): # This gr.Group now acts like the old gr.Box
534
- gr.Markdown("### API & Model")
535
- api_key_tb = gr.Textbox(label="AI Provider API Key (Optional Override)", type="password", placeholder="Paste key here or set env var")
536
  prov_sel_dd = gr.Dropdown(label="AI Provider", choices=avail_provs, value=def_prov, interactive=True)
537
  model_sel_dd = gr.Dropdown(label="AI Model", choices=def_models, value=def_model_disp, interactive=True)
538
- with gr.Group(): # This gr.Group now acts like the old gr.Box
539
  gr.Markdown("### System Prompt")
540
- sys_prompt_tb = gr.Textbox(label="System Prompt Base", lines=10, value=DEFAULT_SYSTEM_PROMPT, interactive=True)
541
 
542
- with gr.Column(scale=3): # Main content area with Tabs
543
  with gr.Tabs():
544
- with gr.TabItem("πŸ’¬ AI Chat & Research"):
545
  gr.Markdown("## πŸ’¬ AI Chat Interface")
546
- main_chat_disp = gr.Chatbot(label="AI Research Chat", height=550, bubble_full_width=False, avatar_images=(None, "https://raw.githubusercontent.com/huggingface/brand-assets/main/hf-logo-with-title.png"), show_copy_button=True, render_markdown=True, sanitize_html=True)
547
  with gr.Row():
548
  user_msg_tb = gr.Textbox(show_label=False, placeholder="Ask your research question or give an instruction...", scale=7, lines=1, max_lines=5, autofocus=True)
549
  send_btn = gr.Button("Send", variant="primary", scale=1, min_width=100)
550
- with gr.Accordion("πŸ“ Full Response / Output Details", open=False):
551
  fmt_report_tb = gr.Textbox(label="Full AI Response", lines=10, interactive=True, show_copy_button=True, value="*AI responses will appear here...*")
552
- dl_report_btn = gr.DownloadButton("Download Report", interactive=False, visible=False)
553
  detect_out_md = gr.Markdown("*Insights used or other intermediate details will show here...*")
554
 
555
  with gr.TabItem("🧠 Knowledge Base Management"):
556
  gr.Markdown("## Manage Stored Knowledge")
557
  with gr.Row(equal_height=False):
558
  with gr.Column(scale=1):
559
- with gr.Group(): # Replaced gr.Box with gr.Group
560
  gr.Markdown("### πŸ“œ Rules (Learned Insights)")
561
  rules_disp_ta = gr.TextArea(label="View/Edit Rules", lines=15, interactive=True, placeholder="Load or type rules here (one per line or '---' separated for multiple).")
562
  with gr.Row():
@@ -568,15 +497,14 @@ with gr.Blocks(theme=custom_theme, title="AI Research Mega Agent v5.4") as demo:
568
  save_faiss_ram_btn_kb = gr.Button("Save FAISS Indices", visible=(MEMORY_STORAGE_BACKEND == "RAM"))
569
 
570
  with gr.Column(scale=1):
571
- with gr.Group(): # Replaced gr.Box with gr.Group
572
  gr.Markdown("### πŸ“š Memories (Past Interactions)")
573
- mems_disp_json = gr.JSON(label="View Memories (JSON format)") # Removed scale=2, as it's not a standard gr.JSON param
574
  with gr.Row(): view_mems_btn = gr.Button("πŸ”„ Load/Refresh Memories")
575
  upload_mems_fobj = gr.File(label="Upload Memories File (.jsonl)", file_types=[".jsonl"])
576
  mems_stat_tb = gr.Textbox(label="Memories Operation Status", interactive=False, lines=2, placeholder="Status...")
577
  clear_mems_btn = gr.Button("⚠️ Clear All Memories", variant="stop")
578
 
579
- # --- Event Handlers (remain the same as your last correct version) ---
580
  def dyn_upd_model_dd(sel_prov_dyn:str): models_dyn, def_model_dyn = get_model_display_names_for_provider(sel_prov_dyn), get_default_model_display_name_for_provider(sel_prov_dyn); return gr.Dropdown(choices=models_dyn, value=def_model_dyn, interactive=True)
581
  prov_sel_dd.change(fn=dyn_upd_model_dd, inputs=prov_sel_dd, outputs=model_sel_dd)
582
  chat_ins = [user_msg_tb, main_chat_disp, prov_sel_dd, model_sel_dd, api_key_tb, sys_prompt_tb]
@@ -622,9 +550,8 @@ with gr.Blocks(theme=custom_theme, title="AI Research Mega Agent v5.4") as demo:
622
 
623
  demo.load(fn=app_load_fn, inputs=None, outputs=[agent_stat_tb, rules_disp_ta, mems_disp_json])
624
 
625
-
626
  if __name__ == "__main__":
627
- logger.info(f"Starting Gradio AI Research Mega Agent (v5.4 - UI Fixes, Memory: {MEMORY_STORAGE_BACKEND})...")
628
  app_port, app_server = int(os.getenv("GRADIO_PORT", 7860)), os.getenv("GRADIO_SERVER_NAME", "127.0.0.1")
629
  app_debug, app_share = os.getenv("GRADIO_DEBUG", "False").lower()=="true", os.getenv("GRADIO_SHARE", "False").lower()=="true"
630
  logger.info(f"Launching Gradio server: http://{app_server}:{app_port}. Debug: {app_debug}, Share: {app_share}")
 
398
  msg = f"Memories Upload: Processed {total_to_process}. Added: {added_count}, Format Errors: {format_error_count}, Save Errors: {save_error_count}."
399
  logger.info(msg); return msg
400
 
401
+ # --- UI Definition ---
402
+ custom_theme = gr.themes.Base(
403
+ primary_hue="teal",
404
+ secondary_hue="purple",
405
+ neutral_hue="zinc",
406
+ text_size="sm",
407
+ spacing_size="md",
408
+ radius_size="sm",
409
+ font=[gr.themes.GoogleFont("Inter"), "System UI", "sans-serif"] # Added Inter as primary
410
+ )
411
 
 
412
  custom_css = """
413
+ body, html { margin: 0 !important; padding: 0 !important; height: 100%; background: linear-gradient(to bottom right, #2c3e50, #34495e); color: #ecf0f1; font-family: 'Inter', 'System UI', sans-serif;}
414
+ .gradio-container { background: transparent !important; max-width: 100% !important; padding: 1em !important; box-sizing: border-box; min-height: 100vh; display: flex; flex-direction: column;}
415
+ .gr-block.gr-group, .gr-tabs, .gr-accordion { background-color: rgba(44, 62, 80, 0.85) !important; border: 1px solid rgba(189, 195, 199, 0.2) !important; border-radius: 8px !important; box-shadow: 0 3px 5px rgba(0,0,0,0.15); margin-bottom: 1em;}
416
+ .gr-tabitem { background-color: rgba(52, 73, 94, 0.8) !important; border-radius: 6px !important; padding: 1em !important; border: 1px solid rgba(189, 195, 199, 0.1) !important;}
417
+ .gr-textbox, .gr-dropdown, .gr-button, .gr-code, .gr-chat-message, .gr-json, .gr-file input[type="file"], .gr-file . OgniCn { border: 1px solid rgba(189, 195, 199, 0.3) !important; background-color: rgba(52, 73, 94, 0.9) !important; color: #ecf0f1 !important; border-radius: 6px !important;}
418
+ .gr-file { background-color: transparent !important; border: none !important; padding:0 !important;} /* Make file upload cleaner */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
419
  .gr-file > .label-text { color: #bdc3c7 !important;}
420
  .gr-textarea textarea, .gr-textbox input { color: #ecf0f1 !important; }
 
421
  .gr-button.gr-button-primary { background-color: #1abc9c !important; color: white !important; border-color: #16a085 !important; }
422
  .gr-button.gr-button-secondary { background-color: #9b59b6 !important; color: white !important; border-color: #8e44ad !important; }
423
  .gr-button.gr-button-stop { background-color: #e74c3c !important; color: white !important; border-color: #c0392b !important; }
424
+ .gr-markdown { padding: 0px; }
425
+ .gr-markdown h1, .gr-markdown h2, .gr-markdown h3 { color: #1abc9c !important; border-bottom: 1px solid rgba(189, 195, 199, 0.2) !important; padding-bottom: 0.3em; margin-top:0.8em; margin-bottom: 0.6em; }
426
+ .gr-markdown p, .gr-markdown li { color: #ecf0f1 !important; line-height:1.6;}
427
+ .gr-markdown pre code { background-color: rgba(30, 40, 50, 0.95) !important; border: 1px solid rgba(189, 195, 199, 0.3) !important; color: #ecf0f1; border-radius: 4px; padding: 0.8em; }
428
+ .gr-chatbot { background-color: rgba(44, 62, 80, 0.7) !important; border: 1px solid rgba(189, 195, 199, 0.2) !important; }
429
+ .gr-chatbot .message { background-color: rgba(52, 73, 94, 0.9) !important; color: #ecf0f1 !important; border: 1px solid rgba(189, 195, 199, 0.2) !important; box-shadow: 0 1px 2px rgba(0,0,0,0.1) !important; }
430
+ .gr-chatbot .message.user { background-color: rgba(46, 204, 113, 0.8) !important; color: #2c3e50 !important; }
431
+ .gr-input-label > .label-text, .gr-dropdown-label > .label-text, .gr-checkbox-label > .label-text { color: #bdc3c7 !important; font-size: 0.9rem !important; margin-bottom: 0.2rem !important; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
432
  .gr-info { color: #bdc3c7 !important; font-size: 0.85rem !important; }
433
+ .status-bar { background-color: rgba(44, 62, 80, 0.8) !important; padding: 0.5rem 1rem !important; border-radius: 6px; margin-bottom: 1rem; }
434
+ .tabnav button { background-color: rgba(52, 73, 94, 0.8) !important; color: #ecf0f1 !important; border-bottom: 2px solid transparent !important; border-top-left-radius: 6px !important; border-top-right-radius: 6px !important; margin-right: 2px !important; padding: 0.5em 1em !important; }
435
+ .tabnav button.selected { background-color: rgba(44, 62, 80, 0.95) !important; color: #1abc9c !important; border-bottom: 2px solid #1abc9c !important;}
436
+ .main-title { text-align: center; margin-bottom: 0.5rem !important; }
437
+ .main-subtitle { text-align: center; color: #bdc3c7; font-size: 0.95rem; margin-bottom: 1.5rem !important; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
438
  """
439
 
440
+ with gr.Blocks(theme=custom_theme, css=custom_css, title="AI Research Agent") as demo:
441
+ gr.HTML("<style>" + custom_css + "</style>") # Ensure CSS is applied
442
 
443
+ gr.Markdown("# πŸ€– AI Research Agent", elem_classes="main-title")
444
+ gr.Markdown("Configure AI, chat for research, and manage its knowledge base.", elem_classes="main-subtitle")
445
 
446
  avail_provs = get_available_providers()
447
  def_prov = avail_provs[0] if avail_provs else None
 
455
  with gr.Column(scale=2): gr.Textbox(label="SQLite Path", value=f"{MEMORY_SQLITE_PATH}", interactive=False)
456
  elif MEMORY_STORAGE_BACKEND == "HF_DATASET":
457
  with gr.Column(scale=2): gr.Textbox(label="HF Repos", value=f"M: {MEMORY_HF_MEM_REPO}, R: {MEMORY_HF_RULES_REPO}", interactive=False)
458
+
459
+ with gr.Row(equal_height=False):
460
+ with gr.Column(scale=1, min_width=360): # Sidebar for configuration
461
  gr.Markdown("## βš™οΈ Configuration")
462
+ with gr.Group():
463
+ gr.Markdown("### AI Model Settings")
464
+ api_key_tb = gr.Textbox(label="AI Provider API Key (Optional Override)", type="password", placeholder="Paste key or use .env")
465
  prov_sel_dd = gr.Dropdown(label="AI Provider", choices=avail_provs, value=def_prov, interactive=True)
466
  model_sel_dd = gr.Dropdown(label="AI Model", choices=def_models, value=def_model_disp, interactive=True)
467
+ with gr.Group():
468
  gr.Markdown("### System Prompt")
469
+ sys_prompt_tb = gr.Textbox(label="System Prompt Base", lines=12, value=DEFAULT_SYSTEM_PROMPT, interactive=True)
470
 
471
+ with gr.Column(scale=2): # Main content area with Tabs
472
  with gr.Tabs():
473
+ with gr.TabItem("πŸ’¬ AI Chat & Research Output"):
474
  gr.Markdown("## πŸ’¬ AI Chat Interface")
475
+ main_chat_disp = gr.Chatbot(label="AI Research Chat", height=500, bubble_full_width=False, avatar_images=(None, "https://raw.githubusercontent.com/huggingface/brand-assets/main/hf-logo-with-title.png"), show_copy_button=True, render_markdown=True, sanitize_html=True)
476
  with gr.Row():
477
  user_msg_tb = gr.Textbox(show_label=False, placeholder="Ask your research question or give an instruction...", scale=7, lines=1, max_lines=5, autofocus=True)
478
  send_btn = gr.Button("Send", variant="primary", scale=1, min_width=100)
479
+ with gr.Accordion("πŸ“ Full Response / Output Details", open=True): # Open by default
480
  fmt_report_tb = gr.Textbox(label="Full AI Response", lines=10, interactive=True, show_copy_button=True, value="*AI responses will appear here...*")
481
+ dl_report_btn = gr.DownloadButton(value="Download Report", interactive=False, visible=False)
482
  detect_out_md = gr.Markdown("*Insights used or other intermediate details will show here...*")
483
 
484
  with gr.TabItem("🧠 Knowledge Base Management"):
485
  gr.Markdown("## Manage Stored Knowledge")
486
  with gr.Row(equal_height=False):
487
  with gr.Column(scale=1):
488
+ with gr.Group():
489
  gr.Markdown("### πŸ“œ Rules (Learned Insights)")
490
  rules_disp_ta = gr.TextArea(label="View/Edit Rules", lines=15, interactive=True, placeholder="Load or type rules here (one per line or '---' separated for multiple).")
491
  with gr.Row():
 
497
  save_faiss_ram_btn_kb = gr.Button("Save FAISS Indices", visible=(MEMORY_STORAGE_BACKEND == "RAM"))
498
 
499
  with gr.Column(scale=1):
500
+ with gr.Group():
501
  gr.Markdown("### πŸ“š Memories (Past Interactions)")
502
+ mems_disp_json = gr.JSON(label="View Memories (JSON format)")
503
  with gr.Row(): view_mems_btn = gr.Button("πŸ”„ Load/Refresh Memories")
504
  upload_mems_fobj = gr.File(label="Upload Memories File (.jsonl)", file_types=[".jsonl"])
505
  mems_stat_tb = gr.Textbox(label="Memories Operation Status", interactive=False, lines=2, placeholder="Status...")
506
  clear_mems_btn = gr.Button("⚠️ Clear All Memories", variant="stop")
507
 
 
508
  def dyn_upd_model_dd(sel_prov_dyn:str): models_dyn, def_model_dyn = get_model_display_names_for_provider(sel_prov_dyn), get_default_model_display_name_for_provider(sel_prov_dyn); return gr.Dropdown(choices=models_dyn, value=def_model_dyn, interactive=True)
509
  prov_sel_dd.change(fn=dyn_upd_model_dd, inputs=prov_sel_dd, outputs=model_sel_dd)
510
  chat_ins = [user_msg_tb, main_chat_disp, prov_sel_dd, model_sel_dd, api_key_tb, sys_prompt_tb]
 
550
 
551
  demo.load(fn=app_load_fn, inputs=None, outputs=[agent_stat_tb, rules_disp_ta, mems_disp_json])
552
 
 
553
  if __name__ == "__main__":
554
+ logger.info(f"Starting Gradio AI Research Mega Agent (v5.5 - UI Style Update, Memory: {MEMORY_STORAGE_BACKEND})...")
555
  app_port, app_server = int(os.getenv("GRADIO_PORT", 7860)), os.getenv("GRADIO_SERVER_NAME", "127.0.0.1")
556
  app_debug, app_share = os.getenv("GRADIO_DEBUG", "False").lower()=="true", os.getenv("GRADIO_SHARE", "False").lower()=="true"
557
  logger.info(f"Launching Gradio server: http://{app_server}:{app_port}. Debug: {app_debug}, Share: {app_share}")