File size: 3,132 Bytes
c2164fe
 
 
 
 
 
283e984
c2164fe
 
8b8b671
 
 
 
 
 
 
 
c2164fe
8b8b671
 
c2164fe
8b8b671
 
 
 
c2164fe
 
 
8b8b671
 
 
 
 
 
c2164fe
 
 
8b8b671
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2164fe
 
 
8b8b671
c2164fe
 
8b8b671
 
c2164fe
 
8b8b671
 
 
c2164fe
8b8b671
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2164fe
 
 
8b8b671
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
#
# @File:   app.py
# @Author: Haozhe Xie
# @Date:   2024-03-02 16:30:00
# @Last Modified by: Haozhe Xie
# @Last Modified at: 2024-03-03 10:39:25
# @Email:  root@haozhexie.com

import logging
import os
import torch
import gradio as gr
import subprocess
import urllib.request
import ssl
import sys

# Fix: ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
ssl._create_default_https_context = ssl._create_unverified_context

sys.path.append(os.path.join(os.path.dirname(__file__), "citydreamer"))
# Import CityDreamer modules
# import citydreamer.model
# import citydreamer.inference


def setup_runtime_env():
    subprocess.call(["pip", "freeze"])
    ext_dir = os.path.join(os.path.dirname(__file__), "citydreamer", "extensions")
    for e in os.listdir(ext_dir):
        if not os.path.isdir(e):
            continue
        subprocess.call(["pip", "install", "."], workdir=os.path.join(ext_dir, e))


def get_models():
    if not os.path.exists("CityDreamer-Fgnd.pth"):
        urllib.request.urlretrieve(
            "https://huggingface.co/hzxie/city-dreamer/resolve/main/CityDreamer-Fgnd.pth",
            "CityDreamer-Fgnd.pth",
        )
    if not os.path.exists("CityDreamer-Bgnd.pth"):
        urllib.request.urlretrieve(
            "https://huggingface.co/hzxie/city-dreamer/resolve/main/CityDreamer-Bgnd.pth",
            "CityDreamer-Bgnd.pth",
        )

    bgm_ckpt = torch.load("CityDreamer-Bgnd.pth")
    fgm_ckpt = torch.load("CityDreamer-Fgnd.pth")
    bgm = citydreamer.model.GanCraftGenerator(bgm_ckpt["cfg"])
    fgm = citydreamer.model.GanCraftGenerator(fgm_ckpt["cfg"])
    if torch.cuda.is_available():
        fgm = torch.nn.DataParallel(fgm).cuda().eval()
        bgm = torch.nn.DataParallel(bgm).cuda().eval()

    return bgm, fgm


def get_generated_city(radius, altitude, azimuth):
    print(radius, altitude, azimuth)


def main(debug):
    title = "CityDreamer Demo 🏙️"
    with open("README.md", "r") as f:
        markdown = f.read()
        desc = markdown[markdown.rfind("---") + 3:]
    with open("ARTICLE.md", "r") as f:
        arti = f.read()

    app = gr.Interface(
        get_generated_city,
        [
            gr.Slider(
                128, 512, value=320, step=5, label="Camera Radius (m)"
            ),
            gr.Slider(
                256, 512, value=384, step=5, label="Camera Altitude (m)"
            ),
            gr.Slider(0, 360, value=180, step=5, label="Camera Azimuth (°)"),
        ],
        [gr.Image(type="numpy", label="Generated City")],
        title=title,
        description=desc,
        article=arti,
        allow_flagging="never",
    )
    app.queue(api_open=False)
    app.launch(debug=debug)


if __name__ == "__main__":
    logging.basicConfig(
        format="[%(levelname)s] %(asctime)s %(message)s", level=logging.INFO
    )
    logging.info("Compile CUDA extensions...")
    # setup_runtime_env()
    logging.info("Downloading pretrained models...")
    # fgm, bgm = get_models()
    logging.info("Starting the main application...")
    main(os.getenv("DEBUG") == "1")