Cobra_Aviator / app.py
Mrhuman1's picture
Update app.py
69c9c5c verified
Raw
History Blame Contribute Delete
4.18 kB
import gradio as gr
# Conversion functions
def convert_speed(value, from_unit, to_unit):
conversions = {
"kt": 1,
"mph": 1.15078,
"km/h": 1.852,
"m/s": 0.514444,
"Mach": 0.0015118,
}
return value * (conversions[to_unit] / conversions[from_unit])
def convert_distance(value, from_unit, to_unit):
conversions = {
"NM": 1,
"miles": 1.15078,
"km": 1.852,
"ft": 6076.12,
"m": 1852,
}
return value * (conversions[to_unit] / conversions[from_unit])
def convert_weight(value, from_unit, to_unit):
conversions = {
"lb": 1,
"kg": 0.453592,
"ton (metric)": 0.000453592,
"ton (US)": 0.0005,
}
return value * (conversions[to_unit] / conversions[from_unit])
def convert_temperature(value, from_unit, to_unit):
if from_unit == to_unit:
return value
if from_unit == "°C":
return (value * 9/5 + 32) if to_unit == "°F" else value + 273.15
if from_unit == "°F":
return (value - 32) * 5/9 if to_unit == "°C" else ((value - 32) * 5/9) + 273.15
if from_unit == "K":
return value - 273.15 if to_unit == "°C" else ((value - 273.15) * 9/5) + 32
def convert_pressure(value, from_unit, to_unit):
conversions = {
"hPa": 1,
"inHg": 0.029529983071445,
"mmHg": 0.75006375541921,
}
return value * (conversions[to_unit] / conversions[from_unit])
def convert_fuel(value, from_unit, to_unit):
conversions = {
"gallons (US)": 1,
"L": 3.78541,
"kg": 0.8,
"lb": 6.7,
}
return value * (conversions[to_unit] / conversions[from_unit])
def convert_heading(value, from_unit, to_unit):
return value % 360 if to_unit == "degrees" else value # placeholder logic
# Interface layout
def build_interface():
with gr.Blocks() as demo:
gr.Markdown("## ✈️ Cobra: Aviation Unit Converter")
category = gr.Radio(
["Speed / Velocity / Wind", "Distance / Altitude", "Weight / Mass",
"Temperature", "Pressure", "Fuel", "Heading / Bearings / Navigation"],
label="Select a Category"
)
value = gr.Number(label="Enter Value")
from_unit = gr.Dropdown(choices=[], label="Convert From")
to_unit = gr.Dropdown(choices=[], label="Convert To")
result = gr.Textbox(label="Converted Result")
def update_units(cat):
options = {
"Speed / Velocity / Wind": ["kt", "mph", "km/h", "m/s", "Mach"],
"Distance / Altitude": ["NM", "miles", "km", "ft", "m"],
"Weight / Mass": ["lb", "kg", "ton (metric)", "ton (US)"],
"Temperature": ["°C", "°F", "K"],
"Pressure": ["hPa", "inHg", "mmHg"],
"Fuel": ["gallons (US)", "L", "kg", "lb"],
"Heading / Bearings / Navigation": ["degrees"],
}
unit_list = options.get(cat, [])
return gr.update(choices=unit_list, value=None), gr.update(choices=unit_list, value=None)
def convert_all(cat, val, from_u, to_u):
if not all([cat, val, from_u, to_u]):
return "Please fill in all fields."
try:
converters = {
"Speed / Velocity / Wind": convert_speed,
"Distance / Altitude": convert_distance,
"Weight / Mass": convert_weight,
"Temperature": convert_temperature,
"Pressure": convert_pressure,
"Fuel": convert_fuel,
"Heading / Bearings / Navigation": convert_heading,
}
converter = converters.get(cat)
result = converter(float(val), from_u, to_u)
return round(result, 4)
except Exception as e:
return f"Error: {str(e)}"
category.change(update_units, inputs=category, outputs=[from_unit, to_unit])
gr.Button("Convert").click(convert_all, inputs=[category, value, from_unit, to_unit], outputs=result)
return demo
app = build_interface()
app.launch()