import gradio as gr
import googlemaps
from gradio_folium import Folium
from utils import *
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
current_directory = os.getcwd()
googlemaps_apikey = os.getenv('GOOGLEMAPS_APIKEY')
center = (13.736717, 100.523186)
data = read_data(current_directory+'/data/CleanedEVstationData.csv')
fmap_provider = plot_fmap_provider(data, center)
gmaps = googlemaps.Client(key=googlemaps_apikey)
with gr.Blocks() as demo:
gr.HTML("""
EV Charging planner
Thailand's EV Charging Planner. Plan to charge your electric vehicle in your trip!!!🚗
"""
)
with gr.Group() as main_group:
with gr.Tab("EV Charger Stations"):
plot_type = gr.Radio(['Provider', 'Charging Type'], value="Provider", label='Map type',
info="Map type plots separatly EV station by providers, charging type or both.",
interactive=True)
gr_fmap = Folium(fmap_provider)
with gr.Tab("EV Charger along the Route"):
with gr.Row():
gr.DownloadButton(label="Input locations", interactive=False)
with gr.Row():
origin_address = gr.Textbox('มหาวิทยาลัยเชียงใหม่', label="Your location.")
destination_address = gr.Textbox('สยามพารากอน', label="Your destination.")
with gr.Row():
gr.DownloadButton(label="Setting Configuration", interactive=False)
with gr.Row():
search_type = gr.Radio(['Place Name', 'Coordinate'], value="Place Name", label='Search by',
info="Search address by place name or coordinate in format of (lat,long).",
interactive=True)
plotcircle_radius = gr.Radio(['Without Circle', 'With Circle'], value="Without Circle", label='Plot Circle',
info="Plot a map with(or without) a circle for each point returned by the Google API.", interactive=True)
radius = gr.Slider(minimum=0.1, maximum=10, value=1, step=0.1, label="Circle Radius (Km.)",
info='Adjust the radius of the circle.', interactive=True)
with gr.Row():
submit_btn = gr.Button("Submit", variant='primary')
with gr.Row():
gr.DownloadButton(label="Map", interactive=False)
with gr.Row():
fmap_route = Folium()
with gr.Row():
total_distance = gr.Textbox(label="Total Distance", interactive=False)
total_duration = gr.Textbox(label="Total Duration", interactive=False)
with gr.Row():
gr.DownloadButton(label="Route Dataframe", interactive=False)
with gr.Row():
route_df = gr.Dataframe(label="Route Dataframe", row_count=(5,'dynamic'), interactive=False, wrap=True)
with gr.Row():
gr.DownloadButton(label="Stations Dataframe", interactive=False)
with gr.Row():
stations_df = gr.Dataframe(label="Stations Dataframe", row_count=(5,'dynamic'), interactive=False, wrap=True)
with gr.Row():
gr.DownloadButton(label="Download your data as Excel file below", interactive=False)
with gr.Row():
file1 = gr.File(current_directory+"/data/route.xlsx", label="Download Here")
file2 = gr.File(current_directory+"/data/stations.xlsx", label="Download Here")
with gr.Tab("Optimize your best stations"):
with gr.Row():
gr.DownloadButton(label="Input Locations", interactive=False)
with gr.Row():
opt_origin_address = gr.Textbox('มหาวิทยาลัยเชียงใหม่', label="Your location.")
opt_destination_address = gr.Textbox('สยามพารากอน', label="Your destination.")
with gr.Row():
gr.DownloadButton(label="Input Battery Details", interactive=False)
with gr.Row():
opt_battery_capacity = gr.Number(value=60, label='Battery capacity (kWh):',
info='Input battery capacity in kWh.')
opt_battery_initial = gr.Slider(minimum=0, maximum=100, value=50, step=0.1,
label="Current battery level (%)", info='Input your current battery level in %.',
interactive=True)
opt_battery_arrival = gr.Slider(minimum=0, maximum=100, value=50, step=0.1,
label="Desired battery level at destination (%)",
info='Input your desired battery level at destination in %.', interactive=True)
opt_reserve_battery = gr.Slider(minimum=0, maximum=100, value=10, step=0.1, label="Reserve battery level (%)",
info='Battery arrival of each station will be always grater than reserve battery.',
interactive=True)
with gr.Row():
opt_charging_ports = gr.CheckboxGroup(["Type2", "CCS2", "CHAdeMO"],
value=["Type2", "CCS2", "CHAdeMO"], label="Usable charging connectors",
info="Select your usable charging connectors.",interactive=True)
opt_provider_filter = gr.CheckboxGroup(['PTT', 'PEA', 'EleX', 'Altervim', 'EA'],
value=['PTT', 'PEA', 'EleX', 'Altervim', 'EA'],
label="Charging providers", info="Select your charging providers.",
interactive=True)
with gr.Row():
with gr.Accordion("Advanced Optimization Settings", open=False):
with gr.Row():
opt_radius_km = gr.Slider(minimum=0.1, maximum=10, value=1, step=0.1, label="Circle Radius (Km.)",
info='Adjust the radius of the circle to find charging stations.',
interactive=True)
opt_epochs = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="Epochs",
info='Adjust the number of epochs for optimization.', interactive=True)
with gr.Row():
opt_submit_btn = gr.Button("Submit", variant='primary')
with gr.Row():
gr.DownloadButton(label="Map and Output", interactive=False)
with gr.Row():
opt_fmap = Folium()
with gr.Row():
opt_distance = gr.Textbox(label="Total Distance", interactive=False)
opt_driving_time = gr.Textbox(label="Driving Time", interactive=False)
opt_charging_time = gr.Textbox(label="Charging Time", interactive=False)
opt_total_time = gr.Textbox(label="Estimate Total Time", interactive=False)
plot_type.select(clear_component, None, gr_fmap).then(lambda plottype : select_plottype(plottype, data, center), plot_type, gr_fmap)
search_type.select(select_searchtype, search_type, [origin_address, destination_address])
submit_btn.click(lambda originaddress, destinationaddress, searchtype, radius, plotcircle :
find_all_station_near_route(originaddress, destinationaddress, data, searchtype, gmaps=gmaps, radius_km=radius,
plotcircle_radius=plotcircle),
[origin_address, destination_address, search_type, radius, plotcircle_radius],
[fmap_route, total_distance, total_duration, route_df, stations_df, file1, file2])
opt_submit_btn.click(lambda originaddress, destinationaddress, batcapacity, batinit, batarival, batreserve, ports, provider, radius, epochs :
plot_optimization(originaddress, destinationaddress, batcapacity, batinit, batarival, batreserve, ports, provider, radius, epochs, gmaps=gmaps, data=data),
[opt_origin_address, opt_destination_address, opt_battery_capacity, opt_battery_initial, opt_battery_arrival, opt_reserve_battery, opt_charging_ports, opt_provider_filter, opt_radius_km, opt_epochs],
[opt_fmap, opt_distance, opt_driving_time, opt_charging_time, opt_total_time])
demo.launch(debug=False)