CoolFace
Apppublic

jeffrey1963/Cattle-Elk-R5

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py123 linesDownload Raw Back to root
1import os2import gradio as gr3import numpy as np4from openai import OpenAI5from Sim_Setup_Fcns import (6    load_and_crop_image, cluster_image, build_parcel_map,7    get_cluster_labels, get_land_colors, plot_parcel_map_to_file8)9from Sim_Engine import run_full_simulation10from feedback_fcns import (11    summarize_initial_conditions, plot_forage_map_to_file,12    elk_feedback, usfs_feedback, simulate_and_summarize, full_response13)14 15from zone_utils import (16    identify_zones, plot_labeled_zones,17    assign_zone_labels, save_zone_info_to_excel,override_zone_id_and_label18)19 20# === Setup on Launch ===21img = load_and_crop_image("Carson_map.png")22clustered_img = cluster_image(img)23parcel_map, n_rows, n_cols = build_parcel_map(clustered_img)24cluster_labels = get_cluster_labels()25land_colors = get_land_colors()26plot_parcel_map_to_file(parcel_map, cluster_labels, land_colors, save_path="clustered_map.png")27 28# === Zoning ===29 30# 1. Identify contiguous zones31zone_map, zone_to_cluster = identify_zones(parcel_map, connectivity="queen")32 33# 2. Assign human-readable labels (before override)34zone_labels = assign_zone_labels(zone_to_cluster)35# === Manual override for mislabeled riparian zone ===36# First, update the zone label once37for zid, lbl in zone_labels.items():38    if lbl == "A" and zone_to_cluster[zid] == 1:39        zone_labels[zid] = "Riparian A1"40    if lbl == "M" and zone_to_cluster[zid] == 1:41         zone_labels[zid] = "Riparian A2"42# Then update all matching parcels43for i in range(n_rows):44    for j in range(n_cols):45        zone_id = zone_map[i, j]46        if zone_labels.get(zone_id) == "Riparian A1":47            parcel_map[i, j] = 248        if zone_labels.get(zone_id) == "Riparian A2":49            parcel_map[i, j] = 250 51#       52 53 54# ⬇️ Add this block right after the override55zone_to_cluster = {}56for zone_id in np.unique(zone_map):57    indices = np.argwhere(zone_map == zone_id)58    if len(indices) > 0:59        i, j = indices[0]60        zone_to_cluster[zone_id] = parcel_map[i, j]61 62# 6. Plot labeled zones after override and mapping63plot_labeled_zones(zone_map, zone_labels, zone_to_cluster, save_path="zones_labeled.png")64 65# 5. Define cluster-to-class mapping (should stay after override)66cluster_to_class = {67    0: "desert",68    1: "pasture",69    2: "riparain",70    3: "sensitive riparian",71    4: "wetland",72    5: "water"73}74 75# 7. Save zone info to Excel76zone_excel_path = "zone_info.xlsx"77save_zone_info_to_excel(78    parcel_map, zone_map, zone_labels, zone_to_cluster, cluster_to_class,79    save_path=zone_excel_path80)81 82 83client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))84 85# === Gradio App ===86with gr.Blocks() as demo:87    gr.Markdown("# AGEC 3052 — Grazing Strategy Simulation")88    gr.Image(value="clustered_map.png", label="Initial 25×25 Parcel Layout")89    gr.Image(value="zones_labeled.png", label="Labeled Pasture & Riparian Zones")90    # ✅ Downloadable Excel91    gr.File(value=zone_excel_path, label="Download Zone Info (Excel)")92    93    plan = gr.Radio(["Conservative", "Normal", "Aggressive"], label="Grazing Plan")94    essay = gr.Textbox(lines=8, label="Your Essay Justifying the Plan")95 96    elk_output = gr.Textbox(label="Elk Stakeholder Feedback")97    usfs_output = gr.Textbox(label="USFS Feedback")98    sim_output = gr.Textbox(label="Simulation Results", lines=2)99    sim_image = gr.Image(label="Forage Map After Simulation", type="filepath")100    health_image = gr.Image(label="Health Map After Simulation", type="filepath")101 102    round_counter = gr.State(value=1)103    history = gr.State(value=[summarize_initial_conditions(n_rows, n_cols)])104 105    def submit_handler(plan_choice, essay_text, history_val):106        return full_response(plan_choice, essay_text, history_val[-1])107 108    def sim_handler(plan_choice, round_val, history_val):109        summary, map_path, health_path, new_round = simulate_and_summarize(110            plan_choice, round_val, parcel_map, cluster_labels, n_rows, n_cols,111            run_full_simulation, history_val112        )113        history_val.append(summary)114        return summary, map_path, health_path, new_round, history_val115 116    submit_btn = gr.Button("Submit Grazing Plan")117    sim_btn = gr.Button("Run Simulation")118 119    submit_btn.click(fn=submit_handler, inputs=[plan, essay, history], outputs=[elk_output, usfs_output])120    sim_btn.click(fn=sim_handler, inputs=[plan, round_counter, history], outputs=[sim_output, sim_image, health_image, round_counter, history])121 122demo.launch()123