HemanM commited on
Commit
e6e291b
·
verified ·
1 Parent(s): 0c388fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -1,7 +1,7 @@
1
  # app.py
2
  import math, json, random, time, threading, io, os
3
  from dataclasses import dataclass, asdict
4
- from typing import List, Tuple, Dict, Any
5
  import numpy as np
6
  import plotly.graph_objs as go
7
  import gradio as gr
@@ -185,7 +185,7 @@ class EvoRunner:
185
  g.fitness = fitness_hook(g, dataset, explore)
186
 
187
  history: List[Tuple[int,float]] = []
188
- best_overall: Genome | None = None
189
 
190
  for gen in range(1, generations+1):
191
  if self.stop_flag: break
@@ -285,14 +285,12 @@ def stop_evo():
285
  def poll_state():
286
  with runner.lock:
287
  s = runner.state.copy()
288
- # Defaults before first run
289
  sphere = s.get("sphere", go.Figure())
290
  history = s.get("history", go.Figure())
291
  best = s.get("best", {})
292
  gen = s.get("gen", 0)
293
  dataset = s.get("dataset", "Demo (Surrogate)")
294
  top = s.get("top", [])
295
- # Build stats Markdown
296
  if best:
297
  stats_md = (
298
  f"**Dataset:** {dataset} \n"
@@ -305,8 +303,6 @@ def poll_state():
305
  )
306
  else:
307
  stats_md = "Waiting… click **Start Evolution**."
308
-
309
- # Dataframe rows
310
  import pandas as pd
311
  df = pd.DataFrame(top)
312
  return sphere, history, stats_md, df
@@ -314,7 +310,6 @@ def poll_state():
314
  def export_snapshot():
315
  with runner.lock:
316
  payload = json.dumps(runner.state, default=lambda o: o, indent=2)
317
- # Write to a temp file so user can download
318
  path = "evo_snapshot.json"
319
  with open(path, "w", encoding="utf-8") as f:
320
  f.write(payload)
@@ -373,8 +368,12 @@ with gr.Blocks(theme=gr.themes.Soft(), css=CUSTOM_CSS) as demo:
373
  stop.click(stop_evo, [], [start, stop])
374
  export_btn.click(export_snapshot, [], [export_file])
375
 
376
- # Live polling
377
- demo.load(poll_state, None, [sphere_plot, hist_plot, stats_md, top_df], every=0.7)
 
 
 
 
378
 
379
  if __name__ == "__main__":
380
  demo.launch()
 
1
  # app.py
2
  import math, json, random, time, threading, io, os
3
  from dataclasses import dataclass, asdict
4
+ from typing import List, Tuple, Dict, Any, Optional
5
  import numpy as np
6
  import plotly.graph_objs as go
7
  import gradio as gr
 
185
  g.fitness = fitness_hook(g, dataset, explore)
186
 
187
  history: List[Tuple[int,float]] = []
188
+ best_overall: Optional[Genome] = None
189
 
190
  for gen in range(1, generations+1):
191
  if self.stop_flag: break
 
285
  def poll_state():
286
  with runner.lock:
287
  s = runner.state.copy()
 
288
  sphere = s.get("sphere", go.Figure())
289
  history = s.get("history", go.Figure())
290
  best = s.get("best", {})
291
  gen = s.get("gen", 0)
292
  dataset = s.get("dataset", "Demo (Surrogate)")
293
  top = s.get("top", [])
 
294
  if best:
295
  stats_md = (
296
  f"**Dataset:** {dataset} \n"
 
303
  )
304
  else:
305
  stats_md = "Waiting… click **Start Evolution**."
 
 
306
  import pandas as pd
307
  df = pd.DataFrame(top)
308
  return sphere, history, stats_md, df
 
310
  def export_snapshot():
311
  with runner.lock:
312
  payload = json.dumps(runner.state, default=lambda o: o, indent=2)
 
313
  path = "evo_snapshot.json"
314
  with open(path, "w", encoding="utf-8") as f:
315
  f.write(payload)
 
368
  stop.click(stop_evo, [], [start, stop])
369
  export_btn.click(export_snapshot, [], [export_file])
370
 
371
+ # Initial paint once when app loads
372
+ demo.load(poll_state, None, [sphere_plot, hist_plot, stats_md, top_df])
373
+
374
+ # Continuous polling (every 0.7s)
375
+ poller = gr.Timer(0.7)
376
+ poller.tick(poll_state, None, [sphere_plot, hist_plot, stats_md, top_df])
377
 
378
  if __name__ == "__main__":
379
  demo.launch()