# 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
from inferences import ClimateGAN
def predict(api_key):
def _predict(*args):
print("args: ", args)
image = place = None
if len(args) == 1:
image = args[0]
else:
assert len(args) == 2, "Unknown number of inputs {}".format(len(args))
image, place = 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
flood, wildfire, smog = model.inference(img_np)
return img_np, flood, 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)
model = ClimateGAN(model_path="config/model/masker")
inputs = inputs = [gr.inputs.Image(label="Input Image")]
if api_key:
inputs += [gr.inputs.Textbox(label="Address or place name")]
gr.Interface(
predict(api_key),
inputs=[
gr.inputs.Textbox(label="Address or place name"),
gr.inputs.Image(label="Input Image"),
],
outputs=[
gr.outputs.Image(type="numpy", label="Original image"),
gr.outputs.Image(type="numpy", label="Flooding"),
gr.outputs.Image(type="numpy", label="Wildfire"),
gr.outputs.Image(type="numpy", label="Smog"),
],
title="ClimateGAN: Visualize Climate Change",
description='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 ThisClimateDoesNotExist.com.
Enter an address or place name, and ClimateGAN will generate images showing how the location could be impacted by flooding, wildfires, or smog.', # noqa: E501
article="
This project is an unofficial clone of ThisClimateDoesNotExist | ClimateGAN GitHub Repo
", # noqa: E501 # examples=[ # "Vancouver Art Gallery", # "Chicago Bean", # "Duomo Siracusa", # ], css=".footer{display:none !important}", ).launch()