STARxMOUT commited on
Commit
c736b41
·
verified ·
1 Parent(s): a08761a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ # --- Conversion Functions ---
4
+ def kg_to_g(value): return value * 1000
5
+ def g_to_kg(value): return value / 1000
6
+ def m_to_cm(value): return value * 100
7
+ def cm_to_m(value): return value / 100
8
+ def m_to_ft(value): return value * 3.28084
9
+ def ft_to_m(value): return value / 3.28084
10
+ def c_to_f(value): return (value * 9/5) + 32
11
+ def f_to_c(value): return (value - 32) * 5/9
12
+
13
+ # --- Conversion Dispatcher ---
14
+ def convert_value(category, from_unit, to_unit, input_value):
15
+ try:
16
+ x = float(input_value)
17
+ except:
18
+ return "⚠️ Please enter a valid number."
19
+
20
+ if category == "Mass":
21
+ if from_unit == "kg" and to_unit == "g": result = kg_to_g(x)
22
+ elif from_unit == "g" and to_unit == "kg": result = g_to_kg(x)
23
+ elif from_unit == to_unit: result = x
24
+ else: return "Conversion not supported in Mass category."
25
+
26
+ elif category == "Length":
27
+ if from_unit == "m" and to_unit == "cm": result = m_to_cm(x)
28
+ elif from_unit == "cm" and to_unit == "m": result = cm_to_m(x)
29
+ elif from_unit == "m" and to_unit == "ft": result = m_to_ft(x)
30
+ elif from_unit == "ft" and to_unit == "m": result = ft_to_m(x)
31
+ elif from_unit == to_unit: result = x
32
+ else: return "Conversion not supported in Length category."
33
+
34
+ elif category == "Temperature":
35
+ if from_unit == "°C" and to_unit == "°F": result = c_to_f(x)
36
+ elif from_unit == "°F" and to_unit == "°C": result = f_to_c(x)
37
+ elif from_unit == to_unit: result = x
38
+ else: return "Conversion not supported in Temperature category."
39
+
40
+ else:
41
+ return "Unknown category."
42
+
43
+ return f"{x} {from_unit} = {round(result, 4)} {to_unit}"
44
+
45
+ # --- Gradio Interface ---
46
+ categories = ["Mass", "Length", "Temperature"]
47
+ units_map = {
48
+ "Mass": ["kg", "g"],
49
+ "Length": ["m", "cm", "ft"],
50
+ "Temperature": ["°C", "°F"]
51
+ }
52
+
53
+ def update_units(cat):
54
+ return (
55
+ gr.update(choices=units_map[cat], value=units_map[cat][0]),
56
+ gr.update(choices=units_map[cat], value=units_map[cat][1]),
57
+ )
58
+
59
+ with gr.Blocks() as demo:
60
+ gr.Markdown("# 🧮 Unit Converter App\nConvert mass, length, and temperature easily!")
61
+ with gr.Row():
62
+ category = gr.Dropdown(label="Category", choices=categories, value="Mass")
63
+ from_unit = gr.Dropdown(label="From unit", choices=units_map["Mass"], value="kg")
64
+ to_unit = gr.Dropdown(label="To unit", choices=units_map["Mass"], value="g")
65
+ value = gr.Textbox(label="Value to convert", placeholder="e.g., 12.5")
66
+ convert_btn = gr.Button("Convert")
67
+ output = gr.Textbox(label="Result", interactive=False)
68
+
69
+ category.change(fn=update_units, inputs=category, outputs=[from_unit, to_unit])
70
+ convert_btn.click(fn=convert_value, inputs=[category, from_unit, to_unit, value], outputs=output)
71
+
72
+ demo.launch()