Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# Conversion logic
|
| 4 |
+
def convert_units(category, from_unit, to_unit, value):
|
| 5 |
+
try:
|
| 6 |
+
value = float(value)
|
| 7 |
+
except:
|
| 8 |
+
return "Invalid input value"
|
| 9 |
+
|
| 10 |
+
conversions = {
|
| 11 |
+
"Length": {
|
| 12 |
+
"units": ["Meters", "Kilometers", "Feet", "Miles"],
|
| 13 |
+
"factors": {
|
| 14 |
+
"Meters": 1,
|
| 15 |
+
"Kilometers": 1000,
|
| 16 |
+
"Feet": 0.3048,
|
| 17 |
+
"Miles": 1609.34
|
| 18 |
+
}
|
| 19 |
+
},
|
| 20 |
+
"Weight": {
|
| 21 |
+
"units": ["Grams", "Kilograms", "Pounds", "Ounces"],
|
| 22 |
+
"factors": {
|
| 23 |
+
"Grams": 1,
|
| 24 |
+
"Kilograms": 1000,
|
| 25 |
+
"Pounds": 453.592,
|
| 26 |
+
"Ounces": 28.3495
|
| 27 |
+
}
|
| 28 |
+
},
|
| 29 |
+
"Temperature": {
|
| 30 |
+
"units": ["Celsius", "Fahrenheit", "Kelvin"]
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
if category == "Temperature":
|
| 35 |
+
if from_unit == to_unit:
|
| 36 |
+
return value
|
| 37 |
+
if from_unit == "Celsius":
|
| 38 |
+
if to_unit == "Fahrenheit":
|
| 39 |
+
return (value * 9/5) + 32
|
| 40 |
+
elif to_unit == "Kelvin":
|
| 41 |
+
return value + 273.15
|
| 42 |
+
elif from_unit == "Fahrenheit":
|
| 43 |
+
if to_unit == "Celsius":
|
| 44 |
+
return (value - 32) * 5/9
|
| 45 |
+
elif to_unit == "Kelvin":
|
| 46 |
+
return (value - 32) * 5/9 + 273.15
|
| 47 |
+
elif from_unit == "Kelvin":
|
| 48 |
+
if to_unit == "Celsius":
|
| 49 |
+
return value - 273.15
|
| 50 |
+
elif to_unit == "Fahrenheit":
|
| 51 |
+
return (value - 273.15) * 9/5 + 32
|
| 52 |
+
else:
|
| 53 |
+
base_value = value * conversions[category]["factors"][from_unit]
|
| 54 |
+
return base_value / conversions[category]["factors"][to_unit]
|
| 55 |
+
|
| 56 |
+
# UI layout
|
| 57 |
+
categories = ["Length", "Weight", "Temperature"]
|
| 58 |
+
units = {
|
| 59 |
+
"Length": ["Meters", "Kilometers", "Feet", "Miles"],
|
| 60 |
+
"Weight": ["Grams", "Kilograms", "Pounds", "Ounces"],
|
| 61 |
+
"Temperature": ["Celsius", "Fahrenheit", "Kelvin"]
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
def unit_converter_app(category, from_unit, to_unit, value):
|
| 65 |
+
result = convert_units(category, from_unit, to_unit, value)
|
| 66 |
+
return f"{value} {from_unit} = {result:.4f} {to_unit}"
|
| 67 |
+
|
| 68 |
+
with gr.Blocks() as demo:
|
| 69 |
+
gr.Markdown("# 🔁 Unit Converter App")
|
| 70 |
+
|
| 71 |
+
category_input = gr.Dropdown(choices=categories, label="Category", value="Length")
|
| 72 |
+
from_unit_input = gr.Dropdown(choices=units["Length"], label="From Unit")
|
| 73 |
+
to_unit_input = gr.Dropdown(choices=units["Length"], label="To Unit")
|
| 74 |
+
value_input = gr.Textbox(label="Enter value")
|
| 75 |
+
|
| 76 |
+
output = gr.Textbox(label="Result")
|
| 77 |
+
|
| 78 |
+
def update_units(category):
|
| 79 |
+
return gr.update(choices=units[category], value=units[category][0]), gr.update(choices=units[category], value=units[category][1])
|
| 80 |
+
|
| 81 |
+
category_input.change(fn=update_units, inputs=category_input, outputs=[from_unit_input, to_unit_input])
|
| 82 |
+
|
| 83 |
+
convert_button = gr.Button("Convert")
|
| 84 |
+
convert_button.click(fn=unit_converter_app, inputs=[category_input, from_unit_input, to_unit_input, value_input], outputs=output)
|
| 85 |
+
|
| 86 |
+
demo.launch()
|