File size: 4,800 Bytes
a95de33
 
0348881
a95de33
 
 
94f7c96
 
 
a95de33
 
 
 
 
 
 
 
 
 
 
 
 
63a5233
a95de33
 
 
2d6ad29
a95de33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdb57b4
 
a95de33
0348881
2d6ad29
cdb57b4
a961001
2d6ad29
cdb57b4
2d6ad29
 
 
 
 
 
 
 
 
 
 
 
 
a95de33
 
 
2d6ad29
a95de33
 
cdb57b4
 
 
 
 
 
a95de33
 
cdb57b4
0348881
a95de33
 
 
 
 
 
 
cdb57b4
0348881
a95de33
 
 
 
 
 
 
cdb57b4
 
 
a95de33
cdb57b4
 
a95de33
 
 
cdb57b4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os
import requests
import numpy as np
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM

# Load the TinyLlama model and tokenizer
tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
model = AutoModelForCausalLM.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")

class Agent:
    def __init__(self, id, api_key=None):
        self.id = id
        self.task = None
        self.results = None
        self.api_key = api_key

    def execute_task(self):
        if self.task:
            print(f"Agent {self.id} is making an API call to '{self.task}'")
            headers = {"Authorization": f"Bearer {self.api_key}"} if self.api_key else {}
            try:
                response = requests.get(self.task, headers=headers, timeout=10)  # Add timeout
                if response.status_code == 200:
                    self.results = response.json()
                else:
                    self.results = f"Error: Unable to fetch data, status code {response.status_code}"
                print(f"Agent {self.id} received: {self.results}")
            except Exception as e:
                self.results = f"Error: {str(e)}"
                print(f"Agent {self.id} encountered an error: {str(e)}")

    def communicate(self, other_agents):
        pass

class Swarm:
    def __init__(self, num_agents, fractal_pattern, api_key=None):
        self.agents = [Agent(i, api_key) for i in range(num_agents)]
        self.fractal_pattern = fractal_pattern
        print(f"Swarm created with {num_agents} agents using the {fractal_pattern} pattern.")

    def assign_tasks(self, tasks):
        for i, task in enumerate(tasks):
            self.agents[i % len(self.agents)].task = task
            print(f"Task assigned to Agent {self.agents[i % len(self.agents)].id}: {task}")

    def execute(self):
        for agent in self.agents:
            agent.execute_task()
        for agent in self.agents:
            agent.communicate(self.agents)

    def gather_results(self):
        return [agent.results for agent in self.agents if agent.results]

def generate_tasks(api_url, num_tasks):
    return [api_url] * num_tasks

def run_swarm(api_url, api_key, num_agents, num_tasks):
    try:
        tasks = generate_tasks(api_url, num_tasks)
        print(f"Generated tasks: {tasks}")
    except Exception as e:
        return f"Error generating tasks: {str(e)}"

    try:
        swarm = Swarm(num_agents=num_agents, fractal_pattern="Pentagonal", api_key=api_key)
        swarm.assign_tasks(tasks)
        swarm.execute()
    except Exception as e:
        return f"Error executing swarm tasks: {str(e)}"

    try:
        results = swarm.gather_results()
    except Exception as e:
        return f"Error gathering results: {str(e)}"

    print("\nAll results retrieved by the swarm:")
    for i, result in enumerate(results):
        print(f"Result {i + 1}: {result}")

    return results

def gradio_interface(prompt, api_url, api_key, num_agents, num_tasks):
    if prompt == "Autobots, Assemble!":
        results = run_swarm(api_url, api_key, num_agents, num_tasks)
        return "\n".join(str(result) for result in results)
    else:
        return "Prompt not recognized. Please use 'Autobots, Assemble!' to execute the swarm."

# Default values for the inputs
default_prompt = "Autobots, Assemble!"
default_api_url = "https://meowfacts.herokuapp.com/"
default_api_key = ""
default_num_agents = 5
default_num_tasks = 2

iface = gr.Interface(
    fn=gradio_interface,
    inputs=[
        gr.Textbox(label="Prompt", placeholder="Enter the prompt", value=default_prompt),
        gr.Textbox(label="API URL", placeholder="Enter the API URL", value=default_api_url),
        gr.Textbox(label="API Key (Optional)", placeholder="Enter the API Key", value=default_api_key),
        gr.Number(label="Number of Agents", value=default_num_agents, precision=0),
        gr.Number(label="Number of Tasks", value=default_num_tasks, precision=0)
    ],
    outputs=gr.Textbox(label="Results"),
    title="Swarm Model Processing and Result Gatherer",
    description="""
        This Gradio app demonstrates a swarm of agents making API calls and gathering results.
        - When the prompt 'Autobots, Assemble!' is entered, a swarm is created and the API calls are executed.
        - Each agent makes an API call to the specified URL and retrieves data.
        - The results from all agents are gathered and displayed.
        - Enter the prompt 'Autobots, Assemble!', API URL, API Key (optional), number of agents, and number of tasks to see the process in action.
        - By default, the app uses the prompt 'Autobots, Assemble!' and the API URL 'https://meowfacts.herokuapp.com/' with 5 agents and 2 tasks.
    """
)

iface.launch()