Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ import matplotlib.pyplot as plt
|
|
4 |
import numpy as np
|
5 |
import gradio as gr
|
6 |
|
7 |
-
# Define Agent and Swarm classes based on fractal geometry
|
8 |
class Agent:
|
9 |
def __init__(self, id, api_key=None):
|
10 |
self.id = id
|
@@ -16,15 +15,18 @@ class Agent:
|
|
16 |
if self.task:
|
17 |
print(f"Agent {self.id} is making an API call to '{self.task}'")
|
18 |
headers = {"Authorization": f"Bearer {self.api_key}"} if self.api_key else {}
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
25 |
|
26 |
def communicate(self, other_agents):
|
27 |
-
# Communication could be extended for more complex scenarios
|
28 |
pass
|
29 |
|
30 |
class Swarm:
|
@@ -34,7 +36,6 @@ class Swarm:
|
|
34 |
print(f"Swarm created with {num_agents} agents using the {fractal_pattern} pattern.")
|
35 |
|
36 |
def assign_tasks(self, tasks):
|
37 |
-
# Assign tasks to agents based on fractal pattern
|
38 |
for i, task in enumerate(tasks):
|
39 |
self.agents[i % len(self.agents)].task = task
|
40 |
print(f"Task assigned to Agent {self.agents[i % len(self.agents)].id}: {task}")
|
@@ -48,26 +49,21 @@ class Swarm:
|
|
48 |
def gather_results(self):
|
49 |
return [agent.results for agent in self.agents if agent.results]
|
50 |
|
51 |
-
# Generate tasks for the swarm
|
52 |
def generate_tasks(api_url, num_tasks):
|
53 |
return [api_url] * num_tasks
|
54 |
|
55 |
-
# Function to plot points in a pentagonal pattern and mirror them orthogonally
|
56 |
def plot_pentagonal_and_mirrored(results):
|
57 |
fig, ax = plt.subplots()
|
58 |
ax.set_aspect('equal')
|
59 |
|
60 |
-
# Define pentagon vertices
|
61 |
angle = 2 * np.pi / 5
|
62 |
radius = 1
|
63 |
pentagon_points = np.array([(radius * np.cos(i * angle), radius * np.sin(i * angle)) for i in range(5)])
|
64 |
|
65 |
-
# Plot pentagon
|
66 |
for i in range(5):
|
67 |
ax.plot([pentagon_points[i][0], pentagon_points[(i + 1) % 5][0]],
|
68 |
[pentagon_points[i][1], pentagon_points[(i + 1) % 5][1]], 'k-')
|
69 |
|
70 |
-
# Define inner points for 9-agent spread
|
71 |
center = np.array([0, 0])
|
72 |
points = [
|
73 |
center,
|
@@ -81,12 +77,10 @@ def plot_pentagonal_and_mirrored(results):
|
|
81 |
(pentagon_points[2] + pentagon_points[3]) / 2
|
82 |
]
|
83 |
|
84 |
-
# Plot points and results, along with their mirrored counterparts
|
85 |
for i, point in enumerate(points[:len(results)]):
|
86 |
ax.plot(point[0], point[1], 'bo')
|
87 |
ax.text(point[0], point[1], results[i], fontsize=9, ha='right')
|
88 |
|
89 |
-
# Mirrored points
|
90 |
mirrored_x = [-point[0], point[0]]
|
91 |
mirrored_y = [-point[1], point[1]]
|
92 |
|
@@ -98,32 +92,26 @@ def plot_pentagonal_and_mirrored(results):
|
|
98 |
|
99 |
plt.show()
|
100 |
|
101 |
-
# Function to run the swarm and plot results
|
102 |
def run_swarm(api_url, api_key, num_agents, num_tasks):
|
103 |
-
# Create a swarm with a fractal pattern (Pentagonal spread)
|
104 |
swarm = Swarm(num_agents=num_agents, fractal_pattern="Pentagonal", api_key=api_key)
|
105 |
tasks = generate_tasks(api_url, num_tasks)
|
106 |
swarm.assign_tasks(tasks)
|
107 |
swarm.execute()
|
108 |
|
109 |
-
# Gather results
|
110 |
results = swarm.gather_results()
|
111 |
|
112 |
-
# Print all results
|
113 |
print("\nAll results retrieved by the swarm:")
|
114 |
for i, result in enumerate(results):
|
115 |
print(f"Result {i + 1}: {result}")
|
116 |
|
117 |
-
# Plot the results in a pentagonal pattern with mirrored points
|
118 |
if results:
|
119 |
plot_pentagonal_and_mirrored(results)
|
120 |
|
121 |
return results
|
122 |
|
123 |
-
# Gradio interface
|
124 |
def gradio_interface(api_url, api_key, num_agents, num_tasks):
|
125 |
results = run_swarm(api_url, api_key, num_agents, num_tasks)
|
126 |
-
return results
|
127 |
|
128 |
iface = gr.Interface(
|
129 |
fn=gradio_interface,
|
@@ -138,4 +126,4 @@ iface = gr.Interface(
|
|
138 |
description="Enter the API URL, API Key (Optional), number of agents, and number of API calls. The results will be plotted in a pentagonal pattern with mirrored points."
|
139 |
)
|
140 |
|
141 |
-
iface.launch(
|
|
|
4 |
import numpy as np
|
5 |
import gradio as gr
|
6 |
|
|
|
7 |
class Agent:
|
8 |
def __init__(self, id, api_key=None):
|
9 |
self.id = id
|
|
|
15 |
if self.task:
|
16 |
print(f"Agent {self.id} is making an API call to '{self.task}'")
|
17 |
headers = {"Authorization": f"Bearer {self.api_key}"} if self.api_key else {}
|
18 |
+
try:
|
19 |
+
response = requests.get(self.task, headers=headers)
|
20 |
+
if response.status_code == 200:
|
21 |
+
self.results = response.json().get('data')[0]
|
22 |
+
else:
|
23 |
+
self.results = "Error: Unable to fetch data"
|
24 |
+
print(f"Agent {self.id} received: {self.results}")
|
25 |
+
except Exception as e:
|
26 |
+
self.results = f"Error: {str(e)}"
|
27 |
+
print(f"Agent {self.id} encountered an error: {str(e)}")
|
28 |
|
29 |
def communicate(self, other_agents):
|
|
|
30 |
pass
|
31 |
|
32 |
class Swarm:
|
|
|
36 |
print(f"Swarm created with {num_agents} agents using the {fractal_pattern} pattern.")
|
37 |
|
38 |
def assign_tasks(self, tasks):
|
|
|
39 |
for i, task in enumerate(tasks):
|
40 |
self.agents[i % len(self.agents)].task = task
|
41 |
print(f"Task assigned to Agent {self.agents[i % len(self.agents)].id}: {task}")
|
|
|
49 |
def gather_results(self):
|
50 |
return [agent.results for agent in self.agents if agent.results]
|
51 |
|
|
|
52 |
def generate_tasks(api_url, num_tasks):
|
53 |
return [api_url] * num_tasks
|
54 |
|
|
|
55 |
def plot_pentagonal_and_mirrored(results):
|
56 |
fig, ax = plt.subplots()
|
57 |
ax.set_aspect('equal')
|
58 |
|
|
|
59 |
angle = 2 * np.pi / 5
|
60 |
radius = 1
|
61 |
pentagon_points = np.array([(radius * np.cos(i * angle), radius * np.sin(i * angle)) for i in range(5)])
|
62 |
|
|
|
63 |
for i in range(5):
|
64 |
ax.plot([pentagon_points[i][0], pentagon_points[(i + 1) % 5][0]],
|
65 |
[pentagon_points[i][1], pentagon_points[(i + 1) % 5][1]], 'k-')
|
66 |
|
|
|
67 |
center = np.array([0, 0])
|
68 |
points = [
|
69 |
center,
|
|
|
77 |
(pentagon_points[2] + pentagon_points[3]) / 2
|
78 |
]
|
79 |
|
|
|
80 |
for i, point in enumerate(points[:len(results)]):
|
81 |
ax.plot(point[0], point[1], 'bo')
|
82 |
ax.text(point[0], point[1], results[i], fontsize=9, ha='right')
|
83 |
|
|
|
84 |
mirrored_x = [-point[0], point[0]]
|
85 |
mirrored_y = [-point[1], point[1]]
|
86 |
|
|
|
92 |
|
93 |
plt.show()
|
94 |
|
|
|
95 |
def run_swarm(api_url, api_key, num_agents, num_tasks):
|
|
|
96 |
swarm = Swarm(num_agents=num_agents, fractal_pattern="Pentagonal", api_key=api_key)
|
97 |
tasks = generate_tasks(api_url, num_tasks)
|
98 |
swarm.assign_tasks(tasks)
|
99 |
swarm.execute()
|
100 |
|
|
|
101 |
results = swarm.gather_results()
|
102 |
|
|
|
103 |
print("\nAll results retrieved by the swarm:")
|
104 |
for i, result in enumerate(results):
|
105 |
print(f"Result {i + 1}: {result}")
|
106 |
|
|
|
107 |
if results:
|
108 |
plot_pentagonal_and_mirrored(results)
|
109 |
|
110 |
return results
|
111 |
|
|
|
112 |
def gradio_interface(api_url, api_key, num_agents, num_tasks):
|
113 |
results = run_swarm(api_url, api_key, num_agents, num_tasks)
|
114 |
+
return "\n".join(str(result) for result in results)
|
115 |
|
116 |
iface = gr.Interface(
|
117 |
fn=gradio_interface,
|
|
|
126 |
description="Enter the API URL, API Key (Optional), number of agents, and number of API calls. The results will be plotted in a pentagonal pattern with mirrored points."
|
127 |
)
|
128 |
|
129 |
+
iface.launch()
|