Spaces:
Runtime error
Runtime error
update UI
Browse files- app.py +174 -165
- climategan_wrapper.py +1 -1
app.py
CHANGED
|
@@ -2,26 +2,133 @@
|
|
| 2 |
# thank you @NimaBoscarino
|
| 3 |
|
| 4 |
import os
|
| 5 |
-
|
| 6 |
-
import googlemaps
|
| 7 |
-
from skimage import io
|
| 8 |
from urllib import parse
|
|
|
|
|
|
|
|
|
|
| 9 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
from climategan_wrapper import ClimateGAN
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
def predict(cg: ClimateGAN, api_key):
|
| 15 |
def _predict(*args):
|
| 16 |
-
image = place = painter = None
|
| 17 |
-
if
|
| 18 |
-
image = args
|
| 19 |
-
painter = args[1]
|
| 20 |
else:
|
| 21 |
-
|
| 22 |
-
image, place, painter = args
|
| 23 |
|
| 24 |
-
if api_key and place:
|
| 25 |
geocode_result = gmaps.geocode(place)
|
| 26 |
|
| 27 |
address = geocode_result[0]["formatted_address"]
|
|
@@ -91,170 +198,72 @@ if __name__ == "__main__":
|
|
| 91 |
)
|
| 92 |
cg._setup_stable_diffusion()
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
a {
|
| 98 |
-
color: #0088ff;
|
| 99 |
-
text-decoration: underline;
|
| 100 |
-
}
|
| 101 |
-
strong {
|
| 102 |
-
color: #c34318;
|
| 103 |
-
}
|
| 104 |
-
"""
|
| 105 |
-
)
|
| 106 |
-
) as blocks:
|
| 107 |
-
with gr.Row():
|
| 108 |
-
with gr.Column():
|
| 109 |
-
gr.Markdown("# ClimateGAN: Visualize Climate Change")
|
| 110 |
-
gr.HTML(
|
| 111 |
-
dedent(
|
| 112 |
-
"""
|
| 113 |
-
<p>
|
| 114 |
-
Climate change does not impact everyone equally.
|
| 115 |
-
This Space shows the effects of the climate emergency,
|
| 116 |
-
"one address at a time".
|
| 117 |
-
</p>
|
| 118 |
-
|
| 119 |
-
<p>
|
| 120 |
-
Visit the original experience at
|
| 121 |
-
<a href="https://thisclimatedoesnotexist.com/">
|
| 122 |
-
ThisClimateDoesNotExist.com
|
| 123 |
-
</a>
|
| 124 |
-
</p>
|
| 125 |
-
|
| 126 |
-
<br>
|
| 127 |
-
|
| 128 |
-
<p>
|
| 129 |
-
Enter an address or upload a Street View image, and ClimateGAN
|
| 130 |
-
will generate images showing how the location could be impacted
|
| 131 |
-
by flooding, wildfires, or smog if it happened there.
|
| 132 |
-
</p>
|
| 133 |
-
|
| 134 |
-
<br>
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
"""
|
| 148 |
-
|
| 149 |
-
<br>
|
| 150 |
-
<br>
|
| 151 |
-
<br>
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
<p style='text-align: center'>
|
| 155 |
-
Visit
|
| 156 |
-
<a href='https://thisclimatedoesnotexist.com/'>
|
| 157 |
-
ThisClimateDoesNotExist.com
|
| 158 |
-
</a>
|
| 159 |
-
for more information
|
| 160 |
-
|
|
| 161 |
-
Original
|
| 162 |
-
<a href='https://github.com/cc-ai/climategan'>
|
| 163 |
-
ClimateGAN GitHub Repo
|
| 164 |
-
</a>
|
| 165 |
-
</p>
|
| 166 |
-
|
| 167 |
-
<br>
|
| 168 |
-
|
| 169 |
-
<p>
|
| 170 |
-
After you have selected an image and started the inference you
|
| 171 |
-
will see all the outputs of ClimateGAN, including intermediate
|
| 172 |
-
outputs such as the flood mask, the segmentation map and the
|
| 173 |
-
depth maps used to produce the 3 events.
|
| 174 |
-
</p>
|
| 175 |
-
|
| 176 |
-
<br>
|
| 177 |
-
|
| 178 |
-
<p>
|
| 179 |
-
This Space makes use of recent Stable Diffusion in-painting
|
| 180 |
-
pipelines to replace ClimateGAN's original Painter. If you
|
| 181 |
-
select 'Both' painters, you will see a comparison
|
| 182 |
-
</p>
|
| 183 |
-
|
| 184 |
-
<br>
|
| 185 |
-
|
| 186 |
-
<br>
|
| 187 |
-
|
| 188 |
-
<p>
|
| 189 |
-
Read the original
|
| 190 |
-
<a
|
| 191 |
-
href='https://openreview.net/forum?id=EZNOb_uNpJk'
|
| 192 |
-
target='_blank'>
|
| 193 |
-
ICLR 2021 ClimateGAN paper
|
| 194 |
-
</a>
|
| 195 |
-
</p>
|
| 196 |
-
"""
|
| 197 |
-
)
|
| 198 |
-
)
|
| 199 |
-
with gr.Row():
|
| 200 |
-
gr.Markdown("## Inputs")
|
| 201 |
-
with gr.Row():
|
| 202 |
-
with gr.Column():
|
| 203 |
-
inputs = [gr.inputs.Image(label="Input Image")]
|
| 204 |
-
with gr.Column():
|
| 205 |
if api_key:
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
choices=[
|
| 210 |
"ClimateGAN Painter",
|
| 211 |
"Stable Diffusion Painter",
|
| 212 |
"Both",
|
| 213 |
],
|
| 214 |
label="Choose Flood Painter",
|
| 215 |
-
|
| 216 |
)
|
| 217 |
]
|
| 218 |
-
btn =
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
)
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
)
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
outputs.append(
|
| 237 |
-
gr.outputs.Image(type="numpy", label="ClimateGAN-Flooded image"),
|
| 238 |
-
)
|
| 239 |
-
outputs.append(
|
| 240 |
-
gr.outputs.Image(type="numpy", label="Stable Diffusion-Flooded image"),
|
| 241 |
-
)
|
| 242 |
-
outputs.append(
|
| 243 |
-
gr.outputs.Image(
|
| 244 |
type="numpy",
|
| 245 |
label="Stable Diffusion-Flooded image (restricted to masked area)",
|
| 246 |
)
|
| 247 |
-
|
| 248 |
-
with
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
)
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
blocks.launch()
|
|
|
|
| 2 |
# thank you @NimaBoscarino
|
| 3 |
|
| 4 |
import os
|
| 5 |
+
from textwrap import dedent
|
|
|
|
|
|
|
| 6 |
from urllib import parse
|
| 7 |
+
|
| 8 |
+
import googlemaps
|
| 9 |
+
import gradio as gr
|
| 10 |
import numpy as np
|
| 11 |
+
from gradio.components import (
|
| 12 |
+
HTML,
|
| 13 |
+
Button,
|
| 14 |
+
Column,
|
| 15 |
+
Dropdown,
|
| 16 |
+
Image,
|
| 17 |
+
Markdown,
|
| 18 |
+
Radio,
|
| 19 |
+
Row,
|
| 20 |
+
Textbox,
|
| 21 |
+
)
|
| 22 |
+
from skimage import io
|
| 23 |
+
|
| 24 |
from climategan_wrapper import ClimateGAN
|
| 25 |
+
|
| 26 |
+
HTMLS = [
|
| 27 |
+
dedent(
|
| 28 |
+
"""
|
| 29 |
+
<p>
|
| 30 |
+
Climate change does not impact everyone equally.
|
| 31 |
+
This Space shows the effects of the climate emergency,
|
| 32 |
+
"one address at a time".
|
| 33 |
+
</p>
|
| 34 |
+
<p>
|
| 35 |
+
Visit the original experience at
|
| 36 |
+
<a href="https://thisclimatedoesnotexist.com/">
|
| 37 |
+
ThisClimateDoesNotExist.com
|
| 38 |
+
</a>
|
| 39 |
+
</p>
|
| 40 |
+
<br>
|
| 41 |
+
<p>
|
| 42 |
+
Enter an address or upload a Street View image, and ClimateGAN
|
| 43 |
+
will generate images showing how the location could be impacted
|
| 44 |
+
by flooding, wildfires, or smog if it happened there.
|
| 45 |
+
</p>
|
| 46 |
+
<br>
|
| 47 |
+
<p>
|
| 48 |
+
This is <strong>not</strong> an exercise in climate prediction,
|
| 49 |
+
rather an exercise of empathy, to put yourself in other's shoes,
|
| 50 |
+
as if Climate Change came crushing on your doorstep.
|
| 51 |
+
</p>
|
| 52 |
+
"""
|
| 53 |
+
),
|
| 54 |
+
dedent(
|
| 55 |
+
"""
|
| 56 |
+
<br><br><br><br>
|
| 57 |
+
<p style='text-align: center'>
|
| 58 |
+
Visit
|
| 59 |
+
<a href='https://thisclimatedoesnotexist.com/'>
|
| 60 |
+
ThisClimateDoesNotExist.com
|
| 61 |
+
</a>
|
| 62 |
+
for more information
|
| 63 |
+
|
|
| 64 |
+
Original
|
| 65 |
+
<a href='https://github.com/cc-ai/climategan'>
|
| 66 |
+
ClimateGAN GitHub Repo
|
| 67 |
+
</a>
|
| 68 |
+
</p>
|
| 69 |
+
<br>
|
| 70 |
+
<p>
|
| 71 |
+
After you have selected an image and started the inference you
|
| 72 |
+
will see all the outputs of ClimateGAN, including intermediate
|
| 73 |
+
outputs such as the flood mask, the segmentation map and the
|
| 74 |
+
depth maps used to produce the 3 events.
|
| 75 |
+
</p>
|
| 76 |
+
<br>
|
| 77 |
+
<p>
|
| 78 |
+
This Space makes use of recent Stable Diffusion in-painting
|
| 79 |
+
pipelines to replace ClimateGAN's original Painter. If you
|
| 80 |
+
select 'Both' painters, you will see a comparison
|
| 81 |
+
</p>
|
| 82 |
+
<br>
|
| 83 |
+
<br>
|
| 84 |
+
<p>
|
| 85 |
+
Read the original
|
| 86 |
+
<a
|
| 87 |
+
href='https://openreview.net/forum?id=EZNOb_uNpJk'
|
| 88 |
+
target='_blank'>
|
| 89 |
+
ICLR 2021 ClimateGAN paper
|
| 90 |
+
</a>
|
| 91 |
+
</p>
|
| 92 |
+
"""
|
| 93 |
+
),
|
| 94 |
+
]
|
| 95 |
+
CSS = dedent(
|
| 96 |
+
"""
|
| 97 |
+
a {
|
| 98 |
+
color: #0088ff;
|
| 99 |
+
text-decoration: underline;
|
| 100 |
+
}
|
| 101 |
+
strong {
|
| 102 |
+
color: #c34318;
|
| 103 |
+
}
|
| 104 |
+
"""
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def toggle(radio):
|
| 109 |
+
if "address" in radio.lower():
|
| 110 |
+
return [
|
| 111 |
+
gr.update(visible=True),
|
| 112 |
+
gr.update(visible=False),
|
| 113 |
+
gr.update(visible=True),
|
| 114 |
+
]
|
| 115 |
+
else:
|
| 116 |
+
return [
|
| 117 |
+
gr.update(visible=False),
|
| 118 |
+
gr.update(visible=True),
|
| 119 |
+
gr.update(visible=True),
|
| 120 |
+
]
|
| 121 |
|
| 122 |
|
| 123 |
def predict(cg: ClimateGAN, api_key):
|
| 124 |
def _predict(*args):
|
| 125 |
+
image = place = painter = radio = None
|
| 126 |
+
if api_key:
|
| 127 |
+
radio, image, place, painter = args
|
|
|
|
| 128 |
else:
|
| 129 |
+
image, painter = args
|
|
|
|
| 130 |
|
| 131 |
+
if api_key and place and "address" in radio.lower():
|
| 132 |
geocode_result = gmaps.geocode(place)
|
| 133 |
|
| 134 |
address = geocode_result[0]["formatted_address"]
|
|
|
|
| 198 |
)
|
| 199 |
cg._setup_stable_diffusion()
|
| 200 |
|
| 201 |
+
radio = address = None
|
| 202 |
+
pred_ins = []
|
| 203 |
+
pred_outs = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
+
with gr.Blocks(css=CSS) as app:
|
| 206 |
+
with Row():
|
| 207 |
+
with Column():
|
| 208 |
+
Markdown("# ClimateGAN: Visualize Climate Change")
|
| 209 |
+
HTML(HTMLS[0])
|
| 210 |
+
with Column():
|
| 211 |
+
HTML(HTMLS[1])
|
| 212 |
+
with Row():
|
| 213 |
+
Markdown("## Inputs")
|
| 214 |
+
with Row():
|
| 215 |
+
with Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
if api_key:
|
| 217 |
+
radio = Radio(["From Address", "From Image"], label="Input Type")
|
| 218 |
+
pred_ins += [radio]
|
| 219 |
+
im_inp = Image(label="Input Image", visible=not api_key)
|
| 220 |
+
pred_ins += [im_inp]
|
| 221 |
+
if api_key:
|
| 222 |
+
address = Textbox(label="Address or place name", visible=False)
|
| 223 |
+
pred_ins += [address]
|
| 224 |
+
with Column():
|
| 225 |
+
pred_ins += [
|
| 226 |
+
Dropdown(
|
| 227 |
choices=[
|
| 228 |
"ClimateGAN Painter",
|
| 229 |
"Stable Diffusion Painter",
|
| 230 |
"Both",
|
| 231 |
],
|
| 232 |
label="Choose Flood Painter",
|
| 233 |
+
value="Both",
|
| 234 |
)
|
| 235 |
]
|
| 236 |
+
btn = Button(
|
| 237 |
+
"See for yourself!",
|
| 238 |
+
label="Run",
|
| 239 |
+
variant="primary",
|
| 240 |
+
visible=not api_key,
|
| 241 |
+
)
|
| 242 |
+
with Row():
|
| 243 |
+
Markdown("## Outputs")
|
| 244 |
+
with Row():
|
| 245 |
+
pred_outs += [Image(type="numpy", label="Original image")]
|
| 246 |
+
pred_outs += [Image(type="numpy", label="Masked input image")]
|
| 247 |
+
pred_outs += [Image(type="numpy", label="Segmentation map")]
|
| 248 |
+
pred_outs += [Image(type="numpy", label="Depth map")]
|
| 249 |
+
with Row():
|
| 250 |
+
pred_outs += [Image(type="numpy", label="ClimateGAN-Flooded image")]
|
| 251 |
+
pred_outs += [Image(type="numpy", label="Stable Diffusion-Flooded image")]
|
| 252 |
+
pred_outs += [
|
| 253 |
+
Image(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
type="numpy",
|
| 255 |
label="Stable Diffusion-Flooded image (restricted to masked area)",
|
| 256 |
)
|
| 257 |
+
]
|
| 258 |
+
with Row():
|
| 259 |
+
pred_outs += [Image(type="numpy", label="Comparison of flood images")]
|
| 260 |
+
with Row():
|
| 261 |
+
pred_outs += [Image(type="numpy", label="Wildfire")]
|
| 262 |
+
pred_outs += [Image(type="numpy", label="Smog")]
|
| 263 |
+
Image(type="numpy", label="Empty on purpose", interactive=False)
|
| 264 |
+
btn.click(predict(cg, api_key), inputs=pred_ins, outputs=pred_outs)
|
| 265 |
+
|
| 266 |
+
if api_key:
|
| 267 |
+
radio.change(toggle, inputs=[radio], outputs=[address, im_inp, btn])
|
| 268 |
+
|
| 269 |
+
app.launch()
|
|
|
climategan_wrapper.py
CHANGED
|
@@ -241,7 +241,7 @@ class ClimateGAN:
|
|
| 241 |
"""
|
| 242 |
if self.dev_mode:
|
| 243 |
return {
|
| 244 |
-
"input":
|
| 245 |
"mask": np.random.randint(0, 255, (640, 640)),
|
| 246 |
"masked_input": np.random.randint(0, 255, (640, 640, 3)),
|
| 247 |
"climategan_flood": np.random.randint(0, 255, (640, 640, 3)),
|
|
|
|
| 241 |
"""
|
| 242 |
if self.dev_mode:
|
| 243 |
return {
|
| 244 |
+
"input": orig_image,
|
| 245 |
"mask": np.random.randint(0, 255, (640, 640)),
|
| 246 |
"masked_input": np.random.randint(0, 255, (640, 640, 3)),
|
| 247 |
"climategan_flood": np.random.randint(0, 255, (640, 640, 3)),
|