Gianpaolo Macario
feat(app): Add markdown front matter
7c8381c
raw
history blame
4.82 kB
import gradio as gr
import requests
def greet(name: str) -> str:
"""
Greets the user with a personalized hello message.
Args:
name (str): The name of the user to greet.
Returns:
str: A greeting message addressed to the specified user.
"""
if not name:
return "Hello stranger!!"
name = name.strip()
if len(name) > 50:
return "Name is too long!"
return "Hello " + name + "!!"
def get_dad_joke():
"""
Fetches a random dad joke from the icanhazdadjoke.com API.
The function makes an HTTP GET request to the <https://icanhazdadjoke.com/> API
with appropriate headers to receive JSON response.
Args:
None
Returns:
str: A random dad joke if successful, otherwise returns an error message.
- Success: Returns the joke text
- Failure: Returns "Failed to retrieve a joke." if the API request fails
- No joke: Returns "No joke found." if the API response doesn't contain a joke
Requires:
requests: The requests library must be imported to make HTTP requests
"""
headers = {"Accept": "application/json"}
response = requests.get("https://icanhazdadjoke.com/", headers=headers)
if response.status_code == 200:
data = response.json()
return data.get("joke", "No joke found.")
else:
return "Failed to retrieve a joke."
def calculate(n1, op, n2):
"""
Performs basic arithmetic operations between two numbers.
This function takes two numbers and an operator as input and returns the result
of the arithmetic operation. Supported operations are addition (+), subtraction (-),
multiplication (*), and division (/). For division, checks for division by zero.
Args:
n1 (float): First number
op (str): Operator ('+', '-', '*', or '/')
n2 (float): Second number
Returns:
float: Result of the arithmetic operation if valid
str: "Error" if invalid operation (like division by zero)
Examples:
>>> calculate(10, "+", 5)
15
>>> calculate(10, "/", 0)
"Error"
"""
if op == "+": return str(n1 + n2)
if op == "-": return str(n1 - n2)
if op == "*": return str(n1 * n2)
if op == "/" and n2 != 0: return str(n1 / n2)
return "Error"
def generate_image(prompt: str):
"""
Generates an image based on a text prompt using the Stable Diffusion model.
Args:
prompt (str): The text prompt to generate the image from.
Returns:
str: The URL of the generated image.
"""
# Call the Stable Diffusion API or model here
# For demonstration, we'll return a placeholder image URL
return "https://avatars.githubusercontent.com/u/75182?v=4"
with gr.Blocks() as demo:
gr.Markdown(
"""## 馃МCalculator and other useful tools
### Built from the ground up for LLMs
This app provides a simple calculator, a dad joke generator, a greeting function, and an image generation feature.
Use via API or MCP 馃殌 路 [Powered by Modal](https://modal.com/) 路 [Built with Gradio](https://www.gradio.app/) 馃煣
"""
)
tab_calc = gr.Tab("Calculator")
with tab_calc:
with gr.Row():
num1 = gr.Number(label="First Number")
operation = gr.Dropdown(
choices=["+", "-", "*", "/"],
label="Operation"
)
num2 = gr.Number(label="Second Number")
calc_btn = gr.Button("Calculate")
calc_output = gr.Text(label="Result")
calc_btn.click(
fn=calculate,
inputs=[num1, operation, num2],
outputs=calc_output,
api_name="calculate")
tab_joke = gr.Tab("Dad Joke")
with tab_joke:
joke_btn = gr.Button("Get Dad Joke")
joke_output = gr.Textbox(label="Dad Joke")
joke_btn.click(fn=get_dad_joke, outputs=joke_output, api_name="get_dad_joke")
tab_greet = gr.Tab("Greet")
with tab_greet:
name = gr.Textbox(label="Type your name", placeholder="Enter your name here")
greet_btn = gr.Button("Greet")
output = gr.Textbox(label="Message")
greet_btn.click(fn=greet, inputs=name, outputs=output, api_name="greet")
# Generate an image
tab_image = gr.Tab("Image Generation")
with tab_image:
img_prompt = gr.Textbox(
label="Image Prompt",
placeholder="Enter a prompt for the image generation"
)
img_btn = gr.Button("Generate Image")
img_output = gr.Image(label="Sample Image")
img_btn.click(fn=generate_image, inputs=img_prompt, outputs=img_output, api_name="generate_image")
demo.launch(mcp_server=True, share=True)