import os
import json
import importlib
import requests
import gradio as gr
import subprocess
class Tool:
def __init__(self, name, description, ui_class, icon="🔧", version="0.1", ui_module=None):
self.name = name
self.description = description
self.ui_class = ui_class
self.icon = icon
self.version = version
self.ui_module = ui_module
class MainUI:
def __init__(self):
# Load installed tools from JSON
self.tools = {}
self.load_installed_tools()
# Marketplace server URL
self.marketplace_url = "https://marketplace.vidplus.app/api/marketplace"
self.version = "0.1"
# Create tools directory if it doesn't exist
os.makedirs('tools', exist_ok=True)
def load_installed_tools(self):
"""Load installed tools from JSON file"""
try:
if os.path.exists('installed_tools.json'):
with open('installed_tools.json', 'r') as f:
data = json.load(f)
for tool_data in data.get('tools', []):
# If ui_module is specified, dynamically import it
if tool_data.get('ui_module'):
try:
module = importlib.import_module(tool_data['ui_module'])
ui_class = getattr(module, tool_data['ui_class'])
self.tools[tool_data['id']] = Tool(
name=tool_data['name'],
description=tool_data['description'],
ui_class=ui_class,
icon=tool_data['icon'],
version=tool_data['version'],
ui_module=tool_data['ui_module']
)
except (ImportError, AttributeError) as e:
print(f"Error loading tool {tool_data['name']}: {e}")
except Exception as e:
print(f"Error loading installed tools: {e}")
# Fallback to default BeatSyncer tool
# self.tools = {
# "beatsyncer": Tool(
# name="BeatSyncer",
# description="Create amazing videos synchronized with music beats",
# ui_class=BeatSyncerUI,
# icon="🎵"
# )
# }
def save_installed_tools(self):
"""Save installed tools to JSON file"""
data = {
"tools": [
{
"id": tool_id,
"name": tool.name,
"description": tool.description,
"icon": tool.icon,
"version": tool.version,
"ui_class": tool.ui_class.__name__,
"ui_module": tool.ui_module
}
for tool_id, tool in self.tools.items()
]
}
with open('installed_tools.json', 'w') as f:
json.dump(data, f, indent=4)
def fetch_marketplace_tools(self):
"""Fetch available tools from marketplace server"""
try:
response = requests.get(f"{self.marketplace_url}/tools")
if response.status_code == 200:
# Get the list of tools from the marketplace
marketplace_tools = response.json()
# Ensure we're working with the latest state of installed tools
# This is important after a tool has been removed
if os.path.exists('installed_tools.json'):
with open('installed_tools.json', 'r') as f:
data = json.load(f)
installed_tool_ids = [tool['id'] for tool in data.get('tools', [])]
else:
installed_tool_ids = []
# Update the is_installed flag for each tool
for tool in marketplace_tools:
tool['is_installed'] = tool['id'] in installed_tool_ids
return marketplace_tools
else:
return []
except Exception as e:
print(f"Error fetching marketplace tools: {e}")
return []
def fetch_tool_details(self, tool_id):
"""Fetch details for a specific tool from marketplace server"""
try:
response = requests.get(f"{self.marketplace_url}/tools/{tool_id}")
if response.status_code == 200:
return response.json()
else:
return None
except Exception as e:
print(f"Error fetching tool details: {e}")
return None
def install_tool(self, tool_id):
"""Install a tool from the marketplace"""
try:
# Import required modules
import importlib
import importlib.util
import sys
import subprocess
import gradio as gr
# Fetch tool data
response = requests.get(f"{self.marketplace_url}/tools/{tool_id}/download")
if response.status_code != 200:
return f"Error: Failed to download tool (Status code: {response.status_code})"
tool_data = response.json()
tool_info = tool_data['tool']
tool_files = tool_data['files']
# Check and install dependencies if they exist
dependencies = tool_info.get('dependencies', [])
if dependencies:
try:
# Read current requirements.txt
current_requirements = []
if os.path.exists('requirements.txt'):
with open('requirements.txt', 'r') as f:
current_requirements = [line.strip() for line in f.readlines() if line.strip()]
# Check which dependencies need to be installed
to_install = []
to_add_to_requirements = []
for dep in dependencies:
# Extract just the package name (ignore version requirements)
pkg_name = dep.split('>=')[0].split('==')[0].split('>')[0].split('<')[0].strip()
# Check if dependency is already in requirements.txt
is_in_requirements = False
for req in current_requirements:
req_name = req.split('>=')[0].split('==')[0].split('>')[0].split('<')[0].strip()
if req_name == pkg_name:
is_in_requirements = True
break
if not is_in_requirements:
to_add_to_requirements.append(dep)
try:
# Try to import the package to check if it's installed
importlib.import_module(pkg_name)
except ImportError:
# Package not installed, add to installation list
to_install.append(dep)
# Install missing dependencies
if to_install:
for dep in to_install:
# Use subprocess to run pip install
subprocess.check_call([sys.executable, "-m", "pip", "install", dep])
# Append new dependencies to requirements.txt
if to_add_to_requirements:
with open('requirements.txt', 'a') as f:
for dep in to_add_to_requirements:
f.write(f"\n{dep}")
print(f"Added {len(to_add_to_requirements)} dependencies to requirements.txt")
except Exception as e:
return f"Error installing dependencies: {e}"
# Create tool directory
tool_dir = os.path.join('tools', tool_id)
os.makedirs(tool_dir, exist_ok=True)
# Write tool files
for file_name, file_content in tool_files.items():
# Rename files to match our convention
target_file_name = file_name
if file_name.endswith('_tool_logic.py'):
target_file_name = f"{tool_id}_tool.py"
elif file_name.endswith('_tool_ui.py'):
target_file_name = f"{tool_id}_ui.py"
with open(os.path.join(tool_dir, target_file_name), 'w') as f:
f.write(file_content)
# Create __init__.py to make the directory a package
with open(os.path.join(tool_dir, '__init__.py'), 'w') as f:
f.write(f"# {tool_info['name']} package\n")
# Add tool to installed tools
ui_module = f"tools.{tool_id}.{tool_id}_ui"
ui_class = f"{tool_id.title().replace('_', '')}UI"
# Update installed_tools.json
with open('installed_tools.json', 'r') as f:
data = json.load(f)
data['tools'].append({
"id": tool_id,
"name": tool_info['name'],
"description": tool_info['description'],
"icon": tool_info['icon'],
"version": tool_info['version'],
"ui_class": ui_class,
"ui_module": ui_module
})
with open('installed_tools.json', 'w') as f:
json.dump(data, f, indent=4)
# Dynamically load the newly installed tool
try:
# Reload the module if it was already imported
if ui_module in sys.modules:
importlib.reload(sys.modules[ui_module])
# Import the module
module = importlib.import_module(ui_module)
ui_class_obj = getattr(module, ui_class)
# Add the tool to self.tools
self.tools[tool_id] = Tool(
name=tool_info['name'],
description=tool_info['description'],
ui_class=ui_class_obj,
icon=tool_info['icon'],
version=tool_info['version'],
ui_module=ui_module
)
# Flag to indicate this is a newly installed tool that needs special handling
self.tools[tool_id].newly_installed = True
# Return success message
return f"Successfully installed {tool_info['name']}. The tool is now available in the Tools page."
except Exception as e:
print(f"Error dynamically loading tool: {e}")
import traceback
traceback.print_exc()
# Still return success but with a restart message
return f"Successfully installed {tool_info['name']}. Please restart the application to use the new tool."
except Exception as e:
return f"Error installing tool: {e}"
def remove_tool(self, tool_id):
"""Remove an installed tool"""
try:
if tool_id not in self.tools:
return "Error: Tool is not installed"
# Get tool info before removal
tool_name = self.tools[tool_id].name
# Remove tool directory
tool_dir = os.path.join('tools', tool_id)
if os.path.exists(tool_dir):
import shutil
shutil.rmtree(tool_dir)
print(f"Removed tool directory: {tool_dir}")
# Remove tool from installed_tools.json
if os.path.exists('installed_tools.json'):
with open('installed_tools.json', 'r') as f:
data = json.load(f)
# Filter out the tool to be removed
data['tools'] = [tool for tool in data['tools'] if tool['id'] != tool_id]
# Save updated tools list
with open('installed_tools.json', 'w') as f:
json.dump(data, f, indent=4)
print(f"Removed tool from installed_tools.json: {tool_id}")
# Remove tool from self.tools
del self.tools[tool_id]
return f"✅ Successfully removed {tool_name}"
except Exception as e:
print(f"Error removing tool: {e}")
import traceback
traceback.print_exc()
return f"Error removing tool: {str(e)}"
def create_tool_card(self, name):
return f"""
'
for i, tool in enumerate(tools):
# Use a unique ID for each card based on the tool ID
# Add a class to indicate if the tool is installed
installed_class = "installed" if tool.get('is_installed', False) else ""
# Add an indicator for installed tools
installed_indicator = '
✓ Installed
' if tool.get('is_installed', False) else ''
html += f'''
{tool['icon']}
{tool['name']}
{tool['description']}
{installed_indicator}
'''
html += '
'
# Hide installation result container and confirmation dialog
installation_result_container_update = gr.update(visible=False)
confirmation_dialog_update = gr.update(visible=False)
# Create updates for all tool containers
tool_container_updates = [gr.update(visible=False) for _ in tool_containers]
# Update marketplace_tools_state with the latest tools
# This ensures the marketplace shows the current state of available tools
return [
gr.update(visible=False), # tools_content
gr.update(visible=True), # marketplace_content
gr.update(visible=False), # settings_content
gr.update(visible=False), # tool_interfaces
gr.update(visible=False), # tool_details_content
gr.update(variant="secondary"), # tools_btn
gr.update(variant="primary"), # marketplace_btn
gr.update(variant="secondary"), # settings_btn
tools, # marketplace_tools_state - updated with latest tools
html, # marketplace_html
installation_result_container_update, # installation_result_container
confirmation_dialog_update, # confirmation_dialog
gr.update(value="") # tool_interfaces_html
] + tool_container_updates
def show_settings():
# Create updates to hide all tool containers
tool_container_updates = [gr.update(visible=False) for _ in tool_containers]
return [
gr.update(visible=False), # tools_content
gr.update(visible=False), # marketplace_content
gr.update(visible=True), # settings_content
gr.update(visible=False), # tool_interfaces
gr.update(visible=False), # tool_details_content
gr.update(variant="secondary"), # tools_btn
gr.update(variant="secondary"), # marketplace_btn
gr.update(variant="primary"), # settings_btn
gr.update(value="") # tool_interfaces_html
] + tool_container_updates
def show_tool(tool_id=None):
# Import gradio at the beginning of the function
import gradio as gr
# Check if the tool exists
if tool_id not in self.tools:
print(f"Tool {tool_id} not found")
return show_tools()
# Get the tool
tool = self.tools[tool_id]
# Check if this is a newly installed tool that needs special handling
if hasattr(tool, 'newly_installed') and tool.newly_installed:
print(f"Dynamically creating UI for newly installed tool: {tool_id}")
try:
# Create a message to inform the user that we're loading the tool
loading_message = f"""
Loading {tool.name}...
Please wait while we set up the tool interface.
"""
# First, return a loading message
loading_updates = [
gr.update(visible=False), # tools_content
gr.update(visible=False), # marketplace_content
gr.update(visible=False), # settings_content
gr.update(visible=True), # tool_interfaces
gr.update(visible=False), # tool_details_content
gr.update(variant="secondary"), # tools_btn
gr.update(variant="secondary"), # marketplace_btn
gr.update(variant="secondary"), # settings_btn
gr.update(value=loading_message) # tool_interfaces_html
] + [gr.update(visible=False) for _ in tool_containers]
# Instead of trying to create the UI dynamically, which can cause issues with Gradio components,
# we'll create a simple HTML interface that provides basic functionality and instructions
# Get the tool class name and module for display
tool_class_name = tool.ui_class.__name__
tool_module_name = tool.ui_module
# Create a friendly HTML interface
tool_html = f"""
{tool.name} is Ready!
This tool has been successfully installed.
For the best experience with this tool, please restart the application.
After restarting, you'll be able to use all the features of this tool.
"""
# Remove the newly_installed flag so we don't try to create the UI again
delattr(tool, 'newly_installed')
# Return the HTML interface
return [
gr.update(visible=False), # tools_content
gr.update(visible=False), # marketplace_content
gr.update(visible=False), # settings_content
gr.update(visible=True), # tool_interfaces
gr.update(visible=False), # tool_details_content
gr.update(variant="secondary"), # tools_btn
gr.update(variant="secondary"), # marketplace_btn
gr.update(variant="secondary"), # settings_btn
gr.update(value=tool_html) # tool_interfaces_html
] + [gr.update(visible=False) for _ in tool_containers]
except Exception as e:
print(f"Error creating UI for newly installed tool: {e}")
import traceback
traceback.print_exc()
# If there's an error, show a message asking the user to restart
restart_message = f"""
Error Loading {tool.name}
There was an error setting up the tool interface:
{str(e)}
Please restart the application to use this tool.
"""
return [
gr.update(visible=False), # tools_content
gr.update(visible=False), # marketplace_content
gr.update(visible=False), # settings_content
gr.update(visible=True), # tool_interfaces
gr.update(visible=False), # tool_details_content
gr.update(variant="secondary"), # tools_btn
gr.update(variant="secondary"), # marketplace_btn
gr.update(variant="secondary"), # settings_btn
gr.update(value=restart_message) # tool_interfaces_html
] + [gr.update(visible=False) for _ in tool_containers]
# For existing tools with proper UI containers
# Create a list of visibility updates for all tool containers
updates = []
for i, container in enumerate(tool_containers):
# Get the tool_id for this container
container_tool_id = None
for tid, t in self.tools.items():
if hasattr(t, 'ui_container') and t.ui_container == container:
container_tool_id = tid
break
# Set visibility based on whether this is the selected tool
if container_tool_id == tool_id:
updates.append(gr.update(visible=True))
else:
updates.append(gr.update(visible=False))
return [
gr.update(visible=False), # tools_content
gr.update(visible=False), # marketplace_content
gr.update(visible=False), # settings_content
gr.update(visible=True), # tool_interfaces
gr.update(visible=False), # tool_details_content
gr.update(variant="secondary"), # tools_btn
gr.update(variant="secondary"), # marketplace_btn
gr.update(variant="secondary"), # settings_btn
gr.update(value="") # tool_interfaces_html
] + updates
def show_tool_details(tool_id):
# Fetch tool details
tool = self.fetch_tool_details(tool_id)
# Create updates to hide all tool containers
tool_container_updates = [gr.update(visible=False) for _ in tool_containers]
if not tool:
return [
gr.update(visible=False), # tools_content
gr.update(visible=True), # marketplace_content
gr.update(visible=False), # settings_content
gr.update(visible=False), # tool_interfaces
gr.update(visible=False), # tool_details_content
gr.update(variant="secondary"), # tools_btn
gr.update(variant="primary"), # marketplace_btn
gr.update(variant="secondary"), # settings_btn
None, # current_tool_details_state
"", # tool_details_html
gr.update(visible=False), # installation_result_container
gr.update(visible=False), # confirmation_dialog
gr.update(value="") # tool_interfaces_html
] + tool_container_updates
# Create features HTML
features_html = "".join([
f'
✓{feature}
'
for feature in tool.get('features', [])
])
# Create demo video HTML
demo_video_html = f'' if True else ''
# Check if the tool is already installed
# Use the is_installed flag if available, otherwise check self.tools
is_installed = tool.get('is_installed', False) or tool_id in self.tools
# Determine which button to show based on installation status
if is_installed:
action_button = f''
else:
action_button = f''
# Create HTML for tool details
html = f'''
{tool['icon']}
{tool['name']} v{tool['version']}
{action_button}
{tool['description']}
Features
{features_html}
Video
{demo_video_html}
'''
print(f"Setting current_tool_details_state to: {tool}")
return [
gr.update(visible=False), # tools_content
gr.update(visible=False), # marketplace_content
gr.update(visible=False), # settings_content
gr.update(visible=False), # tool_interfaces
gr.update(visible=True), # tool_details_content
gr.update(variant="secondary"), # tools_btn
gr.update(variant="secondary"), # marketplace_btn
gr.update(variant="secondary"), # settings_btn
tool, # current_tool_details_state
html, # tool_details_html
gr.update(visible=False), # installation_result_container
gr.update(visible=False), # confirmation_dialog
gr.update(value="") # tool_interfaces_html
] + tool_container_updates
def install_selected_tool(tool_details):
print(f"Installing tool: {tool_details}")
if not tool_details:
print("No tool details provided")
return [
gr.update(visible=True), # installation_result_container
"⚠️ Installation Failed", # installation_status
"No tool selected to install." # installation_result
]
try:
# Install the tool
result = self.install_tool(tool_details['id'])
if "Error" in result:
return [
gr.update(visible=True), # installation_result_container
"⚠️ Installation Failed", # installation_status
result # installation_result
]
else:
# If installation was successful, we'll show a success message
# The user can click "Close" to see the installation result
# and then navigate to the tools page to see the new tool
return [
gr.update(visible=True), # installation_result_container
"✅ Installation Successful", # installation_status
result # installation_result
]
except Exception as e:
print(f"Error installing tool: {e}")
return [
gr.update(visible=True), # installation_result_container
"⚠️ Installation Failed", # installation_status
f"Error installing tool: {e}" # installation_result
]
# Create components after defining the functions
with gr.Row():
# Sidebar
with gr.Column(elem_id="sidebar", scale=1):
gr.Markdown("AI TOOLS", elem_classes="title")
tools_btn = gr.Button("Tools", elem_classes="nav-button", variant="primary")
marketplace_btn = gr.Button("Marketplace", elem_classes="nav-button", variant="secondary")
settings_btn = gr.Button("Settings", elem_classes="nav-button", variant="secondary")
gr.Markdown(f"version: {self.version}", elem_classes="version-text")
# Main content
with gr.Column(elem_id="main-content", scale=4):
# Define all content containers
tools_content = gr.Column(visible=True)
marketplace_content = gr.Column(visible=False)
settings_content = gr.Column(visible=False)
tool_interfaces = gr.Column(visible=False)
tool_details_content = gr.Column(visible=False)
# Fill tools content
with tools_content:
gr.Markdown("Tools", elem_classes="page-title")
tool_buttons = []
tool_ids = [] # Store tool_ids in the same order as tool_buttons
# Create HTML for all tools
tools_html = ''
for tool_id, tool in self.tools.items():
# Create HTML for each tool card
tools_html += f'''
{tool.icon}
{tool.name}
{tool.description if hasattr(tool, 'description') else ""}
'''
# Render the HTML inside a div with the tool-grid class
tools_html_component = gr.HTML(f'
{tools_html}
')
# Create hidden add tool button
add_tool_btn = gr.Button("Add New Tool", visible=False, elem_id="add-tool-btn")
# Fill marketplace content
with marketplace_content:
gr.Markdown("Marketplace", elem_classes="page-title")
marketplace_html = gr.HTML("Loading marketplace tools...")
# Create hidden buttons for marketplace tool details with unique IDs
tool_buttons_container = gr.Column(visible=False)
# Create hidden buttons for marketplace tools
marketplace_tool_buttons = []
for i, tool in enumerate(self.fetch_marketplace_tools()):
tool_btn = gr.Button(f"View {tool['name']}", visible=False, elem_id=f"marketplace-tool-btn-{i}")
marketplace_tool_buttons.append((tool['id'], tool_btn))
# The click handlers for marketplace tool buttons will be set up after all components are defined
# Fill tool details content
with tool_details_content:
# Installation result section at the top
with gr.Column(elem_id="installation-result-container", elem_classes="installation-result-container", visible=False) as installation_result_container:
installation_status = gr.Markdown("Ready to install", elem_id="installation-status")
installation_result = gr.Markdown("Click the Install Tool button to install the selected tool.", elem_id="installation-result")
close_result_btn = gr.Button("Close", elem_id="close-result-btn")
# Confirmation dialog for tool removal
with gr.Column(elem_id="confirmation-dialog", elem_classes="confirmation-dialog", visible=False) as confirmation_dialog:
gr.Markdown("⚠️ Warning: Remove Tool", elem_id="confirmation-title", elem_classes="confirmation-title")
gr.Markdown(
"Are you sure you want to remove this tool? This action cannot be undone. "
"All data created by this tool will be permanently deleted.",
elem_id="confirmation-message",
elem_classes="confirmation-message"
)
with gr.Row(elem_classes="confirmation-buttons"):
cancel_btn = gr.Button("Cancel", elem_classes="cancel-button")
confirm_btn = gr.Button("Yes, Remove Tool", elem_classes="confirm-button")
with gr.Row():
with gr.Column(scale=1):
back_btn = gr.Button("← Back to Marketplace", elem_classes="back-button")
with gr.Column(scale=3):
gr.Markdown("", elem_classes="page-title")
# Tool details content
tool_details_html = gr.HTML("")
# Hidden install button (will be triggered by the inline button)
install_btn = gr.Button("Add Tool", elem_classes="install-button", elem_id="install-btn", variant="primary", visible=False)
# Hidden remove button (will be triggered by the inline button)
remove_btn = gr.Button("Remove Tool", elem_classes="remove-button", elem_id="remove-btn", variant="primary", visible=False)
# Add a direct event handler to the install button
def debug_install_click():
print("Install button clicked directly!")
return [gr.update(visible=True), gr.update(value="Install button clicked!")]
# Add a direct event handler to the remove button to show confirmation dialog
def show_confirmation_dialog():
print("Remove button clicked - showing confirmation dialog")
return gr.update(visible=True)
# Function to handle tool removal confirmation
def remove_selected_tool(tool_details):
print(f"Removing tool: {tool_details}")
if not tool_details:
print("No tool details provided")
return [
gr.update(visible=False), # confirmation_dialog
gr.update(visible=True), # installation_result_container
"⚠️ Removal Failed", # installation_status
"No tool selected to remove." # installation_result
]
try:
# Remove the tool
result = self.remove_tool(tool_details['id'])
if "Error" in result:
return [
gr.update(visible=False), # confirmation_dialog
gr.update(visible=True), # installation_result_container
"⚠️ Removal Failed", # installation_status
result # installation_result
]
else:
# After successful removal, show success message
# The user will need to click "Back to Marketplace" manually
# This avoids issues with dynamic UI updates
return [
gr.update(visible=False), # confirmation_dialog
gr.update(visible=True), # installation_result_container
"✅ Removal Successful", # installation_status
f"{result} - Click 'Back to Marketplace' to return. You may need to restart the application to see all changes." # installation_result
]
except Exception as e:
print(f"Error removing tool: {e}")
return [
gr.update(visible=False), # confirmation_dialog
gr.update(visible=True), # installation_result_container
"⚠️ Removal Failed", # installation_status
f"Error removing tool: {e}" # installation_result
]
# Function to cancel removal
def cancel_removal():
return gr.update(visible=False)
debug_output = gr.Textbox(label="Debug Output", visible=False)
# Set up event handlers for buttons
install_btn.click(
fn=install_selected_tool,
inputs=[current_tool_details_state],
outputs=[installation_result_container, installation_status, installation_result]
)
remove_btn.click(fn=show_confirmation_dialog, outputs=confirmation_dialog)
# Set up confirmation dialog buttons
cancel_btn.click(fn=cancel_removal, outputs=confirmation_dialog)
confirm_btn.click(
fn=remove_selected_tool,
inputs=[current_tool_details_state],
outputs=[confirmation_dialog, installation_result_container, installation_status, installation_result]
)
# Fill settings content
with settings_content:
gr.Markdown("Settings", elem_classes="page-title")
gr.Markdown("Settings options will appear here...")
# Fill tool interfaces
with tool_interfaces:
# Add an HTML component to display messages for newly installed tools
tool_interfaces_html = gr.HTML("", elem_id="tool-interfaces-html")
for tool_id, tool in self.tools.items():
with gr.Column(visible=False) as tool_container:
tool_ui = tool.ui_class()
tool_ui.create_ui()
# Store the UI container in the tool object for later reference
tool.ui_container = tool_container
# Set up event handlers after all components are defined
# Set up tool button events
# Collect all tool containers for outputs
tool_containers = []
for tool_id, tool in self.tools.items():
if hasattr(tool, 'ui_container'):
tool_containers.append(tool.ui_container)
# Set up click handlers for marketplace tool buttons
for i, (tool_id, tool_btn) in enumerate(marketplace_tool_buttons):
# We need to create a separate function for each button to avoid lambda closure issues
def make_handler(tid):
return lambda: show_tool_details(tid)
handler = make_handler(tool_id)
tool_btn.click(
fn=handler,
outputs=[
tools_content, marketplace_content, settings_content, tool_interfaces,
tool_details_content, tools_btn, marketplace_btn, settings_btn,
current_tool_details_state, tool_details_html, installation_result_container,
confirmation_dialog, tool_interfaces_html
] + tool_containers
)
for i, btn in enumerate(tool_buttons):
if isinstance(btn, gr.Button): # Check if it's a button from the tools grid
tool_id = tool_ids[i] # Get the corresponding tool_id
# We need to create a separate function for each button to avoid lambda closure issues
def make_handler(tid):
return lambda: show_tool(tid)
handler = make_handler(tool_id)
btn.click(
fn=handler,
outputs=[tools_content, marketplace_content, settings_content, tool_interfaces,
tool_details_content, tools_btn, marketplace_btn, settings_btn,
tool_interfaces_html] + tool_containers
)
# Set up navigation events
tools_btn.click(
fn=show_tools,
outputs=[tools_content, marketplace_content, settings_content, tool_interfaces,
tool_details_content, tools_btn, marketplace_btn, settings_btn,
tools_html_component, tool_interfaces_html] + tool_containers
)
marketplace_btn.click(
fn=show_marketplace,
outputs=[tools_content, marketplace_content, settings_content, tool_interfaces,
tool_details_content, tools_btn, marketplace_btn, settings_btn,
marketplace_tools_state, marketplace_html, installation_result_container,
confirmation_dialog, tool_interfaces_html] + tool_containers
)
settings_btn.click(
fn=show_settings,
outputs=[tools_content, marketplace_content, settings_content, tool_interfaces,
tool_details_content, tools_btn, marketplace_btn, settings_btn,
tool_interfaces_html] + tool_containers
)
# Set up add tool button to navigate to marketplace
add_tool_btn.click(
fn=show_marketplace,
outputs=[tools_content, marketplace_content, settings_content, tool_interfaces,
tool_details_content, tools_btn, marketplace_btn, settings_btn,
marketplace_tools_state, marketplace_html, installation_result_container,
confirmation_dialog, tool_interfaces_html] + tool_containers
)
# Set up event handlers for tool details
back_btn.click(
fn=show_marketplace,
outputs=[
tools_content, marketplace_content, settings_content, tool_interfaces,
tool_details_content, tools_btn, marketplace_btn, settings_btn,
marketplace_tools_state, marketplace_html, installation_result_container,
confirmation_dialog
] + tool_containers
)
# Define a function to handle closing the installation result and navigating to tools
def close_result_and_show_tools():
# First hide the installation result container
installation_result_update = gr.update(visible=False)
# Then get the updates from show_tools
tools_updates = show_tools()
# Combine the updates
return [installation_result_update] + tools_updates
# Set up close button for installation result
close_result_btn.click(
fn=close_result_and_show_tools,
outputs=[installation_result_container,
tools_content, marketplace_content, settings_content, tool_interfaces,
tool_details_content, tools_btn, marketplace_btn, settings_btn,
tools_html_component, tool_interfaces_html] + tool_containers
)
return app
if __name__ == "__main__":
ui = MainUI()
demo = ui.create_ui()
demo.queue() # Add queue for better handling of multiple requests
demo.title = "AI Tools Platform" # Add a title
demo.launch(
debug=True,
share=True,
server_name="0.0.0.0",
server_port=7860
)