File size: 6,831 Bytes
ce190ee
 
 
 
 
 
 
 
ae61fe0
cd31093
ce190ee
 
ae61fe0
ce190ee
ae61fe0
 
ce190ee
ae61fe0
ce190ee
ae61fe0
 
ce190ee
 
 
 
 
 
 
 
 
fe58151
 
e2d1ef3
fe58151
e2d1ef3
fe58151
 
490814b
ae61fe0
 
 
 
 
9da944e
 
ae61fe0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9da944e
 
ae61fe0
 
 
 
 
 
 
ce190ee
 
 
 
 
 
 
 
 
 
 
b75254c
 
95ba8da
b75254c
ae61fe0
ce190ee
ae61fe0
 
 
 
 
95ba8da
 
ae61fe0
 
 
95ba8da
 
 
 
ae61fe0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9da944e
 
 
 
 
 
ae61fe0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# based on https://huggingface.co/spaces/NimaBoscarino/climategan/blob/main/app.py # noqa: E501
# thank you @NimaBoscarino

import os
import gradio as gr
import googlemaps
from skimage import io
from urllib import parse
import numpy as np
from climategan_wrapper import ClimateGAN


def predict(cg: ClimateGAN, api_key):
    def _predict(*args):
        image = place = painter = None
        if len(args) == 2:
            image = args[0]
            painter = args[1]
        else:
            assert len(args) == 3, "Unknown number of inputs {}".format(len(args))
            image, place, painter = args

        if api_key and place:
            geocode_result = gmaps.geocode(place)

            address = geocode_result[0]["formatted_address"]
            static_map_url = f"https://maps.googleapis.com/maps/api/streetview?size=640x640&location={parse.quote(address)}&source=outdoor&key={api_key}"
            img_np = io.imread(static_map_url)
        else:
            img_np = image

        painters = {
            "ClimateGAN Painter": "climategan",
            "Stable Diffusion Painter": "stable_diffusion",
            "Both": "both",
        }

        output_dict = cg.infer_single(img_np, painters[painter], as_pil_image=True)

        input_image = output_dict["input"]
        masked_input = output_dict["masked_input"]
        wildfire = output_dict["wildfire"]
        smog = output_dict["smog"]
        depth = np.repeat(output_dict["depth"][..., None], 3, axis=-1)
        segmentation = output_dict["segmentation"]

        climategan_flood = output_dict.get(
            "climategan_flood",
            np.ones(input_image.shape) * 255,
        )
        stable_flood = output_dict.get(
            "stable_flood",
            np.ones(input_image.shape) * 255,
        )
        stable_copy_flood = output_dict.get(
            "stable_copy_flood",
            np.ones(input_image.shape) * 255,
        )
        concat = output_dict.get(
            "concat",
            np.ones(input_image.shape) * 255,
        )

        return (
            input_image,
            masked_input,
            segmentation,
            depth,
            climategan_flood,
            stable_flood,
            stable_copy_flood,
            concat,
            wildfire,
            smog,
        )

    return _predict


if __name__ == "__main__":

    api_key = os.environ.get("GMAPS_API_KEY")
    gmaps = None
    if api_key is not None:
        gmaps = googlemaps.Client(key=api_key)

    cg = ClimateGAN(
        model_path="config/model/masker",
        dev_mode=os.environ.get("CG_DEV_MODE", "").lower() == "true",
    )
    cg._setup_stable_diffusion()

    with gr.Blocks() as blocks:
        with gr.Row():
            with gr.Column():
                gr.Markdown("# ClimateGAN: Visualize Climate Change")
                gr.HTML(
                    'Climate change does not impact everyone equally. This Space shows the effects of the climate emergency, "one address at a time". Visit the original experience at <a href="https://thisclimatedoesnotexist.com/">ThisClimateDoesNotExist.com</a>.<br>Enter an address or upload a Street View image, and ClimateGAN will generate images showing how the location could be impacted by flooding, wildfires, or smog if it happened there.'  # noqa: E501
                    + "<br><br>This is <strong>not</strong> an exercise in climate prediction, rather an exercise of empathy, to put yourself in other's shoes, as if Climate Change came crushing on your doorstep."  # noqa: E501
                )
            with gr.Column():
                gr.HTML(
                    "<p style='text-align: center'>Visit <a href='https://thisclimatedoesnotexist.com/'>ThisClimateDoesNotExist</a> for more information. | Original <a href='https://github.com/cc-ai/climategan'>ClimateGAN GitHub Repo</a></p>"  # noqa: E501
                    + "<p>After you have selected an image and started the inference you will see all the outputs of ClimateGAN, including intermediate outputs such as the flood mask, the segmentation map and the depth maps used to produce the 3 events</p>"
                    + "<p>This Space makes use of recent Stable Diffusion in-painting pipelines to replace ClimateGAN's original Painter. If you select 'Both' painters, you will see a comparison</p>"
                    + "<p>Read the original <a href='https://openreview.net/forum?id=EZNOb_uNpJk', target='_blank'>ICLR 2021 ClimateGAN paper</a></p>"
                )
        with gr.Row():
            gr.Markdown("## Inputs")
        with gr.Row():
            with gr.Column():
                inputs = [gr.inputs.Image(label="Input Image")]
            with gr.Column():
                if api_key:
                    inputs += [gr.inputs.Textbox(label="Address or place name")]
                inputs += [
                    gr.inputs.Dropdown(
                        choices=[
                            "ClimateGAN Painter",
                            "Stable Diffusion Painter",
                            "Both",
                        ],
                        label="Choose Flood Painter",
                        default="Both",
                    )
                ]
                btn = gr.Button("See for yourself!", label="Run")
        with gr.Row():
            gr.Markdown("## Outputs")
        with gr.Row():
            outputs = []
            outputs.append(
                gr.outputs.Image(type="numpy", label="Original image"),
            )
            outputs.append(
                gr.outputs.Image(type="numpy", label="Masked input image"),
            )
            outputs.append(
                gr.outputs.Image(type="numpy", label="Segmentation map"),
            )
            outputs.append(
                gr.outputs.Image(type="numpy", label="Depth map"),
            )
        with gr.Row():
            outputs.append(
                gr.outputs.Image(type="numpy", label="ClimateGAN-Flooded image"),
            )
            outputs.append(
                gr.outputs.Image(type="numpy", label="Stable Diffusion-Flooded image"),
            )
            outputs.append(
                gr.outputs.Image(
                    type="numpy",
                    label="Stable Diffusion-Flooded image (restricted to masked area)",
                )
            ),
        with gr.Row():
            outputs.append(
                gr.outputs.Image(type="numpy", label="Comparison of previous images"),
            )
        with gr.Row():
            outputs.append(
                gr.outputs.Image(type="numpy", label="Wildfire"),
            )
            outputs.append(
                gr.outputs.Image(type="numpy", label="Smog"),
            )
        btn.click(predict(cg, api_key), inputs=inputs, outputs=outputs)
    blocks.launch()