Spaces:
Runtime error
Runtime error
persistant storage
Browse files6 unmodified lines7891011101112131412 unmodified lines272829303132333435323334363738394041424391 unmodified lines1351361371381391401411421431441451461471481491501511521531541551561571351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681581591601611621631641651661671681691701711721731741751761771781791701711721731741801811821831841761771781791801851861871881891831901911921931861871881891901911921941956 unmodified lines os.environ["OMP_NUM_THREADS"] = "1"
os.environ.setdefault("MPLCONFIGDIR", "/var/tmp/matplotlib")os.environ.setdefault("HF_HOME", "/tmp/hf")os.environ.setdefault("TORCH_HOME", "/tmp/torch")os.environ.setdefault("HF_HOME", "/data/.huggingface")os.environ.setdefault("TORCH_HOME", "/data/.cache/torch")
import tempfile, shutil, globimport matplotlib12 unmodified linesfrom detectree2.models.predict import predict_on_datafrom detectree2.models.train import setup_cfg
# configWEIGHT_DIR = os.getenv("DTR2_WEIGHT_DIR", "/data/weights") os.makedirs(WEIGHT_DIR, exist_ok=True)
# configWEIGHTS = { "Forest": "/weights/230103_randresize_full.pth", # "Urban": "/weights/urban_trees_Cambridge_20230630.pth", # "Flexi": "/weights/250312_flexi.pth", "Forest": os.path.join(WEIGHT_DIR, "230103_randresize_full.pth"), # "Urban": os.path.join(WEIGHT_DIR, "urban_trees_Cambridge_20230630.pth"), # "Flexi": os.path.join(WEIGHT_DIR, "250312_flexi.pth"),}
DEFAULT_ENV = "Forest"DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
91 unmodified linesdef infer(file_path, environment, confidence): return run_detectree2(file_path, environment, confidence)
def save_to_persistent(uploaded_file): os.makedirs(WEIGHT_DIR, exist_ok=True) dest = os.path.join(WEIGHT_DIR, uploaded_file.orig_name) shutil.copy2(uploaded_file.name, dest) return f"✅ Saved {uploaded_file.orig_name} to {WEIGHT_DIR}"
def list_persistent(): out = [] for root, dirs, files in os.walk("/data"): for f in files: p = os.path.join(root, f) try: sz = os.path.getsize(p) / (1024**2) out.append(f"{p} — {sz:.2f} MB") except Exception: out.append(p) return "\n".join(sorted(out)) or "(empty)"
# gradio uiwith gr.Blocks(title="Detectree2 – Landscape Prediction") as demo: gr.Markdown( "# Detectree2\n" "Upload a GeoTIFF and get predicted tree crowns as a PNG overlay + a GPKG." ) with gr.Row(): inp_file = gr.File(label="Input (.tif / .tiff)", file_types=[".tif", ".tiff"], type="filepath") env_dd = gr.Dropdown(label="Environment", choices=list(WEIGHTS.keys()), value=DEFAULT_ENV) conf = gr.Slider(0.0, 1.0, value=0.5, step=0.05, label="Confidence threshold") btn = gr.Button("Run prediction", variant="primary")
out_img = gr.Image(label="Overlay (PNG)", type="filepath") out_gpkg = gr.File(label="Crowns (GPKG)") out_stats = gr.JSON(label="Statistics") out_err = gr.Textbox(label="Logs / Errors", interactive=False)
btn.click( infer, inputs=[inp_file, env_dd, conf], outputs=[out_img, out_gpkg, out_stats, out_err], concurrency_limit=1, )
if __name__ == "__main__": port = int(os.getenv("PORT", "7860")) demo.queue().launch( server_name="0.0.0.0", server_port=port, max_threads=1, show_api=False, )
import shutil gr.Markdown("# Detectree2\nModels load from `/data/weights` (persistent).")
with gr.Tabs(): with gr.Tab("Predict"): with gr.Row(): inp_file = gr.File(label="Input (.tif / .tiff)", file_types=[".tif", ".tiff"], type="filepath") env_dd = gr.Dropdown(label="Environment", choices=list(WEIGHTS.keys()), value=DEFAULT_ENV) conf = gr.Slider(0.0, 1.0, value=0.5, step=0.05, label="Confidence threshold") run_btn = gr.Button("Run prediction", variant="primary")
out_img = gr.Image(label="Overlay (PNG)", type="filepath") out_gpkg = gr.File(label="Crowns (GPKG)") out_stats = gr.JSON(label="Statistics") out_err = gr.Textbox(label="Logs / Errors", interactive=False)
run_btn.click( infer, inputs=[inp_file, env_dd, conf], outputs=[out_img, out_gpkg, out_stats, out_err], concurrency_limit=1, )
def save_to_persistent(uploaded_file): os.makedirs("/data/weights", exist_ok=True) dest = os.path.join("/data/weights", uploaded_file.orig_name) shutil.copy2(uploaded_file.name, dest) return f"✅ Saved {uploaded_file.orig_name} to /data/weights" with gr.Tab("Manage Weights (/data/weights)"): up = gr.File(label="Upload .pth", file_types=[".pth"]) up_out = gr.Textbox(label="Upload status") up.upload(save_to_persistent, up, up_out)
with gr.Blocks(title="Upload to Persistent Storage") as demo: gr.Markdown("### Upload model weights to `/data/weights` (persistent)") f = gr.File(label="Select .pth file", file_types=[".pth"]) out = gr.Textbox() f.upload(save_to_persistent, f, out) list_btn = gr.Button("List /data contents") list_out = gr.Textbox(label="Persistent files", lines=12) list_btn.click(lambda: list_persistent(), None, list_out)
if __name__ == "__main__": demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860))) port = int(os.getenv("PORT", "7860")) demo.queue().launch(server_name="0.0.0.0", server_port=port, max_threads=1, show_api=False)
print("📦 Contents of /data:")for root, dirs, files in os.walk("/data"): level = root.replace("/data", "").count(os.sep) indent = " " * level print(f"{indent}{os.path.basename(root)}/") for f in files: print(f"{indent} {f}")
app.py+58-57