HarishHC21 commited on
Commit
d19fdc1
·
verified ·
1 Parent(s): 12e6c9c

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +29 -12
  2. app.py +155 -0
  3. requirements.txt +1 -0
README.md CHANGED
@@ -1,12 +1,29 @@
1
- ---
2
- title: Time Costcalculator
3
- emoji: 📈
4
- colorFrom: green
5
- colorTo: blue
6
- sdk: gradio
7
- sdk_version: 5.49.1
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # EV Home Charging: Time & Cost Estimator (Gradio)
3
+
4
+ A simple Gradio app to estimate how long a home charging session will take for your EV and what it will cost.
5
+
6
+ ## Features
7
+ - Inputs: battery capacity (kWh), current/target SoC, charger power, efficiency, and electricity tariff.
8
+ - Pricing modes: flat rate or Time-of-Day (ToD) blended.
9
+ - Outputs: battery energy added, wall energy consumed (incl. losses), charging time (hours), and session cost.
10
+
11
+ ## How to run locally
12
+ ```bash
13
+ pip install -r requirements.txt
14
+ python app.py
15
+ ```
16
+
17
+ ## Deploy to Hugging Face Spaces (SDK: Gradio)
18
+ 1. Create a new Space: https://huggingface.co/spaces → **Create new Space** → SDK **Gradio**.
19
+ 2. Upload `app.py` and `requirements.txt` (drag & drop or `git push`).
20
+ 3. Wait for the build logs to finish. The app will appear on your Space URL.
21
+
22
+ ## Formulas
23
+ - Energy on battery (kWh) = `battery_kwh * (target_soc - current_soc)/100`
24
+ - Wall energy (kWh) = `battery_energy / efficiency`
25
+ - Time (hours) = `wall_energy / charger_power_kW`
26
+ - Cost (₹) = `wall_energy * rate` (or blended ToD rate)
27
+
28
+ ## Disclaimer
29
+ Tariffs and fixed charges differ by utility and may change. Use your latest bill and adjust the inputs accordingly.
app.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+
4
+ TITLE = "EV Home Charging: Time & Cost Estimator"
5
+ DESCRIPTION = (
6
+ "Calculate how long it takes to charge your EV at home and how much it costs. "
7
+ "Enter your battery size, current/target state-of-charge (SoC), charger power, "
8
+ "efficiency, and your electricity price."
9
+ )
10
+
11
+ def compute(ev_battery_kwh: float, soc_current: float, soc_target: float,
12
+ power_choice: str, custom_power_kw: float,
13
+ efficiency_pct: float,
14
+ pricing_mode: str,
15
+ flat_rate_rs_per_kwh: float,
16
+ peak_rate_rs_per_kwh: float,
17
+ off_rate_rs_per_kwh: float,
18
+ peak_share_pct: float):
19
+ # sanitize inputs
20
+ soc_current = max(0.0, min(100.0, soc_current))
21
+ soc_target = max(0.0, min(100.0, soc_target))
22
+ if soc_target <= soc_current:
23
+ return (
24
+ "Target SoC must be greater than current SoC.",
25
+ None, None, None, None
26
+ )
27
+ efficiency = max(1e-3, min(100.0, efficiency_pct)) / 100.0
28
+
29
+ # determine charger power (kW)
30
+ if power_choice == "2.3 kW (10A, single-phase)":
31
+ charger_kw = 2.3
32
+ elif power_choice == "3.3 kW (15A, single-phase)":
33
+ charger_kw = 3.3
34
+ elif power_choice == "7.4 kW (32A, single-phase)":
35
+ charger_kw = 7.4
36
+ elif power_choice == "11 kW (3-phase)":
37
+ charger_kw = 11.0
38
+ elif power_choice == "22 kW (3-phase)":
39
+ charger_kw = 22.0
40
+ else: # custom
41
+ charger_kw = max(0.1, custom_power_kw)
42
+
43
+ # energy needed on battery side (kWh)
44
+ energy_battery_kwh = ev_battery_kwh * (soc_target - soc_current) / 100.0
45
+ # account for charging losses -> wall energy
46
+ energy_wall_kwh = energy_battery_kwh / efficiency
47
+ # time (hours)
48
+ charge_hours = energy_wall_kwh / charger_kw
49
+
50
+ # price calculation
51
+ if pricing_mode == "Flat rate":
52
+ cost_rs = energy_wall_kwh * max(0.0, flat_rate_rs_per_kwh)
53
+ breakdown = (
54
+ f"Flat: {energy_wall_kwh:.2f} kWh × ₹{flat_rate_rs_per_kwh:.2f}/kWh"
55
+ )
56
+ else:
57
+ peak_share = max(0.0, min(100.0, peak_share_pct)) / 100.0
58
+ blended_rate = peak_share * max(0.0, peak_rate_rs_per_kwh) + (1 - peak_share) * max(0.0, off_rate_rs_per_kwh)
59
+ cost_rs = energy_wall_kwh * blended_rate
60
+ breakdown = (
61
+ f"ToD blended rate: peak {peak_share*100:.0f}% at ₹{peak_rate_rs_per_kwh:.2f}/kWh, "
62
+ f"off-peak {100-peak_share*100:.0f}% at ₹{off_rate_rs_per_kwh:.2f}/kWh"
63
+ )
64
+
65
+ summary = (
66
+ f"Battery energy added: {energy_battery_kwh:.2f} kWh
67
+ "
68
+ f"Wall energy consumed (incl. losses): {energy_wall_kwh:.2f} kWh
69
+ "
70
+ f"Estimated charging time: {charge_hours:.2f} hours (at {charger_kw:.2f} kW)
71
+ "
72
+ f"Estimated session cost: ₹{cost_rs:.2f}
73
+ "
74
+ f"Pricing: {breakdown}"
75
+ )
76
+
77
+ # structured outputs
78
+ return (
79
+ summary,
80
+ energy_battery_kwh,
81
+ energy_wall_kwh,
82
+ charge_hours,
83
+ cost_rs
84
+ )
85
+
86
+ with gr.Blocks(title=TITLE) as demo:
87
+ gr.Markdown(f"# {TITLE}
88
+ {DESCRIPTION}")
89
+
90
+ with gr.Row():
91
+ with gr.Column():
92
+ ev_battery_kwh = gr.Number(label="Battery capacity (kWh)", value=50.0, precision=2)
93
+ soc_current = gr.Slider(label="Current SoC (%)", minimum=0, maximum=100, value=20, step=1)
94
+ soc_target = gr.Slider(label="Target SoC (%)", minimum=1, maximum=100, value=80, step=1)
95
+
96
+ with gr.Column():
97
+ power_choice = gr.Radio(
98
+ label="Home charger power",
99
+ choices=[
100
+ "2.3 kW (10A, single-phase)",
101
+ "3.3 kW (15A, single-phase)",
102
+ "7.4 kW (32A, single-phase)",
103
+ "11 kW (3-phase)",
104
+ "22 kW (3-phase)",
105
+ "Custom (kW)"
106
+ ],
107
+ value="7.4 kW (32A, single-phase)"
108
+ )
109
+ custom_power_kw = gr.Number(label="Custom power (kW)", value=3.5, precision=2)
110
+ efficiency_pct = gr.Slider(label="Charging efficiency (%)", minimum=70, maximum=100, value=90, step=1)
111
+
112
+ with gr.Row():
113
+ with gr.Column():
114
+ pricing_mode = gr.Radio(label="Pricing mode", choices=["Flat rate", "Time-of-Day (ToD)"] , value="Flat rate")
115
+ flat_rate = gr.Number(label="Flat rate (₹/kWh)", value=7.00, precision=2)
116
+ with gr.Column():
117
+ peak_rate = gr.Number(label="Peak rate (₹/kWh)", value=8.50, precision=2, visible=False)
118
+ off_rate = gr.Number(label="Off-peak rate (₹/kWh)", value=6.00, precision=2, visible=False)
119
+ peak_share = gr.Slider(label="Share of charging during peak (%)", minimum=0, maximum=100, value=30, step=1, visible=False)
120
+
121
+ btn = gr.Button("Estimate")
122
+
123
+ summary = gr.Textbox(label="Results", lines=6)
124
+ energy_battery = gr.Number(label="Battery energy added (kWh)")
125
+ energy_wall = gr.Number(label="Wall energy consumed (kWh)")
126
+ time_hours = gr.Number(label="Charging time (hours)")
127
+ cost_rs = gr.Number(label="Session cost (₹)")
128
+
129
+ def toggle_inputs(pricing_mode):
130
+ show_tod = pricing_mode == "Time-of-Day (ToD)"
131
+ return (
132
+ gr.update(visible=show_tod),
133
+ gr.update(visible=show_tod),
134
+ gr.update(visible=show_tod)
135
+ )
136
+
137
+ pricing_mode.change(toggle_inputs, inputs=[pricing_mode], outputs=[peak_rate, off_rate, peak_share])
138
+
139
+ btn.click(
140
+ compute,
141
+ inputs=[ev_battery_kwh, soc_current, soc_target, power_choice, custom_power_kw, efficiency_pct,
142
+ pricing_mode, flat_rate, peak_rate, off_rate, peak_share],
143
+ outputs=[summary, energy_battery, energy_wall, time_hours, cost_rs]
144
+ )
145
+
146
+ gr.Markdown("""
147
+ ### Notes
148
+ - **Charging efficiency** accounts for AC charging losses (cable, converter, battery). Typical values are 88–95%.
149
+ - **Home charger power** is the sustained AC power delivered to the car. Real-world values may vary due to voltage, wiring, and thermal limits.
150
+ - **Tariffs** vary by state/utility and may be slab-based or ToD. Use your latest bill for accurate rates.
151
+ - **This tool estimates** session time & cost; it does not model tapering at very high SoC.
152
+ """)
153
+
154
+ if __name__ == "__main__":
155
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ gradio>=4.44.0