Commit
·
3924f42
1
Parent(s):
e3fd5ce
api endpoints for UI migration v6
Browse files
app.py
CHANGED
|
@@ -24,6 +24,9 @@ logger = logging.getLogger(__name__)
|
|
| 24 |
orchestrator = None
|
| 25 |
orchestrator_available = False
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
# Process Flow Visualization - DISABLED
|
| 28 |
# Moving functionality to container logs instead of UI
|
| 29 |
process_flow_available = False
|
|
@@ -520,7 +523,8 @@ def create_mobile_optimized_interface():
|
|
| 520 |
interface_components['new_session_btn'].click(
|
| 521 |
fn=new_session,
|
| 522 |
inputs=[interface_components['user_dropdown']],
|
| 523 |
-
outputs=[interface_components['session_info']]
|
|
|
|
| 524 |
)
|
| 525 |
|
| 526 |
# Wire up User Dropdown to update session info - Enhanced API endpoint
|
|
@@ -559,7 +563,8 @@ def create_mobile_optimized_interface():
|
|
| 559 |
interface_components['user_dropdown'].change(
|
| 560 |
fn=update_session_info,
|
| 561 |
inputs=[interface_components['user_dropdown'], interface_components['session_info']],
|
| 562 |
-
outputs=[interface_components['session_info']]
|
|
|
|
| 563 |
)
|
| 564 |
|
| 565 |
# Wire up Settings button to toggle settings panel - Enhanced API endpoint
|
|
@@ -585,7 +590,8 @@ def create_mobile_optimized_interface():
|
|
| 585 |
interface_components['menu_toggle'].click(
|
| 586 |
fn=toggle_settings,
|
| 587 |
inputs=None,
|
| 588 |
-
outputs=[interface_components['settings_panel']]
|
|
|
|
| 589 |
)
|
| 590 |
|
| 591 |
# Also wire up settings_nav_btn from mobile navigation if it exists - Enhanced API endpoint
|
|
@@ -609,7 +615,8 @@ def create_mobile_optimized_interface():
|
|
| 609 |
interface_components['settings_nav_btn'].click(
|
| 610 |
fn=toggle_settings_from_nav,
|
| 611 |
inputs=None,
|
| 612 |
-
outputs=[interface_components['settings_panel']]
|
|
|
|
| 613 |
)
|
| 614 |
|
| 615 |
# Wire up Context Mode change handler - Enhanced API endpoint
|
|
@@ -697,7 +704,8 @@ def create_mobile_optimized_interface():
|
|
| 697 |
context_mode_radio.change(
|
| 698 |
fn=handle_mode_change,
|
| 699 |
inputs=[context_mode_radio, session_info],
|
| 700 |
-
outputs=[mode_status]
|
|
|
|
| 701 |
)
|
| 702 |
|
| 703 |
# Wire up Save Preferences button - Enhanced API endpoint with database persistence
|
|
@@ -768,11 +776,74 @@ def create_mobile_optimized_interface():
|
|
| 768 |
interface_components.get('show_agent_trace', None),
|
| 769 |
interface_components.get('response_speed', None),
|
| 770 |
interface_components.get('cache_enabled', None)
|
| 771 |
-
]
|
|
|
|
| 772 |
)
|
| 773 |
|
| 774 |
# Process Flow event handlers - DISABLED
|
| 775 |
# Process flow information is now logged to container logs instead of UI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 776 |
|
| 777 |
return demo, interface_components
|
| 778 |
|
|
@@ -1823,6 +1894,9 @@ if SPACES_GPU_AVAILABLE and GPU is not None:
|
|
| 1823 |
return _build_dynamic_return_values(error_result, "", _interface_components)
|
| 1824 |
|
| 1825 |
chat_handler_fn = safe_gpu_chat_handler
|
|
|
|
|
|
|
|
|
|
| 1826 |
else:
|
| 1827 |
def chat_handler_wrapper(message, history, user_id="Admin_J", session_text=""):
|
| 1828 |
"""
|
|
@@ -1922,6 +1996,9 @@ else:
|
|
| 1922 |
return _build_dynamic_return_values(error_result, "", _interface_components)
|
| 1923 |
|
| 1924 |
chat_handler_fn = chat_handler_wrapper
|
|
|
|
|
|
|
|
|
|
| 1925 |
|
| 1926 |
# Initialize orchestrator on module load
|
| 1927 |
def initialize_orchestrator():
|
|
|
|
| 24 |
orchestrator = None
|
| 25 |
orchestrator_available = False
|
| 26 |
|
| 27 |
+
# Global API chat handler function (set after handlers are defined)
|
| 28 |
+
_api_chat_handler_fn = None
|
| 29 |
+
|
| 30 |
# Process Flow Visualization - DISABLED
|
| 31 |
# Moving functionality to container logs instead of UI
|
| 32 |
process_flow_available = False
|
|
|
|
| 523 |
interface_components['new_session_btn'].click(
|
| 524 |
fn=new_session,
|
| 525 |
inputs=[interface_components['user_dropdown']],
|
| 526 |
+
outputs=[interface_components['session_info']],
|
| 527 |
+
api_name="new_session"
|
| 528 |
)
|
| 529 |
|
| 530 |
# Wire up User Dropdown to update session info - Enhanced API endpoint
|
|
|
|
| 563 |
interface_components['user_dropdown'].change(
|
| 564 |
fn=update_session_info,
|
| 565 |
inputs=[interface_components['user_dropdown'], interface_components['session_info']],
|
| 566 |
+
outputs=[interface_components['session_info']],
|
| 567 |
+
api_name="update_session_info"
|
| 568 |
)
|
| 569 |
|
| 570 |
# Wire up Settings button to toggle settings panel - Enhanced API endpoint
|
|
|
|
| 590 |
interface_components['menu_toggle'].click(
|
| 591 |
fn=toggle_settings,
|
| 592 |
inputs=None,
|
| 593 |
+
outputs=[interface_components['settings_panel']],
|
| 594 |
+
api_name="toggle_settings"
|
| 595 |
)
|
| 596 |
|
| 597 |
# Also wire up settings_nav_btn from mobile navigation if it exists - Enhanced API endpoint
|
|
|
|
| 615 |
interface_components['settings_nav_btn'].click(
|
| 616 |
fn=toggle_settings_from_nav,
|
| 617 |
inputs=None,
|
| 618 |
+
outputs=[interface_components['settings_panel']],
|
| 619 |
+
api_name="toggle_settings_from_nav"
|
| 620 |
)
|
| 621 |
|
| 622 |
# Wire up Context Mode change handler - Enhanced API endpoint
|
|
|
|
| 704 |
context_mode_radio.change(
|
| 705 |
fn=handle_mode_change,
|
| 706 |
inputs=[context_mode_radio, session_info],
|
| 707 |
+
outputs=[mode_status],
|
| 708 |
+
api_name="handle_mode_change"
|
| 709 |
)
|
| 710 |
|
| 711 |
# Wire up Save Preferences button - Enhanced API endpoint with database persistence
|
|
|
|
| 776 |
interface_components.get('show_agent_trace', None),
|
| 777 |
interface_components.get('response_speed', None),
|
| 778 |
interface_components.get('cache_enabled', None)
|
| 779 |
+
],
|
| 780 |
+
api_name="save_preferences"
|
| 781 |
)
|
| 782 |
|
| 783 |
# Process Flow event handlers - DISABLED
|
| 784 |
# Process flow information is now logged to container logs instead of UI
|
| 785 |
+
|
| 786 |
+
# Expose safe_gpu_chat_handler as REST API endpoint
|
| 787 |
+
# Create hidden components for API-only access (not visible in UI)
|
| 788 |
+
with gr.Row(visible=False):
|
| 789 |
+
api_message = gr.Textbox(label="message", visible=False)
|
| 790 |
+
api_history = gr.JSON(label="history", visible=False)
|
| 791 |
+
api_user_id = gr.Textbox(label="user_id", visible=False)
|
| 792 |
+
api_session_text = gr.Textbox(label="session_text", visible=False)
|
| 793 |
+
|
| 794 |
+
api_output_chatbot = gr.JSON(label="chatbot_history", visible=False)
|
| 795 |
+
api_output_message = gr.Textbox(label="message_input", visible=False)
|
| 796 |
+
api_output_reasoning = gr.JSON(label="reasoning_data", visible=False)
|
| 797 |
+
api_output_performance = gr.JSON(label="performance_data", visible=False)
|
| 798 |
+
api_output_context = gr.JSON(label="context_data", visible=False)
|
| 799 |
+
api_output_session = gr.Textbox(label="session_info", visible=False)
|
| 800 |
+
api_output_skills = gr.HTML(label="skills_html", visible=False)
|
| 801 |
+
|
| 802 |
+
# Create API chat handler wrapper
|
| 803 |
+
# This will use the global _api_chat_handler_fn after handlers are defined
|
| 804 |
+
def api_chat_handler(message, history, user_id="Admin_J", session_text=""):
|
| 805 |
+
"""
|
| 806 |
+
API chat handler wrapper - uses the appropriate handler based on GPU availability.
|
| 807 |
+
The actual handler is set in _api_chat_handler_fn after handlers are defined.
|
| 808 |
+
"""
|
| 809 |
+
global _api_chat_handler_fn
|
| 810 |
+
|
| 811 |
+
# Try to use the global API handler function if available
|
| 812 |
+
if _api_chat_handler_fn is not None:
|
| 813 |
+
try:
|
| 814 |
+
return _api_chat_handler_fn(message, history, user_id, session_text)
|
| 815 |
+
except Exception as e:
|
| 816 |
+
logger.error(f"Error in _api_chat_handler_fn: {e}", exc_info=True)
|
| 817 |
+
# Fallback to process_message
|
| 818 |
+
pass
|
| 819 |
+
|
| 820 |
+
# Fallback: use process_message directly (works regardless of GPU setup)
|
| 821 |
+
is_valid, session_id, validated_user_id, error_msg = _validate_chat_inputs(
|
| 822 |
+
message, history, user_id, session_text, source="api"
|
| 823 |
+
)
|
| 824 |
+
|
| 825 |
+
if not is_valid:
|
| 826 |
+
logger.error(f"Invalid API input: {error_msg}")
|
| 827 |
+
error_result = _create_error_response(error_msg, history)
|
| 828 |
+
return _build_dynamic_return_values(error_result, "", _interface_components)
|
| 829 |
+
|
| 830 |
+
try:
|
| 831 |
+
result = process_message(message, history, session_id, validated_user_id)
|
| 832 |
+
return result
|
| 833 |
+
except Exception as e:
|
| 834 |
+
logger.error(f"Error in api_chat_handler: {e}", exc_info=True)
|
| 835 |
+
error_result = _create_error_response(f"Processing error: {str(e)}", history)
|
| 836 |
+
return _build_dynamic_return_values(error_result, "", _interface_components)
|
| 837 |
+
|
| 838 |
+
# Expose the handler function with api_name for REST API access
|
| 839 |
+
api_message.submit(
|
| 840 |
+
fn=api_chat_handler,
|
| 841 |
+
inputs=[api_message, api_history, api_user_id, api_session_text],
|
| 842 |
+
outputs=[api_output_chatbot, api_output_message, api_output_reasoning,
|
| 843 |
+
api_output_performance, api_output_context, api_output_session,
|
| 844 |
+
api_output_skills],
|
| 845 |
+
api_name="safe_gpu_chat_handler"
|
| 846 |
+
)
|
| 847 |
|
| 848 |
return demo, interface_components
|
| 849 |
|
|
|
|
| 1894 |
return _build_dynamic_return_values(error_result, "", _interface_components)
|
| 1895 |
|
| 1896 |
chat_handler_fn = safe_gpu_chat_handler
|
| 1897 |
+
|
| 1898 |
+
# Make safe_gpu_chat_handler accessible globally for API endpoint
|
| 1899 |
+
_api_chat_handler_fn = safe_gpu_chat_handler
|
| 1900 |
else:
|
| 1901 |
def chat_handler_wrapper(message, history, user_id="Admin_J", session_text=""):
|
| 1902 |
"""
|
|
|
|
| 1996 |
return _build_dynamic_return_values(error_result, "", _interface_components)
|
| 1997 |
|
| 1998 |
chat_handler_fn = chat_handler_wrapper
|
| 1999 |
+
|
| 2000 |
+
# Make chat_handler_wrapper accessible globally for API endpoint
|
| 2001 |
+
_api_chat_handler_fn = chat_handler_wrapper
|
| 2002 |
|
| 2003 |
# Initialize orchestrator on module load
|
| 2004 |
def initialize_orchestrator():
|