Vasanth M
commited on
Commit
·
a0c24f2
1
Parent(s):
0f9722d
Initla commit
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
def convert_temperature(temp, unit):
|
4 |
+
try:
|
5 |
+
temp = float(temp)
|
6 |
+
if unit == "Celsius":
|
7 |
+
celsius = temp
|
8 |
+
fahrenheit = (temp * 9/5) + 32
|
9 |
+
kelvin = temp + 273.15
|
10 |
+
elif unit == "Fahrenheit":
|
11 |
+
celsius = (temp - 32) * 5/9
|
12 |
+
fahrenheit = temp
|
13 |
+
kelvin = celsius + 273.15
|
14 |
+
else: # Kelvin
|
15 |
+
celsius = temp - 273.15
|
16 |
+
fahrenheit = (celsius * 9/5) + 32
|
17 |
+
kelvin = temp
|
18 |
+
|
19 |
+
return (f"{celsius:.2f} °C", f"{fahrenheit:.2f} °F", f"{kelvin:.2f} K")
|
20 |
+
except ValueError:
|
21 |
+
return "Please enter a valid number"
|
22 |
+
|
23 |
+
# Define the Gradio interface
|
24 |
+
with gr.Blocks() as app:
|
25 |
+
gr.Markdown("# Temperature Converter")
|
26 |
+
with gr.Row():
|
27 |
+
temp_input = gr.Textbox(label="Enter Temperature", placeholder="e.g., 25")
|
28 |
+
unit_dropdown = gr.Dropdown(
|
29 |
+
choices=["Celsius", "Fahrenheit", "Kelvin"],
|
30 |
+
label="Select Unit",
|
31 |
+
value="Celsius"
|
32 |
+
)
|
33 |
+
convert_button = gr.Button("Convert")
|
34 |
+
|
35 |
+
with gr.Row():
|
36 |
+
celsius_output = gr.Textbox(label="Celsius")
|
37 |
+
fahrenheit_output = gr.Textbox(label="Fahrenheit")
|
38 |
+
kelvin_output = gr.Textbox(label="Kelvin")
|
39 |
+
|
40 |
+
convert_button.click(
|
41 |
+
fn=convert_temperature,
|
42 |
+
inputs=[temp_input, unit_dropdown],
|
43 |
+
outputs=[celsius_output, fahrenheit_output, kelvin_output]
|
44 |
+
)
|
45 |
+
|
46 |
+
# Launch the app with MCP setting
|
47 |
+
app.launch(mcp=True)
|