Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
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
|
11 |
+
self.task = None
|
12 |
+
self.results = None
|
13 |
+
self.api_key = api_key
|
14 |
+
|
15 |
+
def execute_task(self):
|
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 |
+
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 |
+
|
26 |
+
def communicate(self, other_agents):
|
27 |
+
# Communication could be extended for more complex scenarios
|
28 |
+
pass
|
29 |
+
|
30 |
+
class Swarm:
|
31 |
+
def __init__(self, num_agents, fractal_pattern, api_key=None):
|
32 |
+
self.agents = [Agent(i, api_key) for i in range(num_agents)]
|
33 |
+
self.fractal_pattern = fractal_pattern
|
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}")
|
41 |
+
|
42 |
+
def execute(self):
|
43 |
+
for agent in self.agents:
|
44 |
+
agent.execute_task()
|
45 |
+
for agent in self.agents:
|
46 |
+
agent.communicate(self.agents)
|
47 |
+
|
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,
|
74 |
+
(center + pentagon_points[0]) / 2,
|
75 |
+
(center + pentagon_points[1]) / 2,
|
76 |
+
(center + pentagon_points[2]) / 2,
|
77 |
+
(center + pentagon_points[3]) / 2,
|
78 |
+
(center + pentagon_points[4]) / 2,
|
79 |
+
(pentagon_points[0] + pentagon_points[1]) / 2,
|
80 |
+
(pentagon_points[1] + pentagon_points[2]) / 2,
|
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 |
+
|
93 |
+
for mx in mirrored_x:
|
94 |
+
for my in mirrored_y:
|
95 |
+
if (mx, my) != (point[0], point[1]):
|
96 |
+
ax.plot(mx, my, 'ro')
|
97 |
+
ax.text(mx, my, results[i], fontsize=9, ha='right')
|
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,
|
130 |
+
inputs=[
|
131 |
+
gr.Textbox(label="API URL", placeholder="Enter the API URL"),
|
132 |
+
gr.Textbox(label="API Key (Optional)", placeholder="Enter the API Key"),
|
133 |
+
gr.Number(label="Number of Agents", value=9, precision=0),
|
134 |
+
gr.Number(label="Number of API Calls", value=9, precision=0)
|
135 |
+
],
|
136 |
+
outputs=gr.Textbox(label="Results"),
|
137 |
+
title="Swarm API Call and Plotter",
|
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(share=True)
|