|
import gradio as gr |
|
import requests |
|
|
|
def calculator(num1, operation, num2): |
|
if operation == "add": |
|
return num1 + num2 |
|
elif operation == "subtract": |
|
return num1 - num2 |
|
elif operation == "multiply": |
|
return num1 * num2 |
|
elif operation == "divide": |
|
return num1 / num2 |
|
|
|
with gr.Blocks() as demo: |
|
with gr.Row(): |
|
with gr.Column(): |
|
num_1 = gr.Number(value=4) |
|
operation = gr.Radio(["add", "subtract", "multiply", "divide"]) |
|
num_2 = gr.Number(value=0) |
|
submit_btn = gr.Button(value="Calculate") |
|
with gr.Column(): |
|
result = gr.Number() |
|
|
|
submit_btn.click( |
|
calculator, inputs=[num_1, operation, num_2], outputs=[result], api_name=False |
|
) |
|
examples = gr.Examples( |
|
examples=[ |
|
[5, "add", 3], |
|
[4, "divide", 2], |
|
[-4, "multiply", 2.5], |
|
[0, "subtract", 1.2], |
|
], |
|
inputs=[num_1, operation, num_2], |
|
) |
|
|
|
|
|
def register_tool(tool_data): |
|
|
|
response = requests.post("https://huggingface.co/chat/tools/new", json=tool_data) |
|
return response.json() |
|
|
|
def create_tool(display_name, description, inputs): |
|
tool_data = { |
|
"displayName": display_name, |
|
"description": description, |
|
"color": "yellow", |
|
"icon": "tools", |
|
"baseUrl": "K00B404/toolshed", |
|
"endpoint": "/query", |
|
"name": "query", |
|
"inputs": inputs, |
|
"outputComponent": "0;image", |
|
"outputComponentIdx": 0, |
|
"showOutput": True |
|
} |
|
return register_tool(tool_data) |
|
|
|
inputs=gr.Textbox(label="Display Name") |
|
outputs=gr.Textbox() |
|
fn=create_tool |
|
|
|
demo = gr.Interface( |
|
fn=fn, |
|
inputs=inputs, |
|
outputs=outputs |
|
) |
|
|
|
|
|
|
|
demo.launch(show_api=True, share=True) |