TuringsSolutions commited on
Commit
5d7ede0
·
verified ·
1 Parent(s): a02dc5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -50
app.py CHANGED
@@ -1,9 +1,8 @@
1
  import os
2
  import requests
3
- import matplotlib.pyplot as plt
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
@@ -49,81 +48,56 @@ class Swarm:
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,
70
- (center + pentagon_points[0]) / 2,
71
- (center + pentagon_points[1]) / 2,
72
- (center + pentagon_points[2]) / 2,
73
- (center + pentagon_points[3]) / 2,
74
- (center + pentagon_points[4]) / 2,
75
- (pentagon_points[0] + pentagon_points[1]) / 2,
76
- (pentagon_points[1] + pentagon_points[2]) / 2,
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
-
87
- for mx in mirrored_x:
88
- for my in mirrored_y:
89
- if (mx, my) != (point[0], point[1]):
90
- ax.plot(mx, my, 'ro')
91
- ax.text(mx, my, results[i], fontsize=9, ha='right')
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,
118
  inputs=[
119
- gr.Textbox(label="API URL", placeholder="Enter the API URL"),
120
  gr.Textbox(label="API Key (Optional)", placeholder="Enter the API Key"),
121
- gr.Number(label="Number of Agents", value=9, precision=0),
122
- gr.Number(label="Number of API Calls", value=9, precision=0)
123
  ],
124
  outputs=gr.Textbox(label="Results"),
125
- title="Swarm API Call and Plotter",
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()
 
1
  import os
2
  import requests
 
 
3
  import gradio as gr
4
 
5
+ # Define Agent and Swarm classes based on fractal geometry
6
  class Agent:
7
  def __init__(self, id, api_key=None):
8
  self.id = id
 
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 run the swarm and gather results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  def run_swarm(api_url, api_key, num_agents, num_tasks):
57
+ # Create a swarm with a fractal pattern (Pentagonal spread)
58
  swarm = Swarm(num_agents=num_agents, fractal_pattern="Pentagonal", api_key=api_key)
59
  tasks = generate_tasks(api_url, num_tasks)
60
  swarm.assign_tasks(tasks)
61
  swarm.execute()
62
 
63
+ # Gather results
64
  results = swarm.gather_results()
65
 
66
+ # Print all results
67
  print("\nAll results retrieved by the swarm:")
68
  for i, result in enumerate(results):
69
  print(f"Result {i + 1}: {result}")
70
 
 
 
 
71
  return results
72
 
73
+ # Gradio interface
74
  def gradio_interface(api_url, api_key, num_agents, num_tasks):
75
  results = run_swarm(api_url, api_key, num_agents, num_tasks)
76
  return "\n".join(str(result) for result in results)
77
 
78
+ # Default values for the inputs
79
+ default_api_url = "https://meowfacts.herokuapp.com/"
80
+ default_num_agents = 5
81
+ default_num_tasks = 2
82
+
83
  iface = gr.Interface(
84
  fn=gradio_interface,
85
  inputs=[
86
+ gr.Textbox(label="API URL", placeholder="Enter the API URL", value=default_api_url),
87
  gr.Textbox(label="API Key (Optional)", placeholder="Enter the API Key"),
88
+ gr.Number(label="Number of Agents", value=default_num_agents, precision=0),
89
+ gr.Number(label="Number of API Calls", value=default_num_tasks, precision=0)
90
  ],
91
  outputs=gr.Textbox(label="Results"),
92
+ title="Swarm API Call and Result Gatherer",
93
+ description="""
94
+ This Gradio app demonstrates a swarm of agents making API calls and gathering results.
95
+ - The swarm is created based on a fractal geometry pattern.
96
+ - Each agent makes an API call to the specified URL and retrieves data.
97
+ - The results from all agents are gathered and displayed.
98
+ - Enter the API URL, API Key (optional), number of agents, and number of API calls to see the process in action.
99
+ - By default, the app uses the 'Meow Facts' API with 5 agents and 2 API calls.
100
+ """
101
  )
102
 
103
  iface.launch()