-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
149 lines (121 loc) · 3.79 KB
/
main.py
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
from typing import List
import numpy as np
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.utils.run_model import run
from app.utils.training_seeds import get_training_seeds
from app.utils.behaviour import get_extremes_behaviour0_char, get_extremes_behaviour1_char, get_behaviour0_char, get_behaviour1_char, get_obj_char
from app.utils.get_path import list_models_folders, get_model_settings
LOCAL = False
app = FastAPI(title='Interactive NCA API',
description='API endpoints for the Interactive NCA model')
origins = [
"http://localhost:3000",
"https://localhost",
"https://interactive-nca-ui-lukyrasocha.vercel.app/",
"https://interactive-nca-ui.vercel.app/",
"https://interactive-nca-ui-lukyrasocha.vercel.app",
"https://interactive-nca-ui.vercel.app",
"https://www.zeldalevelcraft.com/",
"https://www.zeldalevelcraft.com"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Hello world endpoint
@app.get("/")
def read_root():
return {"Interactive": "NCA"}
# Generate a level from some given seed
@app.post("/generate")
async def generate(exp_id: int, path_length: float, symmetry: float, input_map: List[List[List[int]]]):
"""
Generate a level from a given seed
Args:
exp_id: int
path_length: float
symmetry: float
input_map: List[List[int]]
Returns:
generated_map: List[List[int]]
"""
input_array = np.array(input_map[0]) # 2D int encoded level
binary_array = np.array(input_map[1]) # binary array noting which tiles are fixed
combined = np.array([input_array, binary_array])
x = run(exp_id, symmetry, path_length, combined, LOCAL)
return {"generated_map": x}
# Get behaviour characteristics
@app.get("/extreme-behaviour0")
def get_extreme_behaviour0():
"""
Get the extreme behaviour0 characteristics
Returns:
extreme0: List[float]
"""
return {"extreme0": get_extremes_behaviour0_char()}
@app.get("/extreme-behaviour1")
def get_extreme_behaviour1():
"""
Get the extreme behaviour1 characteristics
Returns:
extreme1: List[float]
"""
return {"extreme1": get_extremes_behaviour1_char()}
@app.get("/behaviour0")
def get_behaviour0():
"""
Get the behaviour0 characteristics
Returns:
behaviour0: List[float]
"""
return {"behaviour0": get_behaviour0_char()}
@app.get("/behaviour1")
def get_behaviour1():
"""
Get the behaviour1 characteristics
Returns:
behaviour1: List[float]
"""
return {"behaviour1": get_behaviour1_char()}
@app.get("/behaviours")
def get_both_and_obj(exp_id: int):
"""
Get the behaviour0 and behaviour1 characteristics and the objective value
Returns:
behaviours: List[List[float], List[float], List[float]]
"""
return {"behaviours": get_obj_char(exp_id, LOCAL)}
@app.get("/experimentnames")
def experiment_names():
"""
Get experiment names
Returns:
experiments_names: List
"""
return {"names": list_models_folders(LOCAL)}
@app.get("/trainingseeds")
async def get_training_seeds_endpoint(exp_id: int, path_length: float, symmetry: float):
"""
Generate a level from a given seed
Args:
exp_id: int
path_length: float
symmetry: float
Returns:
training_seeds for selected model
"""
x = get_training_seeds(exp_id, symmetry, path_length, LOCAL)
return {"training_seeds": x}
@app.get("/experimentdescriptions")
async def get_exp_desc(exp_id: int):
"""
Return experiment's description
Returns:
desc: str
"""
settings = get_model_settings(exp_id, LOCAL)
return {"desc": settings["settings_to_log"]["description"]}