# filename: request_router.py import gradio as gr import os import importlib.util from typing import Any, Dict import requests # This is the only hardcoded Tool endpoint def register_tool(tool_data): # Send a POST request to register the tool response = requests.post("https://huggingface.co/chat/tools/new", json=tool_data) return response.json() def create_tool(tool_name, tool_data): """One tool to create them all. a AI model can wield this toolto create a new 'tool' script in the tools folder besides the values for the registration payload, the 'tool_data' should also be a string value 'tfn' which contains the code for the tool function script """ print("processing create_tool ") print(tool_name) print(dir(tool_data)) # set the payload for restration tool_payload = { "displayName": tool_data.get("displayName"), # str display name of the tool "description": tool_data.get("description"), # str dscription "color": tool_data.get("color"), # Example color purple,red,green "icon": tool_data.get("icon"), # image binairy "baseUrl": "K00B404/toolshed", # always to the toolshed "endpoint": "/router", # always to the router "name": tool_data.get("name"), # str name for matching function "inputs":tool_data.get("inputs"), # List(str) list the tools input values "outputComponent": tool_data.get("outputComponent"), # Adjust based on your tool's output "outputComponentIdx": tool_data.get("outputComponentIdx"), # Adjust based on your tool's output "showOutput": tool_data.get("showOutput") # bool } print(dir(tool_payload)) # write the new tool script from tfn with open(f'./tools/{tool_data.get("name")}.py' 'w') as f: f.write(tool_data.get("tfn")) # send the registation request for the tool return register_tool(tool_payload) # Define the path to the tools folder TOOLS_DIR = './tools' # Function to dynamically load methods from files in the ./tools folder def load_methods_from_tools(): method_mapping = {} # List all .py files in the tools directory for filename in os.listdir(TOOLS_DIR): if filename.endswith('.py'): method_name = filename[:-3].lower() # Remove '.py' and convert to lowercase # Load the module module_path = os.path.join(TOOLS_DIR, filename) spec = importlib.util.spec_from_file_location(method_name, module_path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) # Map all functions from the module to the method mapping for attr in dir(module): if callable(getattr(module, attr)) and not attr.startswith("__"): method_mapping[method_name] = getattr(module, attr) return method_mapping # Load the method mapping at the start method_mapping = load_methods_from_tools() # Main request router method def request_router(name: str, input_data: Dict[str, Any]) -> Dict[str, Any]: # Retrieve the appropriate method based on the name method = method_mapping.get(name) if method is None: return {"error": "Method not found"} # Call the method with the provided input data try: output = method(input_data) return output except Exception as e: return {"error": str(e)} # Gradio Interface def launch_gradio_app(): with gr.Blocks() as demo: gr.Markdown("# Request Router API") with gr.Row(): name_input = gr.Textbox(label="Method Name") input_data_input = gr.Textbox(label="Input Data (JSON format)") output_output = gr.JSON(label="Output") submit_button = gr.Button("Submit") submit_button.click( fn=lambda name, input_data: request_router(name, eval(input_data)), inputs=[name_input, input_data_input], outputs=output_output ) demo.launch() if __name__ == "__main__": launch_gradio_app()