Agnuxo commited on
Commit
346bbf5
·
verified ·
1 Parent(s): f120c7a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -29
app.py CHANGED
@@ -181,21 +181,6 @@ class NebulaEmergent:
181
  positions = np.array([n.position for n in self.neurons])
182
  self.kdtree = KDTree(positions)
183
 
184
- @jit(nopython=True)
185
- def compute_gravitational_forces_fast(positions, masses, forces):
186
- """Fast gravitational force computation using Numba"""
187
- n = len(positions)
188
- for i in prange(n):
189
- for j in range(i + 1, n):
190
- r = positions[j] - positions[i]
191
- r_mag = np.sqrt(np.sum(r * r))
192
- if r_mag > 1e-10:
193
- f_mag = G * masses[i] * masses[j] / (r_mag ** 2 + 1e-10)
194
- f = f_mag * r / r_mag
195
- forces[i] += f
196
- forces[j] -= f
197
- return forces
198
-
199
  def compute_gravitational_forces(self):
200
  """Compute gravitational forces using Barnes-Hut algorithm approximation"""
201
  if not self.gravity_enabled:
@@ -205,9 +190,17 @@ class NebulaEmergent:
205
  masses = np.array([n.mass for n in self.neurons])
206
  forces = np.zeros((self.n_neurons, 3))
207
 
208
- # Use fast computation for smaller systems
209
  if self.n_neurons < 5000:
210
- forces = self.compute_gravitational_forces_fast(positions, masses, forces)
 
 
 
 
 
 
 
 
211
  else:
212
  # Barnes-Hut approximation for larger systems
213
  # Group nearby neurons and treat as single mass
@@ -599,7 +592,9 @@ class NebulaInterface:
599
  def evolve_step(self):
600
  """Evolve system by one step"""
601
  if self.nebula is None:
602
- return "⚠️ Please create a system first", go.Figure(), go.Figure()
 
 
603
 
604
  self.nebula.evolve()
605
 
@@ -616,10 +611,11 @@ class NebulaInterface:
616
  def evolve_continuous(self, steps: int):
617
  """Evolve system continuously for multiple steps"""
618
  if self.nebula is None:
619
- return "⚠️ Please create a system first", go.Figure(), go.Figure()
 
620
 
621
  status_messages = []
622
- for i in range(steps):
623
  self.nebula.evolve()
624
 
625
  # Store metrics
@@ -633,17 +629,17 @@ class NebulaInterface:
633
  f"Clusters={self.nebula.metrics['clusters']}, "
634
  f"Emergence={self.nebula.metrics['emergence_score']:.3f}")
635
 
636
- return ("\\n".join(status_messages[-5:]),
637
  self.visualize_3d(),
638
  self.create_metrics_plot())
639
 
640
  def encode_image_problem(self, image):
641
  """Encode an image as a problem"""
642
  if self.nebula is None:
643
- return "⚠️ Please create a system first"
644
 
645
  if image is None:
646
- return "⚠️ Please upload an image"
647
 
648
  # Convert image to grayscale and resize
649
  from PIL import Image
@@ -656,13 +652,15 @@ class NebulaInterface:
656
  # Encode in system
657
  self.nebula.encode_problem(img_array)
658
 
659
- return f"✅ Image encoded into system"
660
 
661
  def solve_tsp(self, n_cities: int):
662
  """Solve Traveling Salesman Problem"""
663
  if self.nebula is None:
664
- return "⚠️ Please create a system first", go.Figure()
665
 
 
 
666
  # Generate random cities
667
  cities = np.random.random((n_cities, 2))
668
 
@@ -750,6 +748,9 @@ class NebulaInterface:
750
  def create_gradio_app():
751
  interface = NebulaInterface()
752
 
 
 
 
753
  with gr.Blocks(title="NEBULA EMERGENT - Physical Neural Computing") as app:
754
  gr.Markdown("""
755
  # 🌌 NEBULA EMERGENT - Physical Neural Computing System
@@ -779,17 +780,18 @@ def create_gradio_app():
779
  create_btn = gr.Button("🔨 Create System", variant="primary")
780
 
781
  gr.Markdown("### Evolution Control")
 
782
  step_btn = gr.Button("▶️ Single Step")
783
 
784
  with gr.Row():
785
- steps_input = gr.Number(value=100, label="Steps")
786
  run_btn = gr.Button("🏃 Run Multiple Steps", variant="primary")
787
 
788
  status_text = gr.Textbox(label="Status", lines=5)
789
 
790
  with gr.Column(scale=2):
791
- plot_3d = gr.Plot(label="3D Neuron Visualization")
792
- metrics_plot = gr.Plot(label="System Metrics")
793
 
794
  with gr.Tab("🧩 Problem Solving"):
795
  with gr.Row():
@@ -888,7 +890,7 @@ def create_gradio_app():
888
  encode_img_btn.click(
889
  interface.encode_image_problem,
890
  inputs=[image_input],
891
- outputs=[problem_status]
892
  )
893
 
894
  solve_tsp_btn.click(
 
181
  positions = np.array([n.position for n in self.neurons])
182
  self.kdtree = KDTree(positions)
183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
  def compute_gravitational_forces(self):
185
  """Compute gravitational forces using Barnes-Hut algorithm approximation"""
186
  if not self.gravity_enabled:
 
190
  masses = np.array([n.mass for n in self.neurons])
191
  forces = np.zeros((self.n_neurons, 3))
192
 
193
+ # Direct computation for smaller systems
194
  if self.n_neurons < 5000:
195
+ for i in range(self.n_neurons):
196
+ for j in range(i + 1, self.n_neurons):
197
+ r = positions[j] - positions[i]
198
+ r_mag = np.linalg.norm(r)
199
+ if r_mag > 1e-10:
200
+ f_mag = G * masses[i] * masses[j] / (r_mag ** 2 + 1e-10)
201
+ f = f_mag * r / r_mag
202
+ forces[i] += f
203
+ forces[j] -= f
204
  else:
205
  # Barnes-Hut approximation for larger systems
206
  # Group nearby neurons and treat as single mass
 
592
  def evolve_step(self):
593
  """Evolve system by one step"""
594
  if self.nebula is None:
595
+ # Auto-create system with default settings
596
+ self.nebula = NebulaEmergent(1000)
597
+ return "⚠️ System auto-created with 1000 neurons. Click again to evolve.", self.visualize_3d(), self.create_metrics_plot()
598
 
599
  self.nebula.evolve()
600
 
 
611
  def evolve_continuous(self, steps: int):
612
  """Evolve system continuously for multiple steps"""
613
  if self.nebula is None:
614
+ # Auto-create system with default settings
615
+ self.nebula = NebulaEmergent(1000)
616
 
617
  status_messages = []
618
+ for i in range(int(steps)): # Ensure steps is integer
619
  self.nebula.evolve()
620
 
621
  # Store metrics
 
629
  f"Clusters={self.nebula.metrics['clusters']}, "
630
  f"Emergence={self.nebula.metrics['emergence_score']:.3f}")
631
 
632
+ return ("\n".join(status_messages[-5:]),
633
  self.visualize_3d(),
634
  self.create_metrics_plot())
635
 
636
  def encode_image_problem(self, image):
637
  """Encode an image as a problem"""
638
  if self.nebula is None:
639
+ self.nebula = NebulaEmergent(5000)
640
 
641
  if image is None:
642
+ return "⚠️ Please upload an image", self.visualize_3d()
643
 
644
  # Convert image to grayscale and resize
645
  from PIL import Image
 
652
  # Encode in system
653
  self.nebula.encode_problem(img_array)
654
 
655
+ return f"✅ Image encoded into system", self.visualize_3d()
656
 
657
  def solve_tsp(self, n_cities: int):
658
  """Solve Traveling Salesman Problem"""
659
  if self.nebula is None:
660
+ self.nebula = NebulaEmergent(3000)
661
 
662
+ n_cities = int(n_cities) # Ensure integer
663
+
664
  # Generate random cities
665
  cities = np.random.random((n_cities, 2))
666
 
 
748
  def create_gradio_app():
749
  interface = NebulaInterface()
750
 
751
+ # Auto-initialize with default system
752
+ interface.nebula = NebulaEmergent(1000)
753
+
754
  with gr.Blocks(title="NEBULA EMERGENT - Physical Neural Computing") as app:
755
  gr.Markdown("""
756
  # 🌌 NEBULA EMERGENT - Physical Neural Computing System
 
780
  create_btn = gr.Button("🔨 Create System", variant="primary")
781
 
782
  gr.Markdown("### Evolution Control")
783
+ gr.Markdown("*System auto-creates if not initialized*")
784
  step_btn = gr.Button("▶️ Single Step")
785
 
786
  with gr.Row():
787
+ steps_input = gr.Number(value=100, label="Steps", precision=0)
788
  run_btn = gr.Button("🏃 Run Multiple Steps", variant="primary")
789
 
790
  status_text = gr.Textbox(label="Status", lines=5)
791
 
792
  with gr.Column(scale=2):
793
+ plot_3d = gr.Plot(label="3D Neuron Visualization", value=interface.visualize_3d())
794
+ metrics_plot = gr.Plot(label="System Metrics", value=interface.create_metrics_plot())
795
 
796
  with gr.Tab("🧩 Problem Solving"):
797
  with gr.Row():
 
890
  encode_img_btn.click(
891
  interface.encode_image_problem,
892
  inputs=[image_input],
893
+ outputs=[problem_status, solution_plot]
894
  )
895
 
896
  solve_tsp_btn.click(