Vasanth M commited on
Commit
e51595f
·
1 Parent(s): 9b601cc
Files changed (1) hide show
  1. app.py +17 -9
app.py CHANGED
@@ -1,16 +1,23 @@
1
  import gradio as gr
2
 
3
- def convert_temperature(temp, unit):
4
  """
5
- Convert a temperature value between Celsius, Fahrenheit, and Kelvin.
 
 
6
 
7
  Args:
8
- temp (str or float): The temperature value to convert.
9
- unit (str): The unit of the input temperature ('Celsius', 'Fahrenheit', or 'Kelvin').
10
 
11
  Returns:
12
- tuple: A tuple containing three strings with the temperature in Celsius, Fahrenheit, and Kelvin,
13
- formatted to two decimal places. If input is invalid, returns an error message.
 
 
 
 
 
14
  """
15
  try:
16
  temp = float(temp)
@@ -29,7 +36,7 @@ def convert_temperature(temp, unit):
29
 
30
  return (f"{celsius:.2f} °C", f"{fahrenheit:.2f} °F", f"{kelvin:.2f} K")
31
  except ValueError:
32
- return "Please enter a valid number"
33
 
34
  # Define the Gradio interface
35
  with gr.Blocks() as app:
@@ -51,8 +58,9 @@ with gr.Blocks() as app:
51
  convert_button.click(
52
  fn=convert_temperature,
53
  inputs=[temp_input, unit_dropdown],
54
- outputs=[celsius_output, fahrenheit_output, kelvin_output]
 
55
  )
56
 
57
- # Launch the app with MCP setting
58
  app.launch(mcp_server=True)
 
1
  import gradio as gr
2
 
3
+ def convert_temperature(temp: str, unit: str) -> tuple[str, str, str]:
4
  """
5
+ Converts a temperature value from one unit to Celsius, Fahrenheit, and Kelvin.
6
+
7
+ This function takes a temperature value as a string and its unit, performs the conversion, and returns the equivalent temperatures in all three units as formatted strings. Designed for use in a Gradio interface and as an MCP tool.
8
 
9
  Args:
10
+ temp (str): The temperature value as a string (e.g., '25', '-10.5'). Must be convertible to a float.
11
+ unit (str): The unit of the input temperature. Must be one of 'Celsius', 'Fahrenheit', or 'Kelvin'.
12
 
13
  Returns:
14
+ tuple[str, str, str]: A tuple containing three strings:
15
+ - Temperature in Celsius (e.g., '25.00 °C') or 'Invalid' if input is invalid.
16
+ - Temperature in Fahrenheit (e.g., '77.00 °F') or 'Invalid' if input is invalid.
17
+ - Temperature in Kelvin (e.g., '298.15 K') or 'Invalid' if input is invalid.
18
+
19
+ Raises:
20
+ ValueError: If the temp input cannot be converted to a float.
21
  """
22
  try:
23
  temp = float(temp)
 
36
 
37
  return (f"{celsius:.2f} °C", f"{fahrenheit:.2f} °F", f"{kelvin:.2f} K")
38
  except ValueError:
39
+ return ("Invalid", "Invalid", "Invalid")
40
 
41
  # Define the Gradio interface
42
  with gr.Blocks() as app:
 
58
  convert_button.click(
59
  fn=convert_temperature,
60
  inputs=[temp_input, unit_dropdown],
61
+ outputs=[celsius_output, fahrenheit_output, kelvin_output],
62
+ api_name="/convert_temperature"
63
  )
64
 
65
+ # Launch the app with MCP server enabled
66
  app.launch(mcp_server=True)