Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,25 @@
|
|
| 2 |
import gradio as gr
|
| 3 |
from pipeline import run_search_and_generate
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def run_app(user_img, prompt, k_retrieve, n_i2i, n_t2i, strength_i2i, steps, gen_size, seed):
|
| 6 |
try:
|
| 7 |
retrieved, gen_i2i, gen_t2i, info = run_search_and_generate(
|
|
@@ -23,12 +42,31 @@ def run_app(user_img, prompt, k_retrieve, n_i2i, n_t2i, strength_i2i, steps, gen
|
|
| 23 |
except Exception as e:
|
| 24 |
return [], [], [], f"Error: {e}"
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
with gr.Blocks(title="Satellite Patch: Retrieve + Generate") as demo:
|
| 27 |
gr.Markdown(
|
| 28 |
"# Satellite Patch: Retrieve + Generate\n"
|
| 29 |
"Upload a satellite patch + write a prompt → get (1) similar images from the dataset and (2) newly generated images."
|
| 30 |
)
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
with gr.Row():
|
| 33 |
with gr.Column(scale=1):
|
| 34 |
user_img = gr.Image(type="pil", label="Upload satellite patch")
|
|
@@ -55,8 +93,28 @@ with gr.Blocks(title="Satellite Patch: Retrieve + Generate") as demo:
|
|
| 55 |
out_t2i = gr.Gallery(label="Generated (txt2img)", columns=5, height=260)
|
| 56 |
out_txt = gr.Textbox(label="Summary", lines=8)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
demo.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from pipeline import run_search_and_generate
|
| 4 |
|
| 5 |
+
# --- Quick Starters: load example images directly from the HF dataset repo ---
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from PIL import Image
|
| 9 |
+
|
| 10 |
+
HF_DATASET_ID = "LevyJonas/sat_land_patches"
|
| 11 |
+
QS_CACHE = Path("qs_cache")
|
| 12 |
+
QS_CACHE.mkdir(parents=True, exist_ok=True)
|
| 13 |
+
|
| 14 |
+
def load_qs_image(rel_path: str) -> Image.Image:
|
| 15 |
+
p = hf_hub_download(
|
| 16 |
+
repo_id=HF_DATASET_ID,
|
| 17 |
+
repo_type="dataset",
|
| 18 |
+
filename=rel_path,
|
| 19 |
+
local_dir=str(QS_CACHE),
|
| 20 |
+
local_dir_use_symlinks=False,
|
| 21 |
+
)
|
| 22 |
+
return Image.open(p).convert("RGB")
|
| 23 |
+
|
| 24 |
def run_app(user_img, prompt, k_retrieve, n_i2i, n_t2i, strength_i2i, steps, gen_size, seed):
|
| 25 |
try:
|
| 26 |
retrieved, gen_i2i, gen_t2i, info = run_search_and_generate(
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
return [], [], [], f"Error: {e}"
|
| 44 |
|
| 45 |
+
# Quick starter runner: loads image from HF and calls the same run_app function
|
| 46 |
+
def run_quickstarter(rel_path, prompt, k_retrieve, n_i2i, n_t2i, strength_i2i, steps, gen_size, seed):
|
| 47 |
+
img = load_qs_image(rel_path)
|
| 48 |
+
return run_app(img, prompt, k_retrieve, n_i2i, n_t2i, strength_i2i, steps, gen_size, seed)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
with gr.Blocks(title="Satellite Patch: Retrieve + Generate") as demo:
|
| 52 |
gr.Markdown(
|
| 53 |
"# Satellite Patch: Retrieve + Generate\n"
|
| 54 |
"Upload a satellite patch + write a prompt → get (1) similar images from the dataset and (2) newly generated images."
|
| 55 |
)
|
| 56 |
|
| 57 |
+
# --- Quick Starters (1-click examples) ---
|
| 58 |
+
gr.Markdown("### Quick Starters (1-click examples)")
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
qs1 = gr.Button("Quick Starter: LakeWater")
|
| 62 |
+
qs2 = gr.Button("Quick Starter: DenseForest")
|
| 63 |
+
qs3 = gr.Button("Quick Starter: ResidentialDense")
|
| 64 |
+
|
| 65 |
+
# Change these paths if needed (must exist in your dataset repo)
|
| 66 |
+
QS_1_PATH = "images/LakeWater/LakeWater_000000.jpg"
|
| 67 |
+
QS_2_PATH = "images/DenseForest/DenseForest_000000.jpg"
|
| 68 |
+
QS_3_PATH = "images/ResidentialDense/ResidentialDense_000000.jpg"
|
| 69 |
+
|
| 70 |
with gr.Row():
|
| 71 |
with gr.Column(scale=1):
|
| 72 |
user_img = gr.Image(type="pil", label="Upload satellite patch")
|
|
|
|
| 93 |
out_t2i = gr.Gallery(label="Generated (txt2img)", columns=5, height=260)
|
| 94 |
out_txt = gr.Textbox(label="Summary", lines=8)
|
| 95 |
|
| 96 |
+
# Normal run (user upload)
|
| 97 |
+
btn.click(
|
| 98 |
+
run_app,
|
| 99 |
+
inputs=[user_img, prompt, k_retrieve, n_i2i, n_t2i, strength_i2i, steps, gen_size, seed],
|
| 100 |
+
outputs=[out_retr, out_i2i, out_t2i, out_txt],
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# Quick Starter runs (1 click)
|
| 104 |
+
qs1.click(
|
| 105 |
+
run_quickstarter,
|
| 106 |
+
inputs=[gr.State(QS_1_PATH), prompt, k_retrieve, n_i2i, n_t2i, strength_i2i, steps, gen_size, seed],
|
| 107 |
+
outputs=[out_retr, out_i2i, out_t2i, out_txt],
|
| 108 |
+
)
|
| 109 |
+
qs2.click(
|
| 110 |
+
run_quickstarter,
|
| 111 |
+
inputs=[gr.State(QS_2_PATH), prompt, k_retrieve, n_i2i, n_t2i, strength_i2i, steps, gen_size, seed],
|
| 112 |
+
outputs=[out_retr, out_i2i, out_t2i, out_txt],
|
| 113 |
+
)
|
| 114 |
+
qs3.click(
|
| 115 |
+
run_quickstarter,
|
| 116 |
+
inputs=[gr.State(QS_3_PATH), prompt, k_retrieve, n_i2i, n_t2i, strength_i2i, steps, gen_size, seed],
|
| 117 |
+
outputs=[out_retr, out_i2i, out_t2i, out_txt],
|
| 118 |
+
)
|
| 119 |
|
| 120 |
demo.launch()
|