TuringsSolutions commited on
Commit
cdb57b4
1 Parent(s): 79acb4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -32
app.py CHANGED
@@ -53,33 +53,15 @@ class Swarm:
53
  def gather_results(self):
54
  return [agent.results for agent in self.agents if agent.results]
55
 
56
- def generate_tasks_from_model(api_url, num_tasks):
57
- tasks = []
58
- for _ in range(num_tasks):
59
- prompt = f"Generate an API call task for the following API URL: {api_url}"
60
- inputs = tokenizer(prompt, return_tensors="pt")
61
- print(f"Generating task with prompt: {prompt}")
62
- try:
63
- outputs = model.generate(**inputs, max_length=50) # Limit max length for speed
64
- task = tokenizer.decode(outputs[0], skip_special_tokens=True)
65
- print(f"Generated task: {task}")
66
- # Check if the generated task is a valid URL
67
- if task.startswith("http://") or task.startswith("https://"):
68
- tasks.append(task)
69
- else:
70
- print(f"Invalid task generated: {task}")
71
- tasks.append(f"Error: Invalid task generated: {task}")
72
- except Exception as e:
73
- print(f"Error generating task: {str(e)}")
74
- tasks.append(f"Error generating task: {str(e)}")
75
- return tasks
76
 
77
  def run_swarm(api_url, api_key, num_agents, num_tasks):
78
  try:
79
- tasks = generate_tasks_from_model(api_url, num_tasks)
80
  print(f"Generated tasks: {tasks}")
81
  except Exception as e:
82
- return f"Error generating tasks from model: {str(e)}"
83
 
84
  try:
85
  swarm = Swarm(num_agents=num_agents, fractal_pattern="Pentagonal", api_key=api_key)
@@ -99,11 +81,15 @@ def run_swarm(api_url, api_key, num_agents, num_tasks):
99
 
100
  return results
101
 
102
- def gradio_interface(api_url, api_key, num_agents, num_tasks):
103
- results = run_swarm(api_url, api_key, num_agents, num_tasks)
104
- return "\n".join(str(result) for result in results)
 
 
 
105
 
106
  # Default values for the inputs
 
107
  default_api_url = "https://meowfacts.herokuapp.com/"
108
  default_api_key = ""
109
  default_num_agents = 5
@@ -112,6 +98,7 @@ default_num_tasks = 2
112
  iface = gr.Interface(
113
  fn=gradio_interface,
114
  inputs=[
 
115
  gr.Textbox(label="API URL", placeholder="Enter the API URL", value=default_api_url),
116
  gr.Textbox(label="API Key (Optional)", placeholder="Enter the API Key", value=default_api_key),
117
  gr.Number(label="Number of Agents", value=default_num_agents, precision=0),
@@ -120,14 +107,13 @@ iface = gr.Interface(
120
  outputs=gr.Textbox(label="Results"),
121
  title="Swarm Model Processing and Result Gatherer",
122
  description="""
123
- This Gradio app demonstrates a swarm of agents using a language model to generate API call tasks and gather results.
124
- - The language model generates API calls based on the provided API URL.
125
- - The swarm is created based on a fractal geometry pattern.
126
- - Each agent makes an API call to the generated URLs and retrieves data.
127
  - The results from all agents are gathered and displayed.
128
- - Enter the API URL, API Key (optional), number of agents, and number of tasks to see the process in action.
129
- - By default, the app uses the API URL 'https://meowfacts.herokuapp.com/' with 5 agents and 2 tasks.
130
  """
131
  )
132
 
133
- iface.launch()
 
53
  def gather_results(self):
54
  return [agent.results for agent in self.agents if agent.results]
55
 
56
+ def generate_tasks(api_url, num_tasks):
57
+ return [api_url] * num_tasks
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  def run_swarm(api_url, api_key, num_agents, num_tasks):
60
  try:
61
+ tasks = generate_tasks(api_url, num_tasks)
62
  print(f"Generated tasks: {tasks}")
63
  except Exception as e:
64
+ return f"Error generating tasks: {str(e)}"
65
 
66
  try:
67
  swarm = Swarm(num_agents=num_agents, fractal_pattern="Pentagonal", api_key=api_key)
 
81
 
82
  return results
83
 
84
+ def gradio_interface(prompt, api_url, api_key, num_agents, num_tasks):
85
+ if prompt == "Autobots, Assemble!":
86
+ results = run_swarm(api_url, api_key, num_agents, num_tasks)
87
+ return "\n".join(str(result) for result in results)
88
+ else:
89
+ return "Prompt not recognized. Please use 'Autobots, Assemble!' to execute the swarm."
90
 
91
  # Default values for the inputs
92
+ default_prompt = "Autobots, Assemble!"
93
  default_api_url = "https://meowfacts.herokuapp.com/"
94
  default_api_key = ""
95
  default_num_agents = 5
 
98
  iface = gr.Interface(
99
  fn=gradio_interface,
100
  inputs=[
101
+ gr.Textbox(label="Prompt", placeholder="Enter the prompt", value=default_prompt),
102
  gr.Textbox(label="API URL", placeholder="Enter the API URL", value=default_api_url),
103
  gr.Textbox(label="API Key (Optional)", placeholder="Enter the API Key", value=default_api_key),
104
  gr.Number(label="Number of Agents", value=default_num_agents, precision=0),
 
107
  outputs=gr.Textbox(label="Results"),
108
  title="Swarm Model Processing and Result Gatherer",
109
  description="""
110
+ This Gradio app demonstrates a swarm of agents making API calls and gathering results.
111
+ - When the prompt 'Autobots, Assemble!' is entered, a swarm is created and the API calls are executed.
112
+ - Each agent makes an API call to the specified URL and retrieves data.
 
113
  - The results from all agents are gathered and displayed.
114
+ - Enter the prompt 'Autobots, Assemble!', API URL, API Key (optional), number of agents, and number of tasks to see the process in action.
115
+ - By default, the app uses the prompt 'Autobots, Assemble!' and the API URL 'https://meowfacts.herokuapp.com/' with 5 agents and 2 tasks.
116
  """
117
  )
118
 
119
+ iface.launch()