Vasanth M commited on
Commit
87eaaae
·
1 Parent(s): e51595f
Files changed (1) hide show
  1. app.py +14 -9
app.py CHANGED
@@ -1,20 +1,20 @@
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.
@@ -34,9 +34,13 @@ def convert_temperature(temp: str, unit: str) -> tuple[str, str, str]:
34
  fahrenheit = (celsius * 9/5) + 32
35
  kelvin = 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:
@@ -59,7 +63,8 @@ with gr.Blocks() as app:
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
 
1
  import gradio as gr
2
 
3
+ def convert_temperature(temp: str, unit: str) -> dict[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 in a dictionary. 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
+ dict[str, str]: A dictionary with three keys:
15
+ - 'celsius': Temperature in Celsius (e.g., '25.00 °C') or 'Invalid' if input is invalid.
16
+ - 'fahrenheit': Temperature in Fahrenheit (e.g., '77.00 °F') or 'Invalid' if input is invalid.
17
+ - 'kelvin': 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.
 
34
  fahrenheit = (celsius * 9/5) + 32
35
  kelvin = temp
36
 
37
+ return {
38
+ "celsius": f"{celsius:.2f} °C",
39
+ "fahrenheit": f"{fahrenheit:.2f} °F",
40
+ "kelvin": f"{kelvin:.2f} K"
41
+ }
42
  except ValueError:
43
+ return {"celsius": "Invalid", "fahrenheit": "Invalid", "kelvin": "Invalid"}
44
 
45
  # Define the Gradio interface
46
  with gr.Blocks() as app:
 
63
  fn=convert_temperature,
64
  inputs=[temp_input, unit_dropdown],
65
  outputs=[celsius_output, fahrenheit_output, kelvin_output],
66
+ api_name="/convert_temperature",
67
+ _js="result => [result.celsius, result.fahrenheit, result.kelvin]"
68
  )
69
 
70
  # Launch the app with MCP server enabled