Update app.py
Browse files
app.py
CHANGED
@@ -3,12 +3,13 @@
|
|
3 |
import gradio as gr
|
4 |
import os
|
5 |
import importlib.util
|
6 |
-
|
7 |
import requests
|
|
|
8 |
|
9 |
# Define the path to the tools folder
|
10 |
TOOLS_DIR = './tools'
|
11 |
-
os.makedirs(TOOLS_DIR,
|
12 |
|
13 |
# This is the only hardcoded Tool endpoint
|
14 |
def register_tool(tool_data):
|
@@ -16,43 +17,44 @@ def register_tool(tool_data):
|
|
16 |
response = requests.post("https://huggingface.co/chat/tools/new", json=tool_data)
|
17 |
return response.json()
|
18 |
|
19 |
-
def create_tool(tool_name, tool_data):
|
20 |
-
"""
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
24 |
"""
|
25 |
-
print("
|
26 |
-
|
27 |
-
|
28 |
-
# set the payload for restration
|
29 |
tool_payload = {
|
30 |
-
"displayName": tool_data.get("displayName"),
|
31 |
-
"description": tool_data.get("description"),
|
32 |
-
"color": tool_data.get("color"),
|
33 |
-
"icon": tool_data.get("icon"),
|
34 |
-
"baseUrl": "K00B404/toolshed",
|
35 |
-
"endpoint": "/router",
|
36 |
-
"name": tool_data.get("name"),
|
37 |
-
"inputs":tool_data.get("inputs"),
|
38 |
-
"outputComponent": tool_data.get("outputComponent"),
|
39 |
-
"outputComponentIdx": tool_data.get("outputComponentIdx"),
|
40 |
-
"showOutput": tool_data.get("showOutput")
|
41 |
}
|
42 |
-
print(dir(tool_payload))
|
43 |
|
44 |
-
#
|
45 |
-
with open(f'{TOOLS_DIR}/{tool_data.get("name")}.py' 'w') as f:
|
46 |
-
|
47 |
-
|
|
|
48 |
return register_tool(tool_payload)
|
49 |
|
50 |
-
|
51 |
-
|
52 |
# Function to dynamically load methods from files in the ./tools folder
|
53 |
def load_methods_from_tools():
|
54 |
method_mapping = {}
|
55 |
-
|
|
|
|
|
|
|
56 |
# List all .py files in the tools directory
|
57 |
for filename in os.listdir(TOOLS_DIR):
|
58 |
if filename.endswith('.py'):
|
@@ -76,6 +78,10 @@ method_mapping = load_methods_from_tools()
|
|
76 |
|
77 |
# Main request router method
|
78 |
def request_router(name: str, input_data: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
|
|
|
|
|
|
79 |
# Retrieve the appropriate method based on the name
|
80 |
method = method_mapping.get(name)
|
81 |
if method is None:
|
@@ -99,7 +105,7 @@ def launch_gradio_app():
|
|
99 |
|
100 |
submit_button = gr.Button("Submit")
|
101 |
submit_button.click(
|
102 |
-
fn=lambda name, input_data: request_router(name,
|
103 |
inputs=[name_input, input_data_input],
|
104 |
outputs=output_output
|
105 |
)
|
@@ -107,4 +113,4 @@ def launch_gradio_app():
|
|
107 |
demo.launch()
|
108 |
|
109 |
if __name__ == "__main__":
|
110 |
-
launch_gradio_app()
|
|
|
3 |
import gradio as gr
|
4 |
import os
|
5 |
import importlib.util
|
6 |
+
import json
|
7 |
import requests
|
8 |
+
from typing import Any, Dict
|
9 |
|
10 |
# Define the path to the tools folder
|
11 |
TOOLS_DIR = './tools'
|
12 |
+
os.makedirs(TOOLS_DIR, exist_ok=True)
|
13 |
|
14 |
# This is the only hardcoded Tool endpoint
|
15 |
def register_tool(tool_data):
|
|
|
17 |
response = requests.post("https://huggingface.co/chat/tools/new", json=tool_data)
|
18 |
return response.json()
|
19 |
|
20 |
+
def create_tool(tool_name: str, tool_data: Dict[str, Any]):
|
21 |
+
"""Create a new tool script in the tools folder.
|
22 |
+
|
23 |
+
Args:
|
24 |
+
tool_name: Name for the tool (also the filename).
|
25 |
+
tool_data: Dictionary containing registration payload and function script.
|
26 |
"""
|
27 |
+
print("Processing create_tool")
|
28 |
+
|
29 |
+
# Prepare the payload for registration
|
|
|
30 |
tool_payload = {
|
31 |
+
"displayName": tool_data.get("displayName"),
|
32 |
+
"description": tool_data.get("description"),
|
33 |
+
"color": tool_data.get("color"),
|
34 |
+
"icon": tool_data.get("icon"),
|
35 |
+
"baseUrl": "K00B404/toolshed",
|
36 |
+
"endpoint": "/router",
|
37 |
+
"name": tool_data.get("name"),
|
38 |
+
"inputs": tool_data.get("inputs"),
|
39 |
+
"outputComponent": tool_data.get("outputComponent"),
|
40 |
+
"outputComponentIdx": tool_data.get("outputComponentIdx"),
|
41 |
+
"showOutput": tool_data.get("showOutput"),
|
42 |
}
|
|
|
43 |
|
44 |
+
# Write the new tool script from the 'tfn' (tool function name) data
|
45 |
+
with open(f'{TOOLS_DIR}/{tool_data.get("name").lower()}.py', 'w') as f:
|
46 |
+
f.write(tool_data.get("tfn"))
|
47 |
+
|
48 |
+
# Register the new tool
|
49 |
return register_tool(tool_payload)
|
50 |
|
|
|
|
|
51 |
# Function to dynamically load methods from files in the ./tools folder
|
52 |
def load_methods_from_tools():
|
53 |
method_mapping = {}
|
54 |
+
|
55 |
+
# Manually map the create_tool function
|
56 |
+
method_mapping["create_tool"] = create_tool
|
57 |
+
|
58 |
# List all .py files in the tools directory
|
59 |
for filename in os.listdir(TOOLS_DIR):
|
60 |
if filename.endswith('.py'):
|
|
|
78 |
|
79 |
# Main request router method
|
80 |
def request_router(name: str, input_data: Dict[str, Any]) -> Dict[str, Any]:
|
81 |
+
# Reload methods to include newly created tools
|
82 |
+
global method_mapping
|
83 |
+
method_mapping = load_methods_from_tools()
|
84 |
+
|
85 |
# Retrieve the appropriate method based on the name
|
86 |
method = method_mapping.get(name)
|
87 |
if method is None:
|
|
|
105 |
|
106 |
submit_button = gr.Button("Submit")
|
107 |
submit_button.click(
|
108 |
+
fn=lambda name, input_data: request_router(name, json.loads(input_data)),
|
109 |
inputs=[name_input, input_data_input],
|
110 |
outputs=output_output
|
111 |
)
|
|
|
113 |
demo.launch()
|
114 |
|
115 |
if __name__ == "__main__":
|
116 |
+
launch_gradio_app()
|