Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
app.py - Simple Unit Converter Web App using Gradio
|
| 3 |
+
---------------------------------------------------
|
| 4 |
+
Supports: Mass, Length, Volume, Temperature.
|
| 5 |
+
Beginner-friendly code with inline comments.
|
| 6 |
+
|
| 7 |
+
How to run in Google Colab:
|
| 8 |
+
1. !pip install -r requirements.txt
|
| 9 |
+
2. !python app.py
|
| 10 |
+
3. Click the "share" link generated by Gradio.
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
import gradio as gr
|
| 14 |
+
|
| 15 |
+
# --- STEP 1: Define available units and conversion logic ---
|
| 16 |
+
|
| 17 |
+
# Conversion factors relative to a base unit for each category
|
| 18 |
+
UNIT_MAP = {
|
| 19 |
+
"Mass": { # base = grams
|
| 20 |
+
"kg": 1000.0,
|
| 21 |
+
"g": 1.0,
|
| 22 |
+
"mg": 0.001,
|
| 23 |
+
"lb": 453.592,
|
| 24 |
+
"oz": 28.3495,
|
| 25 |
+
},
|
| 26 |
+
"Length": { # base = meters
|
| 27 |
+
"mm": 0.001,
|
| 28 |
+
"cm": 0.01,
|
| 29 |
+
"m": 1.0,
|
| 30 |
+
"km": 1000.0,
|
| 31 |
+
"in": 0.0254,
|
| 32 |
+
"ft": 0.3048,
|
| 33 |
+
"yd": 0.9144,
|
| 34 |
+
"mi": 1609.34,
|
| 35 |
+
},
|
| 36 |
+
"Volume": { # base = liters
|
| 37 |
+
"mL": 0.001,
|
| 38 |
+
"L": 1.0,
|
| 39 |
+
"m³": 1000.0,
|
| 40 |
+
"cup": 0.24,
|
| 41 |
+
"pint": 0.47,
|
| 42 |
+
"gallon": 3.785,
|
| 43 |
+
},
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
# Special case: Temperature uses formulas (not multipliers)
|
| 47 |
+
TEMP_UNITS = ["C", "F", "K"]
|
| 48 |
+
|
| 49 |
+
# --- STEP 2: Conversion functions ---
|
| 50 |
+
|
| 51 |
+
def convert_temperature(value, from_u, to_u):
|
| 52 |
+
"""Convert temperature between Celsius, Fahrenheit, Kelvin."""
|
| 53 |
+
# convert input to Celsius
|
| 54 |
+
if from_u == "C":
|
| 55 |
+
c = value
|
| 56 |
+
elif from_u == "F":
|
| 57 |
+
c = (value - 32) * 5/9
|
| 58 |
+
elif from_u == "K":
|
| 59 |
+
c = value - 273.15
|
| 60 |
+
else:
|
| 61 |
+
return "Unknown unit"
|
| 62 |
+
|
| 63 |
+
# convert Celsius to target
|
| 64 |
+
if to_u == "C":
|
| 65 |
+
return c
|
| 66 |
+
elif to_u == "F":
|
| 67 |
+
return c * 9/5 + 32
|
| 68 |
+
elif to_u == "K":
|
| 69 |
+
return c + 273.15
|
| 70 |
+
|
| 71 |
+
def convert_linear(value, from_u, to_u, category):
|
| 72 |
+
"""Convert Mass, Length, Volume using base-unit multipliers."""
|
| 73 |
+
mapping = UNIT_MAP[category]
|
| 74 |
+
base_value = value * mapping[from_u] # step 1: to base
|
| 75 |
+
return base_value / mapping[to_u] # step 2: to target
|
| 76 |
+
|
| 77 |
+
def convert(value, from_unit, to_unit, category):
|
| 78 |
+
"""Main function used by Gradio."""
|
| 79 |
+
# --- Input validation ---
|
| 80 |
+
try:
|
| 81 |
+
val = float(value)
|
| 82 |
+
except:
|
| 83 |
+
return "❌ Please enter a numeric value."
|
| 84 |
+
|
| 85 |
+
try:
|
| 86 |
+
if category == "Temperature":
|
| 87 |
+
result = convert_temperature(val, from_unit, to_unit)
|
| 88 |
+
else:
|
| 89 |
+
result = convert_linear(val, from_unit, to_unit, category)
|
| 90 |
+
except Exception as e:
|
| 91 |
+
return f"⚠️ Error: {e}"
|
| 92 |
+
|
| 93 |
+
return f"{val} {from_unit} = {result:.4g} {to_unit}"
|
| 94 |
+
|
| 95 |
+
# --- STEP 3: Define unit lists for dropdown menus ---
|
| 96 |
+
UNITS_BY_CATEGORY = {
|
| 97 |
+
"Mass": list(UNIT_MAP["Mass"].keys()),
|
| 98 |
+
"Length": list(UNIT_MAP["Length"].keys()),
|
| 99 |
+
"Volume": list(UNIT_MAP["Volume"].keys()),
|
| 100 |
+
"Temperature": TEMP_UNITS,
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
# --- STEP 4: Build Gradio UI ---
|
| 104 |
+
with gr.Blocks() as demo:
|
| 105 |
+
gr.Markdown("## 🔄 Simple Unit Converter")
|
| 106 |
+
|
| 107 |
+
# Row: category + unit dropdowns
|
| 108 |
+
with gr.Row():
|
| 109 |
+
category = gr.Dropdown(list(UNITS_BY_CATEGORY.keys()), value="Mass", label="Category")
|
| 110 |
+
from_unit = gr.Dropdown(UNITS_BY_CATEGORY["Mass"], label="From Unit")
|
| 111 |
+
to_unit = gr.Dropdown(UNITS_BY_CATEGORY["Mass"], label="To Unit")
|
| 112 |
+
|
| 113 |
+
# Input + button + output
|
| 114 |
+
value_input = gr.Number(value=1, label="Value")
|
| 115 |
+
convert_btn = gr.Button("Convert")
|
| 116 |
+
output = gr.Textbox(label="Result")
|
| 117 |
+
|
| 118 |
+
# --- Update dropdowns when category changes ---
|
| 119 |
+
def update_units(cat):
|
| 120 |
+
units = UNITS_BY_CATEGORY[cat]
|
| 121 |
+
return gr.update(choices=units, value=units[0]), gr.update(choices=units, value=units[1])
|
| 122 |
+
|
| 123 |
+
category.change(update_units, category, [from_unit, to_unit])
|
| 124 |
+
convert_btn.click(convert, [value_input, from_unit, to_unit, category], output)
|
| 125 |
+
|
| 126 |
+
# Run app (share=True gives a public link in Colab)
|
| 127 |
+
if __name__ == "__main__":
|
| 128 |
+
demo.launch(share=True)
|