K00B404 commited on
Commit
2dada65
·
verified ·
1 Parent(s): c453d15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py CHANGED
@@ -4,6 +4,45 @@ import gradio as gr
4
  import os
5
  import importlib.util
6
  from typing import Any, Dict
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  # Define the path to the tools folder
9
  TOOLS_DIR = './tools'
 
4
  import os
5
  import importlib.util
6
  from typing import Any, Dict
7
+ import requests
8
+
9
+ # This is the only hardcoded Tool endpoint
10
+ def register_tool(tool_data):
11
+ # Send a POST request to register the tool
12
+ response = requests.post("https://huggingface.co/chat/tools/new", json=tool_data)
13
+ return response.json()
14
+
15
+ def create_tool(tool_name, tool_data):
16
+ """One tool to create them all.
17
+ a AI model can wield this toolto create a new 'tool' script in the tools folder
18
+ besides the values for the registration payload,
19
+ the 'tool_data' should also be a string value 'tfn' which contains the code for the tool function script
20
+ """
21
+ print("processing create_tool ")
22
+ print(tool_name)
23
+ print(dir(tool_data))
24
+ # set the payload for restration
25
+ tool_payload = {
26
+ "displayName": tool_data.get("displayName"), # str display name of the tool
27
+ "description": tool_data.get("description"), # str dscription
28
+ "color": tool_data.get("color"), # Example color purple,red,green
29
+ "icon": tool_data.get("icon"), # image binairy
30
+ "baseUrl": "K00B404/toolshed", # always to the toolshed
31
+ "endpoint": "/router", # always to the router
32
+ "name": tool_data.get("name"), # str name for matching function
33
+ "inputs":tool_data.get("inputs"), # List(str) list the tools input values
34
+ "outputComponent": tool_data.get("outputComponent"), # Adjust based on your tool's output
35
+ "outputComponentIdx": tool_data.get("outputComponentIdx"), # Adjust based on your tool's output
36
+ "showOutput": tool_data.get("showOutput") # bool
37
+ }
38
+ print(dir(tool_payload))
39
+
40
+ # write the new tool script from tfn
41
+ with open(f'./tools/{tool_data.get("name")}.py' 'w') as f:
42
+ f.write(tool_data.get("tfn"))
43
+ # send the registation request for the tool
44
+ return register_tool(tool_payload)
45
+
46
 
47
  # Define the path to the tools folder
48
  TOOLS_DIR = './tools'