File size: 4,990 Bytes
a4cd24c
 
 
c453d15
 
a248d68
2dada65
a248d68
2dada65
9d23ee3
 
a248d68
9d23ee3
2dada65
 
 
 
 
 
e7bfe63
a248d68
 
 
070ef9a
9d23ee3
a248d68
 
 
2dada65
a248d68
 
 
 
 
 
 
 
 
 
 
2dada65
 
a248d68
 
 
 
 
2dada65
 
c453d15
 
 
a248d68
 
 
 
c453d15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4cd24c
 
5e6975b
a248d68
 
 
 
a4cd24c
 
 
 
c453d15
a4cd24c
c453d15
 
 
 
 
5e6975b
 
 
 
a4cd24c
5e6975b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4cd24c
 
 
 
 
 
 
 
 
 
 
a248d68
a4cd24c
 
 
 
 
 
5e6975b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4cd24c
a248d68
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# filename: request_router.py

import gradio as gr
import os
import importlib.util
import json
import requests
from typing import Any, Dict

# Define the path to the tools folder
TOOLS_DIR = './tools'
os.makedirs(TOOLS_DIR, exist_ok=True)

# 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_data: Dict[str, Any]):
    """Create a new tool script in the tools folder.
    
    Args:
        tool_data: Dictionary containing registration payload values and tool function script.
    """
    print("Processing create_tool")
    
    # Prepare the payload for registration
    tool_payload = {
        "displayName": tool_data.get("displayName"),
        "description": tool_data.get("description"),
        "color": tool_data.get("color"),
        "icon": tool_data.get("icon"),
        "baseUrl": "K00B404/toolshed",
        "endpoint": "/router",
        "name": tool_data.get("name"),
        "inputs": tool_data.get("inputs"),
        "outputComponent": tool_data.get("outputComponent"),
        "outputComponentIdx": tool_data.get("outputComponentIdx"),
        "showOutput": tool_data.get("showOutput"),
    }
    
    # Write the new tool script from the 'tfn' (tool function name) data
    with open(f'{TOOLS_DIR}/{tool_data.get("name").lower()}.py', 'w') as f:
        f.write(tool_data.get("tfn"))
    
    # Register the new tool
    return register_tool(tool_payload)

# Function to dynamically load methods from files in the ./tools folder
def load_methods_from_tools():
    method_mapping = {}

    # Manually map the create_tool function
    method_mapping["create_tool"] = create_tool

    # 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_routerOld(name: str, input_data: Dict[str, Any]) -> Dict[str, Any]:
    # Reload methods to include newly created tools
    global method_mapping
    method_mapping = load_methods_from_tools()

    # 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)}
        
def request_router(name: str, input_data: Dict[str, Any]) -> Dict[str, Any]:
    global method_mapping
    method_mapping = load_methods_from_tools()

    logging.info(f"Request to invoke method: {name} with input: {input_data}")
    method = method_mapping.get(name)
    if method is None:
        error_message = "Method not found"
        logging.error(error_message)
        return {"error": error_message}
    
    try:
        output = method(input_data)
        logging.info(f"Output: {output}")
        return output
    except Exception as e:
        logging.error(f"Error occurred: {str(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, json.loads(input_data)),
            inputs=[name_input, input_data_input],
            outputs=output_output
        )

    demo.launch()

import logging

# Set up logging configuration
logging.basicConfig(level=logging.INFO, filename='request_logs.txt', 
                    format='%(asctime)s - %(message)s')

def log_payload(*args, **kwargs):
    # Log the incoming payload
    logging.info(f"Received payload: args={args}, kwargs={kwargs}")
    return main_function(*args, **kwargs)

def main_function(name):
    return "Hello " + name + "!!"




if __name__ == "__main__":
    launch_gradio_app()