|
|
import gradio as gr |
|
|
|
|
|
def calculate_mpg(miles, fuel): |
|
|
""" |
|
|
Core logic function to calculate MPG. |
|
|
This replaces the logic inside the Tkinter class. |
|
|
""" |
|
|
try: |
|
|
|
|
|
if fuel <= 0: |
|
|
return "Error: Fuel must be > 0" |
|
|
if miles < 0: |
|
|
return "Error: Miles cannot be negative" |
|
|
|
|
|
|
|
|
mpg = miles / fuel |
|
|
return f"{round(mpg, 2)} MPG" |
|
|
|
|
|
except Exception as e: |
|
|
return f"Error: {str(e)}" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface = gr.Interface( |
|
|
fn=calculate_mpg, |
|
|
inputs=[ |
|
|
gr.Number(label="Miles Driven", value=0), |
|
|
gr.Number(label="Fuel Consumption (Gallons)", value=0) |
|
|
], |
|
|
outputs=gr.Textbox(label="Calculated Fuel Economy"), |
|
|
title="FUEL ECONOMY CALCULATOR", |
|
|
description="Enter miles and fuel to calculate your MPG. This is a web-based version of our Tkinter assignment.", |
|
|
theme="soft" |
|
|
) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
interface.launch() |
|
|
|