Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
import random
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
# Import your pipeline (you already created pipeline.py)
|
| 10 |
+
# Must expose: run_search_and_generate(user_img, k_retrieve, n_i2i, n_t2i, steps_t2i, strength_i2i, gen_size, seed)
|
| 11 |
+
from pipeline import run_search_and_generate
|
| 12 |
+
|
| 13 |
+
# ---- HF dataset repo (read images directly from the dataset repo) ----
|
| 14 |
+
HF_DATASET_ID = "LevyJonas/sat_land_patches"
|
| 15 |
+
CACHE_DIR = Path("hf_cache")
|
| 16 |
+
CACHE_DIR.mkdir(exist_ok=True, parents=True)
|
| 17 |
+
|
| 18 |
+
# ---- Utility: download an image file from HF dataset repo (cached) ----
|
| 19 |
+
def load_image_from_hf(rel_path: str) -> Image.Image:
|
| 20 |
+
local_path = hf_hub_download(
|
| 21 |
+
repo_id=HF_DATASET_ID,
|
| 22 |
+
repo_type="dataset",
|
| 23 |
+
filename=rel_path,
|
| 24 |
+
local_dir=str(CACHE_DIR),
|
| 25 |
+
local_dir_use_symlinks=False,
|
| 26 |
+
)
|
| 27 |
+
return Image.open(local_path).convert("RGB")
|
| 28 |
+
|
| 29 |
+
# ---- Quick starters: choose 3 representative images by known labels ----
|
| 30 |
+
# These paths must exist in your dataset repo.
|
| 31 |
+
# If a file does not exist, just replace with another filename from your metadata.csv.
|
| 32 |
+
QUICK_STARTERS = [
|
| 33 |
+
("LakeWater example", "images/LakeWater/LakeWater_000000.jpg"),
|
| 34 |
+
("DenseForest example", "images/DenseForest/DenseForest_000000.jpg"),
|
| 35 |
+
("ResidentialDense example", "images/ResidentialDense/ResidentialDense_000000.jpg"),
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
def get_quickstarter_image(choice_name: str):
|
| 39 |
+
for name, rel in QUICK_STARTERS:
|
| 40 |
+
if name == choice_name:
|
| 41 |
+
return load_image_from_hf(rel)
|
| 42 |
+
# fallback
|
| 43 |
+
return load_image_from_hf(QUICK_STARTERS[0][1])
|
| 44 |
+
|
| 45 |
+
# ---- App core function ----
|
| 46 |
+
def app_run(user_img, k_retrieve, n_i2i, n_t2i, strength_i2i, steps_t2i, gen_size, seed):
|
| 47 |
+
if user_img is None:
|
| 48 |
+
return [], [], [], "Please upload an image."
|
| 49 |
+
|
| 50 |
+
# run pipeline
|
| 51 |
+
retrieved, gen_i2i, gen_t2i, info = run_search_and_generate(
|
| 52 |
+
user_img=user_img,
|
| 53 |
+
k_retrieve=int(k_retrieve),
|
| 54 |
+
n_i2i=int(n_i2i),
|
| 55 |
+
n_t2i=int(n_t2i),
|
| 56 |
+
steps_t2i=int(steps_t2i),
|
| 57 |
+
strength_i2i=float(strength_i2i),
|
| 58 |
+
gen_size=int(gen_size),
|
| 59 |
+
seed=int(seed),
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Format outputs for Gradio Gallery (image, caption)
|
| 63 |
+
retrieved_gallery = []
|
| 64 |
+
for r in retrieved:
|
| 65 |
+
cap = f"{r.get('label','')} | cos={r.get('sim',0):.3f}"
|
| 66 |
+
retrieved_gallery.append((r["img"], cap))
|
| 67 |
+
|
| 68 |
+
i2i_gallery = [(im, f"img2img #{i+1}") for i, im in enumerate(gen_i2i)]
|
| 69 |
+
t2i_gallery = [(im, f"txt2img #{i+1}") for i, im in enumerate(gen_t2i)]
|
| 70 |
+
|
| 71 |
+
# Summary text
|
| 72 |
+
summary_lines = []
|
| 73 |
+
if isinstance(info, dict):
|
| 74 |
+
summary_lines.append(f"majority_label_from_retrieval: {info.get('majority_label_from_retrieval')}")
|
| 75 |
+
summary_lines.append(f"used_prompt: {info.get('used_prompt')}")
|
| 76 |
+
summary_lines.append(f"k_retrieve: {info.get('k_retrieve')}, n_img2img: {info.get('n_img2img')}, n_txt2img: {info.get('n_t2i', info.get('n_txt2img'))}")
|
| 77 |
+
summary_lines.append(f"strength_i2i: {info.get('requested_strength_img2img', info.get('strength_i2i'))}, steps_txt2img: {info.get('steps_txt2img', info.get('steps_t2i'))}")
|
| 78 |
+
summary_lines.append(f"gen_size: {info.get('gen_size')}, seed: {info.get('seed')}")
|
| 79 |
+
|
| 80 |
+
return retrieved_gallery, i2i_gallery, t2i_gallery, "\n".join(summary_lines)
|
| 81 |
+
|
| 82 |
+
# ---- Quick starter click: set input image ----
|
| 83 |
+
def load_quickstarter(choice_name):
|
| 84 |
+
return get_quickstarter_image(choice_name)
|
| 85 |
+
|
| 86 |
+
# ---- Build UI ----
|
| 87 |
+
with gr.Blocks(title="Satellite Patch Search + Generate") as demo:
|
| 88 |
+
gr.Markdown(
|
| 89 |
+
"# Satellite Patch Search + Generate\n"
|
| 90 |
+
"Upload a satellite-like patch → get **Top-K similar patches** from the dataset + **new generated variants** "
|
| 91 |
+
"(Image-to-Image + Text-to-Image)."
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
with gr.Column(scale=1):
|
| 96 |
+
inp = gr.Image(type="pil", label="Upload satellite patch (input)")
|
| 97 |
+
|
| 98 |
+
qs = gr.Dropdown(
|
| 99 |
+
choices=[x[0] for x in QUICK_STARTERS],
|
| 100 |
+
value=QUICK_STARTERS[0][0],
|
| 101 |
+
label="Quick Starters (1-click examples)"
|
| 102 |
+
)
|
| 103 |
+
btn_qs = gr.Button("Load Quick Starter")
|
| 104 |
+
|
| 105 |
+
k_retrieve = gr.Slider(0, 5, value=2, step=1, label="How many images to retrieve from DB (0–5)")
|
| 106 |
+
n_i2i = gr.Slider(0, 5, value=2, step=1, label="How many img2img generated images (0–5)")
|
| 107 |
+
n_t2i = gr.Slider(0, 5, value=2, step=1, label="How many txt2img generated images (0–5)")
|
| 108 |
+
|
| 109 |
+
strength_i2i = gr.Slider(0.25, 0.80, value=0.35, step=0.01, label="img2img strength (lower = closer to input)")
|
| 110 |
+
steps_t2i = gr.Slider(1, 2, value=1, step=1, label="Generation steps (1–2 for sd-turbo)")
|
| 111 |
+
gen_size = gr.Radio([384, 512], value=512, label="Generation size")
|
| 112 |
+
seed = gr.Number(value=42, precision=0, label="Seed")
|
| 113 |
+
|
| 114 |
+
btn_run = gr.Button("Run")
|
| 115 |
+
|
| 116 |
+
with gr.Column(scale=2):
|
| 117 |
+
out_retr = gr.Gallery(label="Retrieved from Dataset (Top-K)", columns=5, height=260)
|
| 118 |
+
out_i2i = gr.Gallery(label="Generated (Image-to-Image)", columns=5, height=260)
|
| 119 |
+
out_t2i = gr.Gallery(label="Generated (Text-to-Image)", columns=5, height=260)
|
| 120 |
+
out_txt = gr.Textbox(label="Summary", lines=6)
|
| 121 |
+
|
| 122 |
+
# Wire buttons
|
| 123 |
+
btn_qs.click(load_quickstarter, inputs=[qs], outputs=[inp])
|
| 124 |
+
btn_run.click(
|
| 125 |
+
app_run,
|
| 126 |
+
inputs=[inp, k_retrieve, n_i2i, n_t2i, strength_i2i, steps_t2i, gen_size, seed],
|
| 127 |
+
outputs=[out_retr, out_i2i, out_t2i, out_txt],
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
demo.launch()
|