TuringsSolutions commited on
Commit
a95de33
·
verified ·
1 Parent(s): 5de9b7a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -0
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import gradio as gr
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM
5
+
6
+ # Load model and tokenizer
7
+ tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-0.5B")
8
+ model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2-0.5B")
9
+
10
+ class Agent:
11
+ def __init__(self, id, api_key=None):
12
+ self.id = id
13
+ self.task = None
14
+ self.results = None
15
+ self.api_key = api_key
16
+
17
+ def execute_task(self):
18
+ if self.task:
19
+ print(f"Agent {self.id} is making an API call to '{self.task}'")
20
+ headers = {"Authorization": f"Bearer {self.api_key}"} if self.api_key else {}
21
+ try:
22
+ response = requests.get(self.task, headers=headers)
23
+ if response.status_code == 200:
24
+ self.results = response.json()
25
+ else:
26
+ self.results = "Error: Unable to fetch data"
27
+ print(f"Agent {self.id} received: {self.results}")
28
+ except Exception as e:
29
+ self.results = f"Error: {str(e)}"
30
+ print(f"Agent {self.id} encountered an error: {str(e)}")
31
+
32
+ def communicate(self, other_agents):
33
+ pass
34
+
35
+ class Swarm:
36
+ def __init__(self, num_agents, fractal_pattern, api_key=None):
37
+ self.agents = [Agent(i, api_key) for i in range(num_agents)]
38
+ self.fractal_pattern = fractal_pattern
39
+ print(f"Swarm created with {num_agents} agents using the {fractal_pattern} pattern.")
40
+
41
+ def assign_tasks(self, tasks):
42
+ for i, task in enumerate(tasks):
43
+ self.agents[i % len(self.agents)].task = task
44
+ print(f"Task assigned to Agent {self.agents[i % len(self.agents)].id}: {task}")
45
+
46
+ def execute(self):
47
+ for agent in self.agents:
48
+ agent.execute_task()
49
+ for agent in self.agents:
50
+ agent.communicate(self.agents)
51
+
52
+ def gather_results(self):
53
+ return [agent.results for agent in self.agents if agent.results]
54
+
55
+ def generate_tasks_from_model(prompt, num_tasks):
56
+ # Generate tasks using the Qwen model
57
+ inputs = tokenizer(prompt, return_tensors="pt")
58
+ outputs = model.generate(**inputs, max_length=100, num_return_sequences=num_tasks)
59
+ tasks = [tokenizer.decode(output, skip_special_tokens=True) for output in outputs]
60
+ return tasks
61
+
62
+ def run_swarm(prompt, api_key, num_agents, num_tasks):
63
+ # Generate tasks using the model
64
+ tasks = generate_tasks_from_model(prompt, num_tasks)
65
+
66
+ # Create a swarm with a fractal pattern (Pentagonal spread)
67
+ swarm = Swarm(num_agents=num_agents, fractal_pattern="Pentagonal", api_key=api_key)
68
+ swarm.assign_tasks(tasks)
69
+ swarm.execute()
70
+
71
+ # Gather results
72
+ results = swarm.gather_results()
73
+
74
+ # Print all results
75
+ print("\nAll results retrieved by the swarm:")
76
+ for i, result in enumerate(results):
77
+ print(f"Result {i + 1}: {result}")
78
+
79
+ return results
80
+
81
+ def gradio_interface(prompt, api_key, num_agents, num_tasks):
82
+ results = run_swarm(prompt, api_key, num_agents, num_tasks)
83
+ return "\n".join(str(result) for result in results)
84
+
85
+ # Default values for the inputs
86
+ default_prompt = "Generate API calls to fetch random cat facts."
87
+ default_api_key = ""
88
+ default_num_agents = 5
89
+ default_num_tasks = 2
90
+
91
+ iface = gr.Interface(
92
+ fn=gradio_interface,
93
+ inputs=[
94
+ gr.Textbox(label="Prompt", placeholder="Enter the prompt", value=default_prompt),
95
+ gr.Textbox(label="API Key (Optional)", placeholder="Enter the API Key", value=default_api_key),
96
+ gr.Number(label="Number of Agents", value=default_num_agents, precision=0),
97
+ gr.Number(label="Number of Tasks", value=default_num_tasks, precision=0)
98
+ ],
99
+ outputs=gr.Textbox(label="Results"),
100
+ title="Swarm Model Processing and Result Gatherer",
101
+ description="""
102
+ This Gradio app demonstrates a swarm of agents using a language model to generate API calls and gather results.
103
+ - The language model generates API calls based on the provided prompt.
104
+ - The swarm is created based on a fractal geometry pattern.
105
+ - Each agent makes an API call to the generated URLs and retrieves data.
106
+ - The results from all agents are gathered and displayed.
107
+ - Enter the prompt, API Key (optional), number of agents, and number of tasks to see the process in action.
108
+ - By default, the app uses the prompt 'Generate API calls to fetch random cat facts' with 5 agents and 2 tasks.
109
+ """
110
+ )
111
+
112
+ iface.launch()