Update app.py
Browse files123416 unmodified lines21222324252627282930313233344 unmodified lines39404142434445464748495051525354555657585960612 unmodified lines6465666768697071727374757677787980818283848510 unmodified lines9697989910010110215 unmodified lines118119120121122123124import ioimport jsonimport gradio as grimport pandas as pd16 unmodified lines- **Time windows** are *soft* (violations are reported in metrics).- Distances are Euclidean on the X-Y plane for clarity.Tip: Start with the generator, then switch to your CSV."""FOOTER = "Made with β€οΈ using Gradio. No native dependencies; runs quickly on Spaces."def run_generator(n_clients, n_vehicles, capacity, spread, demand_min, demand_max, seed):df = generate_random_instance(n_clients=n_clients,n_vehicles=n_vehicles,4 unmodified linesseed=seed,)depot = (0.0, 0.0)sol = solve_vrp(df, depot=depot, n_vehicles=n_vehicles, capacity=capacity, speed=1.0)fig = plot_solution(df, sol, depot=depot)buf = io.BytesIO()fig.savefig(buf, format="png", bbox_inches="tight", dpi=160)buf.seek(0)# Route tableroute_table = sol["assignments_table"]metrics = json.dumps(sol["metrics"], indent=2)return buf, route_table, metrics, dfdef run_csv(file, n_vehicles, capacity):if file is None:raise gr.Error("Please upload a CSV first.")try:2 unmodified linesraise gr.Error(f"CSV parsing error: {e}")depot = (0.0, 0.0)sol = solve_vrp(df, depot=depot, n_vehicles=n_vehicles, capacity=capacity, speed=1.0)fig = plot_solution(df, sol, depot=depot)buf = io.BytesIO()fig.savefig(buf, format="png", bbox_inches="tight", dpi=160)buf.seek(0)route_table = sol["assignments_table"]metrics = json.dumps(sol["metrics"], indent=2)return buf, route_table, metricsdef download_template():df = make_template_dataframe()return dfwith gr.Blocks(title=TITLE) as demo:gr.Markdown(f"# {TITLE}")gr.Markdown(DESC)10 unmodified linesseed = gr.Slider(0, 9999, value=42, step=1, label="Random seed")run_btn = gr.Button("π Generate & Optimize", variant="primary")with gr.Column():img = gr.Image(label="Route Visualization", interactive=False)with gr.Row():route_df = gr.Dataframe(label="Route assignments (per stop)", wrap=True)metrics = gr.Code(label="Metrics (JSON)")15 unmodified linescapacity2 = gr.Slider(1, 200, value=15, step=1, label="Vehicle capacity (sum of demand)")run_btn2 = gr.Button("π Optimize uploaded data", variant="primary")with gr.Column():img2 = gr.Image(label="Route Visualization", interactive=False)with gr.Row():route_df2 = gr.Dataframe(label="Route assignments (per stop)")12316 unmodified lines202122232425262728293031323334354 unmodified lines4041424344454647484950515253545556572 unmodified lines6061626364656667686970717273747576777879808110 unmodified lines929394959697989915 unmodified lines115116117118119120121import jsonimport gradio as grimport pandas as pd16 unmodified lines- **Time windows** are *soft* (violations are reported in metrics).- Distances are Euclidean on the X-Y plane for clarity.π‘ Tip: Start with the generator, then switch to your CSV."""FOOTER = "Made with β€οΈ using Gradio. No native dependencies; runs quickly on Spaces."# -----------------------------# Functions# -----------------------------def run_generator(n_clients, n_vehicles, capacity, spread, demand_min, demand_max, seed):df = generate_random_instance(n_clients=n_clients,n_vehicles=n_vehicles,4 unmodified linesseed=seed,)depot = (0.0, 0.0)sol = solve_vrp(df, depot=depot, n_vehicles=n_vehicles, capacity=capacity, speed=1.0)# β plot_solution now returns a PIL imageimg = plot_solution(df, sol, depot=depot)route_table = sol["assignments_table"]metrics = json.dumps(sol["metrics"], indent=2)return img, route_table, metrics, dfdef run_csv(file, n_vehicles, capacity):if file is None:raise gr.Error("Please upload a CSV first.")try:2 unmodified linesraise gr.Error(f"CSV parsing error: {e}")depot = (0.0, 0.0)sol = solve_vrp(df, depot=depot, n_vehicles=n_vehicles, capacity=capacity, speed=1.0)img = plot_solution(df, sol, depot=depot)route_table = sol["assignments_table"]metrics = json.dumps(sol["metrics"], indent=2)return img, route_table, metricsdef download_template():return make_template_dataframe()# -----------------------------# UI Layout# -----------------------------with gr.Blocks(title=TITLE) as demo:gr.Markdown(f"# {TITLE}")gr.Markdown(DESC)10 unmodified linesseed = gr.Slider(0, 9999, value=42, step=1, label="Random seed")run_btn = gr.Button("π Generate & Optimize", variant="primary")with gr.Column():img = gr.Image(type="pil", label="Route Visualization", interactive=False)with gr.Row():route_df = gr.Dataframe(label="Route assignments (per stop)", wrap=True)metrics = gr.Code(label="Metrics (JSON)")15 unmodified linescapacity2 = gr.Slider(1, 200, value=15, step=1, label="Vehicle capacity (sum of demand)")run_btn2 = gr.Button("π Optimize uploaded data", variant="primary")with gr.Column():img2 = gr.Image(type="pil", label="Route Visualization", interactive=False)with gr.Row():route_df2 = gr.Dataframe(label="Route assignments (per stop)")
app.py+26-29