Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -43,11 +43,7 @@ DEFAULT_SYSTEM_PROMPT = os.getenv(
|
|
43 |
logger.info(f"App Config: WebSearch={WEB_SEARCH_ENABLED}, ToolDecisionProvider={TOOL_DECISION_PROVIDER_ENV}, ToolDecisionModelID={TOOL_DECISION_MODEL_ID_ENV}, MemoryBackend={MEMORY_STORAGE_BACKEND}")
|
44 |
|
45 |
# --- Helper Functions (format_insights_for_prompt, generate_interaction_metrics, etc.) ---
|
46 |
-
#
|
47 |
-
# process_user_interaction_gradio, deferred_learning_and_memory_task, handle_gradio_chat_submit,
|
48 |
-
# ui_view_rules_action_fn, ui_upload_rules_action_fn, ui_view_memories_action_fn, ui_upload_memories_action_fn)
|
49 |
-
# remain the same as in the previous "full working file".
|
50 |
-
# For brevity here, I will not repeat them fully. Ensure they are present in your actual app.py.
|
51 |
def format_insights_for_prompt(retrieved_insights_list: list[str]) -> tuple[str, list[dict]]:
|
52 |
if not retrieved_insights_list:
|
53 |
return "No specific guiding principles or learned insights retrieved.", []
|
@@ -395,7 +391,7 @@ def ui_upload_memories_action_fn(uploaded_file_obj, progress=gr.Progress()):
|
|
395 |
logger.info(msg); return msg
|
396 |
|
397 |
# --- UI Definition ---
|
398 |
-
# Using the CSS
|
399 |
custom_theme = gr.themes.Base(
|
400 |
primary_hue="teal",
|
401 |
secondary_hue="purple",
|
@@ -403,22 +399,22 @@ custom_theme = gr.themes.Base(
|
|
403 |
text_size="sm",
|
404 |
spacing_size="md",
|
405 |
radius_size="sm",
|
406 |
-
font=["System UI", "sans-serif"]
|
407 |
)
|
408 |
|
409 |
custom_css = """
|
410 |
-
body
|
411 |
-
|
412 |
-
.
|
413 |
.gr-block.gr-group, .gr-tabs, .gr-accordion { background-color: rgba(44, 62, 80, 0.8) !important; border: 1px solid rgba(189, 195, 199, 0.2) !important; border-radius: 8px !important; padding: 1em; margin-bottom: 1em;}
|
414 |
.gr-tabitem { background-color: rgba(52, 73, 94, 0.75) !important; border-radius: 6px !important; padding: 1em !important; border: 1px solid rgba(189, 195, 199, 0.1) !important;}
|
415 |
.gr-textbox, .gr-dropdown, .gr-button, .gr-code, .gr-chat-message, .gr-json, .gr-file input[type="file"], .gr-file button { border-color: rgba(189, 195, 199, 0.3) !important; background-color: rgba(52, 73, 94, 0.9) !important; color: #ecf0f1 !important; border-radius: 6px !important;}
|
416 |
.gr-textarea textarea, .gr-textbox input { color: #ecf0f1 !important; }
|
417 |
.gr-button.gr-button-primary { background-color: #1abc9c !important; color: white !important; border-color: #16a085 !important; }
|
418 |
.gr-button.gr-button-secondary { background-color: #9b59b6 !important; color: white !important; border-color: #8e44ad !important;}
|
419 |
-
.gr-button.gr-button-stop { background-color: #e74c3c !important; color: white !important; border-color: #c0392b !important;}
|
420 |
-
.gr-markdown { padding:
|
421 |
-
.gr-group .gr-markdown { padding: 0px; background-color: transparent !important; }
|
422 |
.gr-markdown h1, .gr-markdown h2, .gr-markdown h3 { color: #ecf0f1 !important; border-bottom-color: rgba(189, 195, 199, 0.3) !important; margin-top: 0.5em; margin-bottom: 0.5em;}
|
423 |
.gr-markdown h1 {font-size: 1.5rem;} .gr-markdown h2 {font-size: 1.25rem;} .gr-markdown h3 {font-size: 1.1rem;}
|
424 |
.gr-markdown p, .gr-markdown li { color: #ecf0f1 !important; }
|
@@ -428,80 +424,77 @@ body { background: linear-gradient(to bottom right, #2c3e50, #34495e); color: #e
|
|
428 |
.gr-chatbot .message.user { background-color: rgba(46, 204, 113, 0.9) !important; color: black !important; }
|
429 |
.gr-input-label > .label-text, .gr-dropdown-label > .label-text { color: #bdc3c7 !important; }
|
430 |
.status-bar { padding: 0.5rem 1rem; border-radius: 6px; margin-bottom: 1rem; background-color: rgba(44, 62, 80, 0.8) !important; }
|
431 |
-
.tabnav button { background-color: rgba(52, 73, 94, 0.8) !important; color: #ecf0f1 !important; border-bottom: 2px solid transparent !important;
|
432 |
.tabnav button.selected { background-color: rgba(44, 62, 80, 0.95) !important; color: #1abc9c !important; border-bottom: 2px solid #1abc9c !important;}
|
433 |
-
|
434 |
-
#app-subtitle { text-align: center; color: #bdc3c7; font-size: 0.9rem; margin-bottom: 1em;}
|
435 |
-
.sidebar { padding-right: 1em; } /* Add some spacing for sidebar */
|
436 |
-
.main-content { flex-grow: 1; display: flex; flex-direction: column; }
|
437 |
-
.chat-area { flex-grow: 1; display: flex; flex-direction: column; }
|
438 |
-
.chat-output-area { flex-grow: 1; }
|
439 |
"""
|
440 |
|
441 |
-
with gr.Blocks(theme=custom_theme, css=custom_css, title="AI Research Agent v5.
|
442 |
-
gr.
|
443 |
-
gr.Markdown("Configure settings, chat with AI for research, and manage its knowledge.", elem_id="app-subtitle")
|
444 |
-
|
445 |
-
avail_provs = get_available_providers()
|
446 |
-
def_prov = avail_provs[0] if avail_provs else None
|
447 |
-
def_models = get_model_display_names_for_provider(def_prov) if def_prov else []
|
448 |
-
def_model_disp = get_default_model_display_name_for_provider(def_prov) if def_prov else None
|
449 |
-
|
450 |
-
with gr.Row(elem_classes="status-bar"):
|
451 |
-
with gr.Column(scale=3): agent_stat_tb = gr.Textbox(label="Agent Status", interactive=False, value="Initializing...", lines=1)
|
452 |
-
with gr.Column(scale=1): memory_backend_info_tb = gr.Textbox(label="Memory Backend", value=f"{MEMORY_STORAGE_BACKEND}", interactive=False, lines=1)
|
453 |
-
# Conditional display for backend path info
|
454 |
-
sqlite_path_display = gr.Textbox(label="SQLite Path", value=f"{MEMORY_SQLITE_PATH}", interactive=False, visible=(MEMORY_STORAGE_BACKEND == "SQLITE"))
|
455 |
-
hf_repos_display = gr.Textbox(label="HF Repos", value=f"M: {MEMORY_HF_MEM_REPO}, R: {MEMORY_HF_RULES_REPO}", interactive=False, visible=(MEMORY_STORAGE_BACKEND == "HF_DATASET"))
|
456 |
|
457 |
-
with gr.
|
458 |
-
with gr.
|
459 |
-
gr.Markdown("
|
460 |
-
|
461 |
-
gr.Markdown("### API & Model Settings")
|
462 |
-
api_key_tb = gr.Textbox(label="AI Provider API Key (Override)", type="password", placeholder="Uses .env if blank")
|
463 |
-
prov_sel_dd = gr.Dropdown(label="AI Provider", choices=avail_provs, value=def_prov, interactive=True)
|
464 |
-
model_sel_dd = gr.Dropdown(label="AI Model", choices=def_models, value=def_model_disp, interactive=True)
|
465 |
-
with gr.Group():
|
466 |
-
gr.Markdown("### System Prompt")
|
467 |
-
sys_prompt_tb = gr.Textbox(label="System Prompt Base", lines=10, value=DEFAULT_SYSTEM_PROMPT, interactive=True)
|
468 |
-
if MEMORY_STORAGE_BACKEND == "RAM":
|
469 |
-
save_faiss_sidebar_btn = gr.Button("Save FAISS Indices", variant="secondary")
|
470 |
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
477 |
with gr.Row():
|
478 |
user_msg_tb = gr.Textbox(show_label=False, placeholder="Ask your research question...", scale=7, lines=1, max_lines=5, autofocus=True)
|
479 |
send_btn = gr.Button("Send", variant="primary", scale=1, min_width=100)
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
-
|
493 |
-
|
494 |
-
|
495 |
-
|
496 |
-
|
497 |
-
|
498 |
-
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
-
|
503 |
-
|
504 |
-
|
505 |
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)
|
506 |
prov_sel_dd.change(fn=dyn_upd_model_dd, inputs=prov_sel_dd, outputs=model_sel_dd)
|
507 |
chat_ins = [user_msg_tb, main_chat_disp, prov_sel_dd, model_sel_dd, api_key_tb, sys_prompt_tb]
|
@@ -538,11 +531,15 @@ with gr.Blocks(theme=custom_theme, css=custom_css, title="AI Research Agent v5.7
|
|
538 |
backend_status = f"AI Systems Initialized. Ready."
|
539 |
rules_on_load = ui_view_rules_action_fn()
|
540 |
mems_on_load = ui_view_memories_action_fn()
|
541 |
-
|
542 |
-
|
|
|
|
|
|
|
|
|
543 |
|
544 |
if __name__ == "__main__":
|
545 |
-
logger.info(f"Starting Gradio AI Research Mega Agent (v5.
|
546 |
app_port, app_server = int(os.getenv("GRADIO_PORT", 7860)), os.getenv("GRADIO_SERVER_NAME", "127.0.0.1")
|
547 |
app_debug, app_share = os.getenv("GRADIO_DEBUG", "False").lower()=="true", os.getenv("GRADIO_SHARE", "False").lower()=="true"
|
548 |
logger.info(f"Launching Gradio server: http://{app_server}:{app_port}. Debug: {app_debug}, Share: {app_share}")
|
|
|
43 |
logger.info(f"App Config: WebSearch={WEB_SEARCH_ENABLED}, ToolDecisionProvider={TOOL_DECISION_PROVIDER_ENV}, ToolDecisionModelID={TOOL_DECISION_MODEL_ID_ENV}, MemoryBackend={MEMORY_STORAGE_BACKEND}")
|
44 |
|
45 |
# --- Helper Functions (format_insights_for_prompt, generate_interaction_metrics, etc.) ---
|
46 |
+
# ENSURE ALL YOUR HELPER AND LOGIC FUNCTIONS ARE DEFINED HERE AS IN PREVIOUS VERSIONS
|
|
|
|
|
|
|
|
|
47 |
def format_insights_for_prompt(retrieved_insights_list: list[str]) -> tuple[str, list[dict]]:
|
48 |
if not retrieved_insights_list:
|
49 |
return "No specific guiding principles or learned insights retrieved.", []
|
|
|
391 |
logger.info(msg); return msg
|
392 |
|
393 |
# --- UI Definition ---
|
394 |
+
# Using the CSS inspired by the "AI Code & Space Generator" example
|
395 |
custom_theme = gr.themes.Base(
|
396 |
primary_hue="teal",
|
397 |
secondary_hue="purple",
|
|
|
399 |
text_size="sm",
|
400 |
spacing_size="md",
|
401 |
radius_size="sm",
|
402 |
+
font=["System UI", "sans-serif"]
|
403 |
)
|
404 |
|
405 |
custom_css = """
|
406 |
+
body { background: linear-gradient(to bottom right, #2c3e50, #34495e); color: #ecf0f1; margin:0; padding:0; font-family: 'System UI', sans-serif; overflow-x: hidden;}
|
407 |
+
.gradio-container { background: transparent !important; padding: 0 !important; /* Remove padding from the outermost container */ }
|
408 |
+
.main-interface-wrapper { max-width: 100%; padding: 1rem; box-sizing: border-box; min-height: 100vh; display: flex; flex-direction: column;} /* New wrapper */
|
409 |
.gr-block.gr-group, .gr-tabs, .gr-accordion { background-color: rgba(44, 62, 80, 0.8) !important; border: 1px solid rgba(189, 195, 199, 0.2) !important; border-radius: 8px !important; padding: 1em; margin-bottom: 1em;}
|
410 |
.gr-tabitem { background-color: rgba(52, 73, 94, 0.75) !important; border-radius: 6px !important; padding: 1em !important; border: 1px solid rgba(189, 195, 199, 0.1) !important;}
|
411 |
.gr-textbox, .gr-dropdown, .gr-button, .gr-code, .gr-chat-message, .gr-json, .gr-file input[type="file"], .gr-file button { border-color: rgba(189, 195, 199, 0.3) !important; background-color: rgba(52, 73, 94, 0.9) !important; color: #ecf0f1 !important; border-radius: 6px !important;}
|
412 |
.gr-textarea textarea, .gr-textbox input { color: #ecf0f1 !important; }
|
413 |
.gr-button.gr-button-primary { background-color: #1abc9c !important; color: white !important; border-color: #16a085 !important; }
|
414 |
.gr-button.gr-button-secondary { background-color: #9b59b6 !important; color: white !important; border-color: #8e44ad !important;}
|
415 |
+
.gr-button.gr-button-stop { background-color: #e74c3c !important; color: white !important; border-color: #c0392b !important; }
|
416 |
+
.gr-markdown { padding: 5px; }
|
417 |
+
.gr-group .gr-markdown { padding: 0px; background-color: transparent !important; }
|
418 |
.gr-markdown h1, .gr-markdown h2, .gr-markdown h3 { color: #ecf0f1 !important; border-bottom-color: rgba(189, 195, 199, 0.3) !important; margin-top: 0.5em; margin-bottom: 0.5em;}
|
419 |
.gr-markdown h1 {font-size: 1.5rem;} .gr-markdown h2 {font-size: 1.25rem;} .gr-markdown h3 {font-size: 1.1rem;}
|
420 |
.gr-markdown p, .gr-markdown li { color: #ecf0f1 !important; }
|
|
|
424 |
.gr-chatbot .message.user { background-color: rgba(46, 204, 113, 0.9) !important; color: black !important; }
|
425 |
.gr-input-label > .label-text, .gr-dropdown-label > .label-text { color: #bdc3c7 !important; }
|
426 |
.status-bar { padding: 0.5rem 1rem; border-radius: 6px; margin-bottom: 1rem; background-color: rgba(44, 62, 80, 0.8) !important; }
|
427 |
+
.tabnav button { background-color: rgba(52, 73, 94, 0.8) !important; color: #ecf0f1 !important; border-bottom: 2px solid transparent !important;}
|
428 |
.tabnav button.selected { background-color: rgba(44, 62, 80, 0.95) !important; color: #1abc9c !important; border-bottom: 2px solid #1abc9c !important;}
|
429 |
+
.app-title-area { padding: 0.5rem 0; text-align: center;} /* Area for title and subtitle */
|
|
|
|
|
|
|
|
|
|
|
430 |
"""
|
431 |
|
432 |
+
with gr.Blocks(theme=custom_theme, css=custom_css, title="AI Research Agent v5.8") as demo:
|
433 |
+
# Removed gr.HTML("<style>" + custom_css + "</style>") as css is passed to gr.Blocks
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
434 |
|
435 |
+
with gr.Column(elem_classes="main-interface-wrapper"): # New overall wrapper for content
|
436 |
+
with gr.Row(elem_classes="app-title-area"):
|
437 |
+
gr.Markdown("# π€ AI Research Agent")
|
438 |
+
# Removed subtitle from here, can be added in description of Space if hosted on HF
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
439 |
|
440 |
+
avail_provs = get_available_providers()
|
441 |
+
def_prov = avail_provs[0] if avail_provs else None
|
442 |
+
def_models = get_model_display_names_for_provider(def_prov) if def_prov else []
|
443 |
+
def_model_disp = get_default_model_display_name_for_provider(def_prov) if def_prov else None
|
444 |
+
|
445 |
+
with gr.Row(elem_classes="status-bar"):
|
446 |
+
with gr.Column(scale=3): agent_stat_tb = gr.Textbox(label="Agent Status", interactive=False, lines=1, value="Initializing systems...")
|
447 |
+
with gr.Column(scale=1): memory_backend_info_tb = gr.Textbox(label="Memory Backend", value=f"{MEMORY_STORAGE_BACKEND}", interactive=False)
|
448 |
+
sqlite_path_display = gr.Textbox(label="SQLite Path", value=f"{MEMORY_SQLITE_PATH}", interactive=False, visible=(MEMORY_STORAGE_BACKEND == "SQLITE"), scale=2) # Use visible
|
449 |
+
hf_repos_display = gr.Textbox(label="HF Repos", value=f"M: {MEMORY_HF_MEM_REPO}, R: {MEMORY_HF_RULES_REPO}", interactive=False, visible=(MEMORY_STORAGE_BACKEND == "HF_DATASET"), scale=2) # Use visible
|
450 |
+
|
451 |
+
with gr.Row(equal_height=False):
|
452 |
+
with gr.Sidebar(): # Using Gradio's explicit Sidebar component
|
453 |
+
gr.Markdown("## βοΈ Configuration")
|
454 |
+
with gr.Group():
|
455 |
+
gr.Markdown("### AI Model Settings")
|
456 |
+
api_key_tb = gr.Textbox(label="AI Provider API Key (Override)", type="password", placeholder="Uses .env if blank")
|
457 |
+
prov_sel_dd = gr.Dropdown(label="AI Provider", choices=avail_provs, value=def_prov, interactive=True)
|
458 |
+
model_sel_dd = gr.Dropdown(label="AI Model", choices=def_models, value=def_model_disp, interactive=True)
|
459 |
+
with gr.Group():
|
460 |
+
gr.Markdown("### System Prompt")
|
461 |
+
sys_prompt_tb = gr.Textbox(label="System Prompt Base", lines=10, value=DEFAULT_SYSTEM_PROMPT, interactive=True)
|
462 |
+
if MEMORY_STORAGE_BACKEND == "RAM":
|
463 |
+
save_faiss_sidebar_btn = gr.Button("Save FAISS Indices", variant="secondary")
|
464 |
+
|
465 |
+
with gr.Column(scale=3): # Main content area to the right of sidebar
|
466 |
+
with gr.Tabs():
|
467 |
+
with gr.TabItem("π¬ AI Chat & Research Output"):
|
468 |
+
gr.Markdown("### AI Chat Interface") # Using H3 for section titles within tabs
|
469 |
+
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)
|
470 |
with gr.Row():
|
471 |
user_msg_tb = gr.Textbox(show_label=False, placeholder="Ask your research question...", scale=7, lines=1, max_lines=5, autofocus=True)
|
472 |
send_btn = gr.Button("Send", variant="primary", scale=1, min_width=100)
|
473 |
+
with gr.Accordion("π Full Response / Details", open=False):
|
474 |
+
fmt_report_tb = gr.Textbox(label="Full AI Response", lines=10, interactive=True, show_copy_button=True)
|
475 |
+
dl_report_btn = gr.DownloadButton("Download Report", interactive=False, visible=False)
|
476 |
+
detect_out_md = gr.Markdown()
|
477 |
|
478 |
+
with gr.TabItem("π§ Knowledge Base Management"):
|
479 |
+
gr.Markdown("### Manage Stored Knowledge")
|
480 |
+
with gr.Row():
|
481 |
+
with gr.Column():
|
482 |
+
with gr.Group():
|
483 |
+
gr.Markdown("#### π Rules (Insights)")
|
484 |
+
rules_disp_ta = gr.TextArea(label="View/Edit Rules", lines=15, interactive=True, placeholder="Load rules or type new ones...")
|
485 |
+
with gr.Row(): view_rules_btn = gr.Button("π Load"); save_edited_rules_btn = gr.Button("πΎ Save Edits", variant="primary")
|
486 |
+
upload_rules_fobj = gr.File(label="Upload Rules File", file_types=[".txt", ".jsonl"])
|
487 |
+
rules_stat_tb = gr.Textbox(label="Rules Status", interactive=False, lines=2)
|
488 |
+
clear_rules_btn = gr.Button("β οΈ Clear All Rules", variant="stop")
|
489 |
+
with gr.Column():
|
490 |
+
with gr.Group():
|
491 |
+
gr.Markdown("#### π Memories")
|
492 |
+
mems_disp_json = gr.JSON(label="View Memories")
|
493 |
+
view_mems_btn = gr.Button("π Load Memories")
|
494 |
+
upload_mems_fobj = gr.File(label="Upload Memories File", file_types=[".jsonl"])
|
495 |
+
mems_stat_tb = gr.Textbox(label="Memories Status", interactive=False, lines=2)
|
496 |
+
clear_mems_btn = gr.Button("β οΈ Clear All Memories", variant="stop")
|
497 |
+
|
498 |
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)
|
499 |
prov_sel_dd.change(fn=dyn_upd_model_dd, inputs=prov_sel_dd, outputs=model_sel_dd)
|
500 |
chat_ins = [user_msg_tb, main_chat_disp, prov_sel_dd, model_sel_dd, api_key_tb, sys_prompt_tb]
|
|
|
531 |
backend_status = f"AI Systems Initialized. Ready."
|
532 |
rules_on_load = ui_view_rules_action_fn()
|
533 |
mems_on_load = ui_view_memories_action_fn()
|
534 |
+
# Initial visibility for conditional Textboxes based on backend
|
535 |
+
vis_sqlite = MEMORY_STORAGE_BACKEND == "SQLITE"
|
536 |
+
vis_hf = MEMORY_STORAGE_BACKEND == "HF_DATASET"
|
537 |
+
return backend_status, rules_on_load, mems_on_load, gr.update(visible=vis_sqlite), gr.update(visible=vis_hf)
|
538 |
+
|
539 |
+
demo.load(fn=app_load_fn, inputs=None, outputs=[agent_stat_tb, rules_disp_ta, mems_disp_json, sqlite_path_display, hf_repos_display])
|
540 |
|
541 |
if __name__ == "__main__":
|
542 |
+
logger.info(f"Starting Gradio AI Research Mega Agent (v5.8 - UI Style/Layout Update, Memory: {MEMORY_STORAGE_BACKEND})...")
|
543 |
app_port, app_server = int(os.getenv("GRADIO_PORT", 7860)), os.getenv("GRADIO_SERVER_NAME", "127.0.0.1")
|
544 |
app_debug, app_share = os.getenv("GRADIO_DEBUG", "False").lower()=="true", os.getenv("GRADIO_SHARE", "False").lower()=="true"
|
545 |
logger.info(f"Launching Gradio server: http://{app_server}:{app_port}. Debug: {app_debug}, Share: {app_share}")
|