anly656 commited on
Commit
4be9855
·
verified ·
1 Parent(s): 46bf491

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +40 -0
  2. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ def calculate_mpg(miles, fuel):
4
+ """
5
+ Core logic function to calculate MPG.
6
+ This replaces the logic inside the Tkinter class.
7
+ """
8
+ try:
9
+ # Input validation
10
+ if fuel <= 0:
11
+ return "Error: Fuel must be > 0"
12
+ if miles < 0:
13
+ return "Error: Miles cannot be negative"
14
+
15
+ # Calculation
16
+ mpg = miles / fuel
17
+ return f"{round(mpg, 2)} MPG"
18
+
19
+ except Exception as e:
20
+ return f"Error: {str(e)}"
21
+
22
+ # Define the Gradio Interface
23
+ # 1. Inputs: Two number boxes
24
+ # 2. Output: A text box for the result
25
+ # 3. Title/Description: For the top of the web page
26
+ interface = gr.Interface(
27
+ fn=calculate_mpg,
28
+ inputs=[
29
+ gr.Number(label="Miles Driven", value=0),
30
+ gr.Number(label="Fuel Consumption (Gallons)", value=0)
31
+ ],
32
+ outputs=gr.Textbox(label="Calculated Fuel Economy"),
33
+ title="FUEL ECONOMY CALCULATOR",
34
+ description="Enter miles and fuel to calculate your MPG. This is a web-based version of our Tkinter assignment.",
35
+ theme="soft" # Optional: gives it a modern look
36
+ )
37
+
38
+ # Launch the app
39
+ if __name__ == "__main__":
40
+ interface.launch()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ gradio