File size: 3,301 Bytes
bca41ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bf07eb
 
bca41ff
 
0bf07eb
 
 
 
 
 
 
 
 
bca41ff
 
 
 
 
 
 
 
 
0bf07eb
 
bca41ff
0bf07eb
bca41ff
0bf07eb
bca41ff
 
0bf07eb
bca41ff
 
0bf07eb
 
 
 
 
 
 
 
bca41ff
 
0bf07eb
 
 
 
 
bca41ff
 
 
 
 
 
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
import gradio as gr
import json
from swarm_editor import SwarmEditor
from firecrawl import FirecrawlApp
from dotenv import load_dotenv
import os

load_dotenv()

CONFIG_FILE = 'swarm_config.json'

class UserInterface:
    def __init__(self):
        self.swarm_editor = SwarmEditor()
        self.firecrawl_app = FirecrawlApp(api_key=os.getenv("FIRECRAWL_API_KEY"))
        self.load_config()

    def load_config(self):
        with open(CONFIG_FILE, 'r') as f:
            self.config = json.load(f)
        self.swarm_editor.load_configuration(CONFIG_FILE)
        return self.config

    def scrape_website(self, url):
        scrape_status = self.firecrawl_app.scrape_url(
            url,
            params={'formats': ['markdown']}
        )
        return scrape_status.get('markdown', 'No content scraped')

    def update_agent(self, index, name, prompt):
        self.swarm_editor.update_agent(index, name, prompt)
        return f"Agent {index} updated"

    def run_workflow(self, scraped_content, progress=gr.Progress()):
        outputs = [""] * len(self.swarm_editor.agents)
        for i, agent in enumerate(self.swarm_editor.agents):
            progress(i / len(self.swarm_editor.agents), f"Running Agent {i}: {agent.name}")
            response = self.swarm_editor.run_single_agent(agent, scraped_content)
            outputs[i] = response.messages[-1]['content']
            scraped_content = outputs[i]
            yield outputs
        progress(1.0, "Workflow complete")

    def launch(self):
        with gr.Blocks() as interface:
            gr.Markdown("# Agent Workflow Editor")
            
            url = gr.Textbox(label="URL to Scrape", value="https://www.lazzloe.com/")
            scrape_button = gr.Button("Scrape Website")
            scraped_content = gr.Textbox(label="Scraped Content")
            
            agent_config_tabs = gr.Tabs()
            with agent_config_tabs:
                for i, agent in enumerate(self.config['agents']):
                    with gr.Tab(f"Agent {i} Config"):
                        name = gr.Textbox(label="Name", value=agent['name'])
                        prompt = gr.Textbox(label="Prompt", value=agent['instructions'], lines=3)
                        update_button = gr.Button(f"Update Agent {i}")
                        update_output = gr.Textbox(label="Update Status")
                        update_button.click(self.update_agent, inputs=[gr.Number(value=i, visible=False), name, prompt], outputs=[update_output])
            
            run_button = gr.Button("Run Workflow")
            
            output_tabs = gr.Tabs()
            output_components = []
            with output_tabs:
                for i, agent in enumerate(self.config['agents']):
                    with gr.Tab(f"Agent {i} Output"):
                        output = gr.Textbox(label=f"{agent['name']} Output", lines=10)
                        output_components.append(output)

            scrape_button.click(self.scrape_website, inputs=[url], outputs=[scraped_content])
            run_button.click(
                self.run_workflow,
                inputs=[scraped_content],
                outputs=output_components
            )

        interface.launch()

if __name__ == "__main__":
    ui = UserInterface()
    ui.launch()