""" Main entry point for the PharmaCircle AI Data Analyst application. This script initializes the necessary services and launches the Gradio user interface. It has been refactored to be a lean entry point, delegating all complex logic to specialized modules. """ import logging import connections from ui import create_ui # --- Suppress Matplotlib Debug Logs --- logging.getLogger('matplotlib').setLevel(logging.WARNING) def main(): """ Initializes connections and launches the Gradio application. """ # Initialize all external connections (SSH, Solr, LLM) ssh_tunnel, solr_client, llm_model = connections.initialize_connections() if not all([ssh_tunnel, solr_client, llm_model]): print("\nSkipping Gradio launch due to initialization errors.") # Ensure the tunnel is closed if it was partially opened if ssh_tunnel and ssh_tunnel.is_active: ssh_tunnel.stop() return # Create and launch the Gradio UI demo = create_ui(llm_model, solr_client) try: demo.queue().launch(debug=True, share=True, allowed_paths=['/tmp/plots']) except (IOError, OSError) as e: print(f"An error occurred while launching the Gradio app: {e}") print("Please check if the port is already in use or if you have the necessary permissions.") except Exception as e: print(f"An unexpected error occurred: {e}") finally: # Ensure the SSH tunnel is closed when the app is shut down print("\nClosing SSH tunnel...") if ssh_tunnel.is_active: ssh_tunnel.stop() print("SSH tunnel closed. Exiting.") if __name__ == "__main__": main()