Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,10 @@
|
|
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 |
|
@@ -52,38 +53,30 @@ class Swarm:
|
|
52 |
def gather_results(self):
|
53 |
return [agent.results for agent in self.agents if agent.results]
|
54 |
|
55 |
-
def generate_tasks_from_model(
|
56 |
-
|
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(
|
63 |
-
|
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(
|
82 |
-
results = run_swarm(
|
83 |
return "\n".join(str(result) for result in results)
|
84 |
|
85 |
# Default values for the inputs
|
86 |
-
|
87 |
default_api_key = ""
|
88 |
default_num_agents = 5
|
89 |
default_num_tasks = 2
|
@@ -91,7 +84,7 @@ default_num_tasks = 2
|
|
91 |
iface = gr.Interface(
|
92 |
fn=gradio_interface,
|
93 |
inputs=[
|
94 |
-
gr.Textbox(label="
|
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)
|
@@ -99,13 +92,13 @@ iface = gr.Interface(
|
|
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
|
103 |
-
- The language model generates API calls based on the provided
|
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
|
108 |
-
- By default, the app uses the
|
109 |
"""
|
110 |
)
|
111 |
|
|
|
1 |
import os
|
2 |
import requests
|
3 |
+
import numpy as np
|
4 |
import gradio as gr
|
5 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
6 |
|
7 |
+
# Load the Qwen model and tokenizer
|
8 |
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-0.5B")
|
9 |
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2-0.5B")
|
10 |
|
|
|
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 |
+
prompt = f"Generate {num_tasks} API call tasks for the following API URL: {api_url}"
|
58 |
inputs = tokenizer(prompt, return_tensors="pt")
|
59 |
outputs = model.generate(**inputs, max_length=100, num_return_sequences=num_tasks)
|
60 |
tasks = [tokenizer.decode(output, skip_special_tokens=True) for output in outputs]
|
61 |
return tasks
|
62 |
|
63 |
+
def run_swarm(api_url, api_key, num_agents, num_tasks):
|
64 |
+
tasks = generate_tasks_from_model(api_url, num_tasks)
|
|
|
|
|
|
|
65 |
swarm = Swarm(num_agents=num_agents, fractal_pattern="Pentagonal", api_key=api_key)
|
66 |
swarm.assign_tasks(tasks)
|
67 |
swarm.execute()
|
|
|
|
|
68 |
results = swarm.gather_results()
|
|
|
|
|
69 |
print("\nAll results retrieved by the swarm:")
|
70 |
for i, result in enumerate(results):
|
71 |
print(f"Result {i + 1}: {result}")
|
|
|
72 |
return results
|
73 |
|
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_api_key = ""
|
81 |
default_num_agents = 5
|
82 |
default_num_tasks = 2
|
|
|
84 |
iface = gr.Interface(
|
85 |
fn=gradio_interface,
|
86 |
inputs=[
|
87 |
+
gr.Textbox(label="API URL", placeholder="Enter the API URL", value=default_api_url),
|
88 |
gr.Textbox(label="API Key (Optional)", placeholder="Enter the API Key", value=default_api_key),
|
89 |
gr.Number(label="Number of Agents", value=default_num_agents, precision=0),
|
90 |
gr.Number(label="Number of Tasks", value=default_num_tasks, precision=0)
|
|
|
92 |
outputs=gr.Textbox(label="Results"),
|
93 |
title="Swarm Model Processing and Result Gatherer",
|
94 |
description="""
|
95 |
+
This Gradio app demonstrates a swarm of agents using a language model to generate API call tasks and gather results.
|
96 |
+
- The language model generates API calls based on the provided API URL.
|
97 |
- The swarm is created based on a fractal geometry pattern.
|
98 |
- Each agent makes an API call to the generated URLs and retrieves data.
|
99 |
- The results from all agents are gathered and displayed.
|
100 |
+
- Enter the API URL, API Key (optional), number of agents, and number of tasks to see the process in action.
|
101 |
+
- By default, the app uses the API URL 'https://meowfacts.herokuapp.com/' with 5 agents and 2 tasks.
|
102 |
"""
|
103 |
)
|
104 |
|