File size: 1,838 Bytes
eaf2e33
 
 
 
 
c3a57cc
a8cd524
c3a57cc
a8cd524
 
eaf2e33
 
 
 
 
 
 
 
 
 
 
 
3582c8a
eaf2e33
 
 
 
 
 
 
 
 
 
 
 
 
2801e45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eaf2e33
 
 
 
 
 
 
 
2801e45
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
import random

import gradio as gr
import os

from os import path
import sys
sys.path.append(path.dirname(path.abspath(__file__)))


from src.olgen.ol_generator import VecOnlineGenerator
from src.olgen.olg_policy import RLGenPolicy
from src.smb.level import save_batch
from src.utils.filesys import getpath
from src.utils.img import make_img_sheet

import torch

device = 'cuda:0' if torch.cuda.is_available() else 'cpu'

def generate_and_play():
    path = 'models/example_policy'
    # 使用example policy做生成
    N, L = 8, 10
    plc = RLGenPolicy.from_path(path, device)
    generator = VecOnlineGenerator(plc, g_device=device)
    fd, _ = os.path.split(getpath(path))
    os.makedirs(fd, exist_ok=True)

    lvls = generator.generate(N, L)
    imgs = [lvl.to_img() for lvl in lvls]
    return imgs



with gr.Blocks(title="NCERL Demo") as demo:
    gr.Markdown(
        """
<div style="display: flex; justify-content: center; align-items: center; text-align: center;">
  <div>
    <h1>Negatively Correlated Ensemble RL</h1>
  </div>
</div>
<div style="display: flex; justify-content: center; align-items: center; text-align: center;">
    <div style="display:flex; gap: 0.25rem;" align="center">
        <a href="https://openreview.net/forum?id=iAW2EQXfwb"><img src='https://img.shields.io/badge/Paper-ICLR-red'></a>
        <a href='https://github.com/PneuC/NCERL-Diverse-PCG'><img src='https://img.shields.io/badge/Github-Code-blue'></a>
    </div>
</div>

![banner](/file=media/banner.png)
"""
    )
    gallery = gr.Gallery(
        label="Generated images", show_label=False, elem_id="gallery"
    , columns=[3], rows=[1], object_fit="contain", height="auto")
    btn = gr.Button("Generate levels", scale=0)

    btn.click(generate_and_play, None, gallery)

if __name__ == "__main__":
    demo.launch(allowed_paths=["./"])