Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ from typing import Dict, Optional, List
|
|
| 4 |
import numpy as np
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
ARTIFACT = "
|
| 8 |
AUTHOR = "Liam Grinstead"
|
| 9 |
|
| 10 |
def sha512_str(s: str) -> str:
|
|
@@ -39,10 +39,81 @@ class Civilization:
|
|
| 39 |
self.age_grid=np.zeros_like(self.grid,dtype=np.uint16)
|
| 40 |
self.food_stock=np.zeros_like(self.grid,dtype=np.float32)
|
| 41 |
self.tick=0;self.worker_mem:Dict[str,WorkerMemory]={}
|
| 42 |
-
# seed forests/food/buildings/shops/males/females/teachers (same as before) …
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
def render_image(self):
|
| 48 |
H,W=self.grid.shape;img=np.zeros((H,W,3),dtype=np.uint8)
|
|
@@ -73,25 +144,40 @@ def start_engine(width,height,seed,male_init,female_init,teacher_init,
|
|
| 73 |
return f"Engine started {params.width}x{params.height}"
|
| 74 |
|
| 75 |
def stop_engine():global RUNNING;RUNNING=False;return "Engine stopped."
|
| 76 |
-
def reset_engine():
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
def get_grid(scale):
|
| 79 |
with ENGINE_LOCK:
|
| 80 |
-
if SIM is None:
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
def get_entity_summary_html():
|
| 86 |
with ENGINE_LOCK:
|
| 87 |
-
if SIM is None:
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
# =========================
|
| 97 |
# Gradio UI
|
|
@@ -100,38 +186,50 @@ with gr.Blocks(title="RFT Civilization Engine — Livestream") as demo:
|
|
| 100 |
gr.Markdown("# RFT Conscious Civilization — Livestream")
|
| 101 |
|
| 102 |
with gr.Row():
|
| 103 |
-
width=gr.Slider(64,256,value=128,step=16,label="Grid width")
|
| 104 |
-
height=gr.Slider(64,256,value=128,step=16,label="Grid height")
|
| 105 |
-
seed=gr.Number(value=7,label="Seed",precision=0)
|
| 106 |
-
tick_rate=gr.Slider(1,30,value=5,step=1,label="Ticks per second")
|
| 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 |
return get_grid(scale_val)
|
| 133 |
|
| 134 |
-
demo.timer(0.5,poll_grid,inputs=[scale,live_on],outputs=[img_out,stats_out])
|
| 135 |
|
| 136 |
-
if __name__=="__main__":
|
| 137 |
-
demo.launch(server_name="0.0.0.0",server_port=7860)
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
ARTIFACT = "RFT_Civilization_Engine_v9"
|
| 8 |
AUTHOR = "Liam Grinstead"
|
| 9 |
|
| 10 |
def sha512_str(s: str) -> str:
|
|
|
|
| 39 |
self.age_grid=np.zeros_like(self.grid,dtype=np.uint16)
|
| 40 |
self.food_stock=np.zeros_like(self.grid,dtype=np.float32)
|
| 41 |
self.tick=0;self.worker_mem:Dict[str,WorkerMemory]={}
|
|
|
|
| 42 |
|
| 43 |
+
# Seed forests and food
|
| 44 |
+
forest_mask=self.rng.random(self.grid.shape)<params.forest_fraction
|
| 45 |
+
self.grid[forest_mask]=FOREST
|
| 46 |
+
food_mask=(self.rng.random(self.grid.shape)<params.food_fraction)&(self.grid==EMPTY)
|
| 47 |
+
self.grid[food_mask]=FOOD;self.food_stock[food_mask]=1.0
|
| 48 |
+
|
| 49 |
+
# Seed buildings
|
| 50 |
+
for _ in range(params.building_seed):
|
| 51 |
+
y=self.rng.integers(0,params.height);x=self.rng.integers(0,params.width)
|
| 52 |
+
self.grid[y,x]=BUILDING
|
| 53 |
+
|
| 54 |
+
# Seed shops near buildings
|
| 55 |
+
shop_spots=np.argwhere(self.grid==BUILDING)
|
| 56 |
+
for (y,x) in shop_spots[:max(1,int(len(shop_spots)*params.shop_ratio))]:
|
| 57 |
+
for ny,nx in self._neighbors8(y,x):
|
| 58 |
+
if self.grid[ny,nx]==EMPTY and self.rng.random()<0.5:
|
| 59 |
+
self.grid[ny,nx]=SHOP
|
| 60 |
+
|
| 61 |
+
# Seed males & females
|
| 62 |
+
empties=np.argwhere(self.grid==EMPTY);self.rng.shuffle(empties)
|
| 63 |
+
for (y,x) in empties[:params.male_init]:self.grid[y,x]=MALE
|
| 64 |
+
for (y,x) in empties[params.male_init:params.male_init+params.female_init]:self.grid[y,x]=FEMALE
|
| 65 |
+
|
| 66 |
+
# Seed teachers
|
| 67 |
+
remaining=[tuple(p) for p in empties[params.male_init+params.female_init:]]
|
| 68 |
+
self.rng.shuffle(remaining)
|
| 69 |
+
for (y,x) in remaining[:params.teacher_init]:self.grid[y,x]=TEACHER
|
| 70 |
+
|
| 71 |
+
def _neighbors8(self,y,x):
|
| 72 |
+
H,W=self.grid.shape
|
| 73 |
+
for dy in (-1,0,1):
|
| 74 |
+
for dx in (-1,0,1):
|
| 75 |
+
if dy==0 and dx==0:continue
|
| 76 |
+
ny,nx=(y+dy)%H,(x+dx)%W
|
| 77 |
+
yield ny,nx
|
| 78 |
+
|
| 79 |
+
def _mem_key(self,y,x):return f"{y},{x}"
|
| 80 |
+
def _ensure_worker_mem(self,y,x):
|
| 81 |
+
key=self._mem_key(y,x)
|
| 82 |
+
if key not in self.worker_mem:self.worker_mem[key]=WorkerMemory()
|
| 83 |
+
|
| 84 |
+
def step(self):
|
| 85 |
+
H,W=self.grid.shape
|
| 86 |
+
new_grid=self.grid.copy();new_age=self.age_grid.copy();new_food=self.food_stock.copy()
|
| 87 |
+
|
| 88 |
+
# Forest regen and consumption
|
| 89 |
+
new_food[self.grid==FOREST]+=0.01
|
| 90 |
+
pop_mask=(self.grid==MALE)|(self.grid==FEMALE)|(self.grid==BABY)|(self.grid==WORKER)
|
| 91 |
+
new_food[pop_mask]-=self.params.food_need_per_capita
|
| 92 |
+
new_food=np.clip(new_food,0.0,5.0)
|
| 93 |
+
|
| 94 |
+
for y in range(H):
|
| 95 |
+
for x in range(W):
|
| 96 |
+
cell=self.grid[y,x];nb=[(ny,nx) for ny,nx in self._neighbors8(y,x)]
|
| 97 |
+
nb_cells=np.array([self.grid[ny,nx] for ny,nx in nb],dtype=np.uint8)
|
| 98 |
+
nb_food=float(np.mean([self.food_stock[ny,nx] for ny,nx in nb]))
|
| 99 |
+
candidates=[(ny,nx) for (ny,nx),c in zip(nb,nb_cells) if c==EMPTY]
|
| 100 |
+
|
| 101 |
+
if cell in (MALE,FEMALE):
|
| 102 |
+
if MALE in nb_cells and FEMALE in nb_cells and self.rng.random()<self.params.pair_prob_base:
|
| 103 |
+
if candidates:ty,tx=candidates[self.rng.integers(0,len(candidates))];new_grid[ty,tx]=BABY
|
| 104 |
+
elif cell==BABY:
|
| 105 |
+
new_age[y,x]=self.age_grid[y,x]+1
|
| 106 |
+
if new_age[y,x]>=self.params.baby_age_ticks:
|
| 107 |
+
new_grid[y,x]=WORKER;self._ensure_worker_mem(y,x)
|
| 108 |
+
elif cell==WORKER:
|
| 109 |
+
self._ensure_worker_mem(y,x);mem=self.worker_mem[self._mem_key(y,x)]
|
| 110 |
+
mem.skill=min(1.0,mem.skill+0.01)
|
| 111 |
+
if candidates and self.rng.random()<0.5:
|
| 112 |
+
target=self.rng.choice([ROAD,HOUSE,FOOD])
|
| 113 |
+
ty,tx=candidates[self.rng.integers(0,len(candidates))];new_grid[ty,tx]=target
|
| 114 |
+
if target==FOOD:new_food[ty,tx]+=0.3
|
| 115 |
+
|
| 116 |
+
self.grid=new_grid;self.age_grid=new_age;self.food_stock=new_food;self.tick+=1
|
| 117 |
|
| 118 |
def render_image(self):
|
| 119 |
H,W=self.grid.shape;img=np.zeros((H,W,3),dtype=np.uint8)
|
|
|
|
| 144 |
return f"Engine started {params.width}x{params.height}"
|
| 145 |
|
| 146 |
def stop_engine():global RUNNING;RUNNING=False;return "Engine stopped."
|
| 147 |
+
def reset_engine():
|
| 148 |
+
stop_engine()
|
| 149 |
+
globals()["SIM"] = None
|
| 150 |
+
return "Reset complete."
|
| 151 |
|
| 152 |
def get_grid(scale):
|
| 153 |
with ENGINE_LOCK:
|
| 154 |
+
if SIM is None:
|
| 155 |
+
return None, "Engine not running."
|
| 156 |
+
img = SIM.render_image()
|
| 157 |
+
img_scaled = np.repeat(np.repeat(img, scale, axis=0), scale, axis=1)
|
| 158 |
+
g = SIM.grid
|
| 159 |
+
stats = f"Tick={SIM.tick} | Pop={int(np.sum(np.isin(g,[MALE,FEMALE,BABY,WORKER])))}"
|
| 160 |
+
return img_scaled, stats
|
| 161 |
|
| 162 |
def get_entity_summary_html():
|
| 163 |
with ENGINE_LOCK:
|
| 164 |
+
if SIM is None:
|
| 165 |
+
return "<p>Engine not running.</p>"
|
| 166 |
+
g = SIM.grid
|
| 167 |
+
rows = []
|
| 168 |
+
for name, code in [
|
| 169 |
+
("EMPTY", EMPTY), ("MALE", MALE), ("FEMALE", FEMALE), ("BABY", BABY), ("WORKER", WORKER),
|
| 170 |
+
("BUILDING", BUILDING), ("HOUSE", HOUSE), ("SHOP", SHOP), ("ROAD", ROAD),
|
| 171 |
+
("FOREST", FOREST), ("FOOD", FOOD), ("TEACHER", TEACHER), ("GUILD", GUILD), ("SCHOOL", SCHOOL)
|
| 172 |
+
]:
|
| 173 |
+
count = int(np.sum(g == code))
|
| 174 |
+
r, gcol, b = COLORS[name]
|
| 175 |
+
color = f"rgb({r},{gcol},{b})"
|
| 176 |
+
rows.append(
|
| 177 |
+
f"<tr><td style='background:{color};width:18px'></td>"
|
| 178 |
+
f"<td>{name}</td><td>{count}</td></tr>"
|
| 179 |
+
)
|
| 180 |
+
return "<table>" + "".join(rows) + "</table>"
|
| 181 |
|
| 182 |
# =========================
|
| 183 |
# Gradio UI
|
|
|
|
| 186 |
gr.Markdown("# RFT Conscious Civilization — Livestream")
|
| 187 |
|
| 188 |
with gr.Row():
|
| 189 |
+
width = gr.Slider(64, 256, value=128, step=16, label="Grid width")
|
| 190 |
+
height = gr.Slider(64, 256, value=128, step=16, label="Grid height")
|
| 191 |
+
seed = gr.Number(value=7, label="Seed", precision=0)
|
| 192 |
+
tick_rate = gr.Slider(1, 30, value=5, step=1, label="Ticks per second")
|
| 193 |
+
|
| 194 |
+
with gr.Row():
|
| 195 |
+
male_init = gr.Slider(50, 600, value=240, step=10, label="Initial males")
|
| 196 |
+
female_init = gr.Slider(50, 600, value=240, step=10, label="Initial females")
|
| 197 |
+
teacher_init = gr.Slider(1, 50, value=10, step=1, label="Initial teachers")
|
| 198 |
+
forest_fraction = gr.Slider(0.0, 0.6, value=0.16, step=0.02, label="Forest fraction")
|
| 199 |
+
food_fraction = gr.Slider(0.0, 0.3, value=0.06, step=0.01, label="Food fraction")
|
| 200 |
+
baby_age_ticks = gr.Slider(4, 24, value=8, step=1, label="Baby→Worker ticks")
|
| 201 |
+
override_sensitivity = gr.Slider(0.3, 0.9, value=0.60, step=0.05, label="Override sensitivity")
|
| 202 |
+
|
| 203 |
+
run_btn = gr.Button("Run simulation", variant="primary")
|
| 204 |
+
stop_btn = gr.Button("Stop")
|
| 205 |
+
reset_btn = gr.Button("Reset")
|
| 206 |
+
|
| 207 |
+
with gr.Accordion("Live grid", open=True):
|
| 208 |
+
scale = gr.Slider(1, 8, value=4, step=1, label="Display scale")
|
| 209 |
+
img_out = gr.Image(type="numpy", label="Grid")
|
| 210 |
+
stats_out = gr.Textbox(label="Stats", lines=2)
|
| 211 |
+
live_on = gr.Checkbox(label="Livestream updates", value=True)
|
| 212 |
+
|
| 213 |
+
with gr.Accordion("Entity key", open=False):
|
| 214 |
+
key_chart = gr.HTML()
|
| 215 |
+
key_btn = gr.Button("Update key chart")
|
| 216 |
+
|
| 217 |
+
run_btn.click(
|
| 218 |
+
start_engine,
|
| 219 |
+
inputs=[width, height, seed, male_init, female_init, teacher_init,
|
| 220 |
+
forest_fraction, food_fraction, baby_age_ticks, override_sensitivity, tick_rate],
|
| 221 |
+
outputs=[]
|
| 222 |
+
)
|
| 223 |
+
stop_btn.click(stop_engine, inputs=[], outputs=[])
|
| 224 |
+
reset_btn.click(reset_engine, inputs=[], outputs=[])
|
| 225 |
+
key_btn.click(get_entity_summary_html, inputs=[], outputs=[key_chart])
|
| 226 |
+
|
| 227 |
+
def poll_grid(scale_val, live_flag):
|
| 228 |
+
if not live_flag:
|
| 229 |
+
return gr.update(), gr.update()
|
| 230 |
return get_grid(scale_val)
|
| 231 |
|
| 232 |
+
demo.timer(0.5, poll_grid, inputs=[scale, live_on], outputs=[img_out, stats_out])
|
| 233 |
|
| 234 |
+
if __name__ == "__main__":
|
| 235 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|