K00B404 commited on
Commit
4d8b82c
·
verified ·
1 Parent(s): d56935b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -54
app.py CHANGED
@@ -1,71 +1,35 @@
1
  import gradio as gr
2
  import requests
3
 
4
- def calculator(num1, operation, num2):
5
- if operation == "add":
6
- return num1 + num2
7
- elif operation == "subtract":
8
- return num1 - num2
9
- elif operation == "multiply":
10
- return num1 * num2
11
- elif operation == "divide":
12
- return num1 / num2
13
-
14
- with gr.Blocks() as demo:
15
- with gr.Row():
16
- with gr.Column():
17
- num_1 = gr.Number(value=4)
18
- operation = gr.Radio(["add", "subtract", "multiply", "divide"])
19
- num_2 = gr.Number(value=0)
20
- submit_btn = gr.Button(value="Calculate")
21
- with gr.Column():
22
- result = gr.Number()
23
-
24
- submit_btn.click(
25
- calculator, inputs=[num_1, operation, num_2], outputs=[result], api_name=False
26
- )
27
- examples = gr.Examples(
28
- examples=[
29
- [5, "add", 3],
30
- [4, "divide", 2],
31
- [-4, "multiply", 2.5],
32
- [0, "subtract", 1.2],
33
- ],
34
- inputs=[num_1, operation, num_2],
35
- )
36
-
37
-
38
  def register_tool(tool_data):
39
  # Send a POST request to register the tool
40
  response = requests.post("https://huggingface.co/chat/tools/new", json=tool_data)
41
  return response.json()
42
 
43
- def create_tool(display_name, description, inputs):
44
- tool_data = {
45
- "displayName": display_name,
46
- "description": description,
47
- "color": "yellow", # Example color
48
- "icon": "tools",
49
  "baseUrl": "K00B404/toolshed",
50
- "endpoint": "/query",
51
- "name": "query",
52
  "inputs": inputs,
53
- "outputComponent": "0;image", # Adjust based on your tool's output
54
- "outputComponentIdx": 0,
55
- "showOutput": True
56
  }
57
- return register_tool(tool_data)
58
 
59
- inputs=gr.Textbox(label="Display Name")
60
- outputs=gr.Textbox()
61
- fn=create_tool
62
 
63
  demo = gr.Interface(
64
- fn=fn,
65
- inputs=inputs, # Updated line
66
- outputs=outputs # You can use gr.Textbox or other components as needed
67
  )
68
 
69
-
70
-
71
  demo.launch(show_api=True, share=True)
 
1
  import gradio as gr
2
  import requests
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  def register_tool(tool_data):
5
  # Send a POST request to register the tool
6
  response = requests.post("https://huggingface.co/chat/tools/new", json=tool_data)
7
  return response.json()
8
 
9
+ def create_tool(tool_name, tool_data):
10
+ tool_payload = {
11
+ "displayName": tool_data.get("displayName"),
12
+ "description": tool_data.get("description"),
13
+ "color": tool_data.get("color"), # Example color
14
+ "icon": tool_data.get("icon"),
15
  "baseUrl": "K00B404/toolshed",
16
+ "endpoint": "/router",
17
+ "name": tool_data.get("name"),
18
  "inputs": inputs,
19
+ "outputComponent": tool_data.get("outputComponent"), # Adjust based on your tool's output
20
+ "outputComponentIdx": tool_data.get("outputComponentIdx"),
21
+ "showOutput": tool_data.get("showOutput")
22
  }
23
+ return register_tool(tool_payload)
24
 
25
+ def tool_router(tool_name: str, tool_inputs: dict):
26
+ if tool_name == "create_tool":
27
+ create_tool(tool_name, tool_inputs)
28
 
29
  demo = gr.Interface(
30
+ fn=tool_router,
31
+ inputs=["text", {}],
32
+ outputs=["text", "number"],
33
  )
34
 
 
 
35
  demo.launch(show_api=True, share=True)