test_mcp / app.py
Vasanth M
Add docstr
9b601cc
raw
history blame
1.97 kB
import gradio as gr
def convert_temperature(temp, unit):
"""
Convert a temperature value between Celsius, Fahrenheit, and Kelvin.
Args:
temp (str or float): The temperature value to convert.
unit (str): The unit of the input temperature ('Celsius', 'Fahrenheit', or 'Kelvin').
Returns:
tuple: A tuple containing three strings with the temperature in Celsius, Fahrenheit, and Kelvin,
formatted to two decimal places. If input is invalid, returns an error message.
"""
try:
temp = float(temp)
if unit == "Celsius":
celsius = temp
fahrenheit = (temp * 9/5) + 32
kelvin = temp + 273.15
elif unit == "Fahrenheit":
celsius = (temp - 32) * 5/9
fahrenheit = temp
kelvin = celsius + 273.15
else: # Kelvin
celsius = temp - 273.15
fahrenheit = (celsius * 9/5) + 32
kelvin = temp
return (f"{celsius:.2f} °C", f"{fahrenheit:.2f} °F", f"{kelvin:.2f} K")
except ValueError:
return "Please enter a valid number"
# Define the Gradio interface
with gr.Blocks() as app:
gr.Markdown("# Temperature Converter")
with gr.Row():
temp_input = gr.Textbox(label="Enter Temperature", placeholder="e.g., 25")
unit_dropdown = gr.Dropdown(
choices=["Celsius", "Fahrenheit", "Kelvin"],
label="Select Unit",
value="Celsius"
)
convert_button = gr.Button("Convert")
with gr.Row():
celsius_output = gr.Textbox(label="Celsius")
fahrenheit_output = gr.Textbox(label="Fahrenheit")
kelvin_output = gr.Textbox(label="Kelvin")
convert_button.click(
fn=convert_temperature,
inputs=[temp_input, unit_dropdown],
outputs=[celsius_output, fahrenheit_output, kelvin_output]
)
# Launch the app with MCP setting
app.launch(mcp_server=True)