-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.py
51 lines (38 loc) · 1.13 KB
/
webserver.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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from musigen.core.evolution import Evolution
from musigen.core.population import Population
from musigen.api.hash import decodeUrl, encodeUrl
app = FastAPI()
origins = [
"http://localhost",
"http://localhost:8000",
"http://localhost:8080",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["get"],
allow_headers=["*"],
)
def main(grid_hash: str) -> str:
mutation_probability = 0.05
num_mutations = 5
evo = Evolution(
mutation_probability=mutation_probability,
num_mutation_rounds=num_mutations,
fitness_limit=5,
generation_limit=3,
)
ppl = Population.from_hash(grid_hash)
ppl.genomes = evo.run_evolution(ppl)
return ppl.to_hash()
@app.get("/{synthpad_data_url}")
def read_item(synthpad_data_url: str):
if synthpad_data_url == "favicon.ico":
return
grid_hash, scale, bpm = decodeUrl(synthpad_data_url)
updated_grid_hash = main(grid_hash)
return encodeUrl(updated_grid_hash, scale, bpm)
@app.post()