Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# Conversion logic
|
| 4 |
+
def convert_units(value, from_unit, to_unit, category):
|
| 5 |
+
conversions = {
|
| 6 |
+
"Length": {
|
| 7 |
+
"Meter": 1,
|
| 8 |
+
"Kilometer": 1000,
|
| 9 |
+
"Centimeter": 0.01,
|
| 10 |
+
"Millimeter": 0.001,
|
| 11 |
+
"Mile": 1609.34,
|
| 12 |
+
"Yard": 0.9144,
|
| 13 |
+
"Foot": 0.3048,
|
| 14 |
+
"Inch": 0.0254,
|
| 15 |
+
},
|
| 16 |
+
"Weight": {
|
| 17 |
+
"Kilogram": 1,
|
| 18 |
+
"Gram": 0.001,
|
| 19 |
+
"Milligram": 0.000001,
|
| 20 |
+
"Pound": 0.453592,
|
| 21 |
+
"Ounce": 0.0283495,
|
| 22 |
+
},
|
| 23 |
+
"Temperature": None # handled separately
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
if category == "Temperature":
|
| 27 |
+
if from_unit == to_unit:
|
| 28 |
+
return value
|
| 29 |
+
elif from_unit == "Celsius" and to_unit == "Fahrenheit":
|
| 30 |
+
return (value * 9/5) + 32
|
| 31 |
+
elif from_unit == "Fahrenheit" and to_unit == "Celsius":
|
| 32 |
+
return (value - 32) * 5/9
|
| 33 |
+
elif from_unit == "Celsius" and to_unit == "Kelvin":
|
| 34 |
+
return value + 273.15
|
| 35 |
+
elif from_unit == "Kelvin" and to_unit == "Celsius":
|
| 36 |
+
return value - 273.15
|
| 37 |
+
elif from_unit == "Fahrenheit" and to_unit == "Kelvin":
|
| 38 |
+
return (value - 32) * 5/9 + 273.15
|
| 39 |
+
elif from_unit == "Kelvin" and to_unit == "Fahrenheit":
|
| 40 |
+
return (value - 273.15) * 9/5 + 32
|
| 41 |
+
|
| 42 |
+
else:
|
| 43 |
+
base_value = value * conversions[category][from_unit]
|
| 44 |
+
result = base_value / conversions[category][to_unit]
|
| 45 |
+
return result
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def update_units(category):
|
| 49 |
+
if category == "Length":
|
| 50 |
+
units = ["Meter", "Kilometer", "Centimeter", "Millimeter", "Mile", "Yard", "Foot", "Inch"]
|
| 51 |
+
elif category == "Weight":
|
| 52 |
+
units = ["Kilogram", "Gram", "Milligram", "Pound", "Ounce"]
|
| 53 |
+
elif category == "Temperature":
|
| 54 |
+
units = ["Celsius", "Fahrenheit", "Kelvin"]
|
| 55 |
+
return gr.update(choices=units, value=units[0]), gr.update(choices=units, value=units[1])
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 59 |
+
gr.Markdown("## 🌡️ Universal Unit Converter\nConvert between different unit categories easily!")
|
| 60 |
+
|
| 61 |
+
with gr.Row():
|
| 62 |
+
category = gr.Dropdown(["Length", "Weight", "Temperature"], label="Select Category", value="Length")
|
| 63 |
+
|
| 64 |
+
with gr.Row():
|
| 65 |
+
value = gr.Number(label="Enter Value", value=1)
|
| 66 |
+
|
| 67 |
+
with gr.Row():
|
| 68 |
+
from_unit = gr.Dropdown([], label="From Unit")
|
| 69 |
+
to_unit = gr.Dropdown([], label="To Unit")
|
| 70 |
+
|
| 71 |
+
category.change(fn=update_units, inputs=category, outputs=[from_unit, to_unit])
|
| 72 |
+
|
| 73 |
+
convert_btn = gr.Button("Convert")
|
| 74 |
+
output = gr.Textbox(label="Converted Value")
|
| 75 |
+
|
| 76 |
+
convert_btn.click(fn=convert_units,
|
| 77 |
+
inputs=[value, from_unit, to_unit, category],
|
| 78 |
+
outputs=output)
|
| 79 |
+
|
| 80 |
+
demo.launch()
|