Update app.py
Browse files
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(
|
44 |
-
|
45 |
-
"displayName":
|
46 |
-
"description": description,
|
47 |
-
"color": "
|
48 |
-
"icon": "
|
49 |
"baseUrl": "K00B404/toolshed",
|
50 |
-
"endpoint": "/
|
51 |
-
"name": "
|
52 |
"inputs": inputs,
|
53 |
-
"outputComponent": "
|
54 |
-
"outputComponentIdx":
|
55 |
-
"showOutput":
|
56 |
}
|
57 |
-
return register_tool(
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
|
63 |
demo = gr.Interface(
|
64 |
-
fn=
|
65 |
-
inputs=
|
66 |
-
outputs=
|
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)
|