RFTSystems commited on
Commit
f59b79f
·
verified ·
1 Parent(s): 1889b1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -60
app.py CHANGED
@@ -1,28 +1,26 @@
1
  import gradio as gr
2
  import json
3
  import os
4
- import matplotlib.pyplot as plt # Kept for potential future gr.Plot use or if figures are handled directly
5
  from simulation import RFTSimulation
6
  from utils import export_json, seed_everything
7
 
8
- # Conditionally import IPython.display only if in Colab
9
  IN_COLAB = 'COLAB_GPU' in os.environ or 'Google' in os.environ.get('COLAB_CLOUD_PLATFORM', '')
10
  if IN_COLAB:
11
- from IPython.display import Image, display # For Colab dev mode inline image display
 
 
 
 
12
 
13
- # Helper function to run simulation and generate plots/export data
14
  def run_simulation_and_plot(num_agents, steps, mutation_rate, drift_rate, seed=None):
15
  if seed is not None:
16
  seed_everything(seed)
17
 
18
- # Instantiate simulation with save_plots=True, so it saves plots during its run() method
19
  simulation = RFTSimulation(num_agents, steps, mutation_rate, drift_rate, save_plots=True)
20
-
21
- # Run the simulation. The internal run method will save plots if save_plots=True.
22
- # It returns all_agents_history, coherence_list, stability_list.
23
  all_agents_history, coherence_list, stability_list = simulation.run()
24
 
25
- # Prepare final agent states for JSON export
26
  final_agent_states = []
27
  for i, agent in enumerate(simulation.agents):
28
  final_agent_states.append({
@@ -33,11 +31,9 @@ def run_simulation_and_plot(num_agents, steps, mutation_rate, drift_rate, seed=N
33
  'fitness': agent.fitness
34
  })
35
 
36
- # Export JSON
37
  json_filename = "final_agent_states.json"
38
  export_json(final_agent_states, json_filename)
39
 
40
- # Return filenames of the generated plots and the JSON for Gradio or dev mode display
41
  plot_filenames = [
42
  'phi_plot.png',
43
  'tau_plot.png',
@@ -48,62 +44,46 @@ def run_simulation_and_plot(num_agents, steps, mutation_rate, drift_rate, seed=N
48
 
49
  return (*plot_filenames, json_filename)
50
 
51
- # Gradio Interface setup
52
- iface = gr.Interface(
53
- fn=run_simulation_and_plot,
54
- inputs=[
55
- gr.Slider(1, 10, value=3, step=1, label="Number of Agents"),
56
- gr.Slider(10, 500, value=100, step=10, label="Steps"),
57
- gr.Slider(0.01, 0.5, value=0.05, step=0.01, label="Mutation Rate"),
58
- gr.Slider(0.01, 0.5, value=0.02, step=0.01, label="Drift Rate"),
59
- gr.Number(value=42, label="Random Seed (optional, leave empty for random)") # Clarified label
60
- ],
61
- outputs=[
62
- gr.Image(label="Phi Plot"), # Changed to gr.Image to display saved PNGs
 
 
 
 
 
 
 
 
63
  gr.Image(label="Tau Effective Plot"),
64
  gr.Image(label="Fitness Plot"),
65
  gr.Image(label="Coherence Plot"),
66
  gr.Image(label="Stability Plot"),
67
  gr.File(label="Exported Final Agent States (JSON)")
68
- ],
69
- title="RFT Agent Simulation Engine MVP",
70
- description="Simulate RFT Agents and visualize their dynamics."
71
- )
72
 
73
- # Colab-friendly "dev mode" or Gradio launch logic
74
- # The IN_COLAB check is now done at the top for conditional import
 
 
 
 
 
 
 
75
 
76
  if IN_COLAB:
77
  print("Running in Colab development mode (skipping Gradio launch).")
78
- # Run the test simulation logic as per requirements
79
- num_agents = 3
80
- steps = 100
81
- mutation_rate = 0.05
82
- drift_rate = 0.02
83
- seed = 42
84
-
85
- print("\n--- Running Test Simulation in Dev Mode ---")
86
- # Call the core simulation function. It will save plots and JSON.
87
- plot_files_and_json_file = run_simulation_and_plot(num_agents, steps, mutation_rate, drift_rate, seed)
88
- plot_files = plot_files_and_json_file[:-1]
89
- json_file = plot_files_and_json_file[-1]
90
-
91
- print("\nSimulation complete. Displaying plots and exported JSON path:")
92
- for plot_file in plot_files:
93
- print(f" - {os.path.abspath(plot_file)}")
94
- if os.path.exists(plot_file):
95
- display(Image(filename=plot_file)) # Display inline in Colab
96
- else:
97
- print(f"Warning: Plot file {plot_file} not found for inline display.")
98
-
99
- print(f" - {os.path.abspath(json_file)}")
100
- print(f"Final agent states exported to {json_file}")
101
- print("\n--- End of Test Simulation in Dev Mode ---")
102
-
103
  else:
104
- print("Running Gradio interface. Call iface.launch() to start it.")
105
- # For local development, uncomment this to launch Gradio:
106
- # iface.launch()
107
-
108
- print("app.py updated successfully.")
109
- iface.launch()
 
1
  import gradio as gr
2
  import json
3
  import os
4
+ import matplotlib.pyplot as plt
5
  from simulation import RFTSimulation
6
  from utils import export_json, seed_everything
7
 
8
+ # Detect Colab
9
  IN_COLAB = 'COLAB_GPU' in os.environ or 'Google' in os.environ.get('COLAB_CLOUD_PLATFORM', '')
10
  if IN_COLAB:
11
+ from IPython.display import Image, display
12
+
13
+ # -----------------------------
14
+ # Core Simulation Function
15
+ # -----------------------------
16
 
 
17
  def run_simulation_and_plot(num_agents, steps, mutation_rate, drift_rate, seed=None):
18
  if seed is not None:
19
  seed_everything(seed)
20
 
 
21
  simulation = RFTSimulation(num_agents, steps, mutation_rate, drift_rate, save_plots=True)
 
 
 
22
  all_agents_history, coherence_list, stability_list = simulation.run()
23
 
 
24
  final_agent_states = []
25
  for i, agent in enumerate(simulation.agents):
26
  final_agent_states.append({
 
31
  'fitness': agent.fitness
32
  })
33
 
 
34
  json_filename = "final_agent_states.json"
35
  export_json(final_agent_states, json_filename)
36
 
 
37
  plot_filenames = [
38
  'phi_plot.png',
39
  'tau_plot.png',
 
44
 
45
  return (*plot_filenames, json_filename)
46
 
47
+ # -----------------------------
48
+ # PREMIUM UI (Always Unlocked)
49
+ # -----------------------------
50
+
51
+ with gr.Blocks(title="RFT Agent Simulation Engine — Premium") as iface:
52
+
53
+ gr.Markdown("# 🔥 RFT Agent Simulation Engine — Premium Edition")
54
+ gr.Markdown("Full‑power simulation engine with extended limits, advanced controls, and unrestricted exports.")
55
+
56
+ with gr.Box():
57
+ gr.Markdown("### Premium Simulation Controls")
58
+
59
+ num_agents = gr.Slider(1, 200, value=10, step=1, label="Number of Agents (Premium)")
60
+ steps = gr.Slider(10, 20000, value=500, step=10, label="Steps (Premium)")
61
+ mutation_rate = gr.Slider(0.001, 1.0, value=0.05, step=0.001, label="Mutation Rate")
62
+ drift_rate = gr.Slider(0.001, 1.0, value=0.02, step=0.001, label="Drift Rate")
63
+ seed = gr.Number(value=42, label="Random Seed")
64
+
65
+ outputs = [
66
+ gr.Image(label="Phi Plot"),
67
  gr.Image(label="Tau Effective Plot"),
68
  gr.Image(label="Fitness Plot"),
69
  gr.Image(label="Coherence Plot"),
70
  gr.Image(label="Stability Plot"),
71
  gr.File(label="Exported Final Agent States (JSON)")
72
+ ]
73
+
74
+ run_button = gr.Button("🚀 Run Premium Simulation")
 
75
 
76
+ run_button.click(
77
+ run_simulation_and_plot,
78
+ inputs=[num_agents, steps, mutation_rate, drift_rate, seed],
79
+ outputs=outputs
80
+ )
81
+
82
+ # -----------------------------
83
+ # Launch Logic
84
+ # -----------------------------
85
 
86
  if IN_COLAB:
87
  print("Running in Colab development mode (skipping Gradio launch).")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  else:
89
+ iface.launch()