Spaces:
Sleeping
Sleeping
acecalisto3
commited on
Commit
•
13a7a5d
1
Parent(s):
092af19
Update app.py
Browse files
app.py
CHANGED
@@ -1,184 +1,54 @@
|
|
1 |
import streamlit as st
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
from
|
6 |
-
from
|
7 |
-
from
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
self.name = name
|
14 |
-
self.
|
15 |
-
self.
|
16 |
-
self.
|
17 |
-
self.llm = HuggingFaceHub(repo_id="google/flan-t5-xl", model_kwargs={"temperature": 0.5})
|
18 |
-
|
19 |
-
def act(self, task, context):
|
20 |
-
# This method should be implemented in subclasses
|
21 |
-
raise NotImplementedError
|
22 |
-
|
23 |
-
# Base Tool class
|
24 |
-
class BaseTool(Tool):
|
25 |
-
def __init__(self, name, description):
|
26 |
-
super().__init__(name=name, description=description, func=self.run)
|
27 |
-
self.llm = HuggingFaceHub(repo_id="google/flan-t5-xl", model_kwargs={"temperature": 0.5})
|
28 |
-
|
29 |
-
def run(self, arguments: str) -> str:
|
30 |
-
raise NotImplementedError
|
31 |
-
|
32 |
-
# Specific tool implementations
|
33 |
-
class CodeGenerationTool(BaseTool):
|
34 |
-
def __init__(self):
|
35 |
-
super().__init__("Code Generation", "Generates code snippets in various languages.")
|
36 |
-
self.prompt_template = PromptTemplate(
|
37 |
-
input_variables=["language", "task"],
|
38 |
-
template="Generate {language} code for: {task}"
|
39 |
-
)
|
40 |
-
self.chain = LLMChain(llm=self.llm, prompt=self.prompt_template)
|
41 |
-
|
42 |
-
def run(self, arguments: str) -> str:
|
43 |
-
language, task = arguments.split(", ", 1)
|
44 |
-
return self.chain.run(language=language, task=task)
|
45 |
-
|
46 |
-
class DataRetrievalTool(BaseTool):
|
47 |
-
def __init__(self):
|
48 |
-
super().__init__("Data Retrieval", "Accesses data from APIs, databases, or files.")
|
49 |
-
self.prompt_template = PromptTemplate(
|
50 |
-
input_variables=["source", "query"],
|
51 |
-
template="Retrieve data from {source} based on: {query}"
|
52 |
-
)
|
53 |
-
self.chain = LLMChain(llm=self.llm, prompt=self.prompt_template)
|
54 |
-
|
55 |
-
def run(self, arguments: str) -> str:
|
56 |
-
source, query = arguments.split(", ", 1)
|
57 |
-
return self.chain.run(source=source, query=query)
|
58 |
-
|
59 |
-
class TextGenerationTool(BaseTool):
|
60 |
-
def __init__(self):
|
61 |
-
super().__init__("Text Generation", "Generates human-like text based on a given prompt.")
|
62 |
-
self.prompt_template = PromptTemplate(
|
63 |
-
input_variables=["prompt"],
|
64 |
-
template="Generate text based on: {prompt}"
|
65 |
-
)
|
66 |
-
self.chain = LLMChain(llm=self.llm, prompt=self.prompt_template)
|
67 |
-
|
68 |
-
def run(self, arguments: str) -> str:
|
69 |
-
return self.chain.run(prompt=arguments)
|
70 |
-
|
71 |
-
# Specialized Agent Definitions
|
72 |
-
class SpecializedAgent(Agent):
|
73 |
-
def __init__(self, name, role, tools, knowledge_base=None):
|
74 |
-
super().__init__(name, role, tools, knowledge_base)
|
75 |
-
self.prompt = PromptTemplate.from_template(
|
76 |
-
"You are a specialized AI assistant named {name} with the role of {role}. "
|
77 |
-
"Use the following tools to complete your task: {tools}\n\n"
|
78 |
-
"Task: {input}\n"
|
79 |
-
"Thought: Let's approach this step-by-step:\n"
|
80 |
-
"{agent_scratchpad}"
|
81 |
-
)
|
82 |
-
self.react_agent = create_react_agent(self.llm, self.tools, self.prompt)
|
83 |
-
self.agent_executor = AgentExecutor(
|
84 |
-
agent=self.react_agent,
|
85 |
-
tools=self.tools,
|
86 |
-
verbose=True,
|
87 |
-
max_iterations=5
|
88 |
-
)
|
89 |
-
|
90 |
-
def act(self, task, context):
|
91 |
-
return self.agent_executor.run(input=task)
|
92 |
-
|
93 |
-
class RequirementsAgent(SpecializedAgent):
|
94 |
-
def __init__(self):
|
95 |
-
super().__init__("RequirementsAnalyst", "Analyzing and refining project requirements", [TextGenerationTool()])
|
96 |
-
|
97 |
-
class ArchitectureAgent(SpecializedAgent):
|
98 |
-
def __init__(self):
|
99 |
-
super().__init__("SystemArchitect", "Designing system architecture", [TextGenerationTool()])
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
super().__init__("FrontendDeveloper", "Developing the frontend", [CodeGenerationTool()])
|
104 |
-
|
105 |
-
class BackendAgent(SpecializedAgent):
|
106 |
-
def __init__(self):
|
107 |
-
super().__init__("BackendDeveloper", "Developing the backend", [CodeGenerationTool(), DataRetrievalTool()])
|
108 |
-
|
109 |
-
class DatabaseAgent(SpecializedAgent):
|
110 |
-
def __init__(self):
|
111 |
-
super().__init__("DatabaseEngineer", "Designing and implementing the database", [CodeGenerationTool(), DataRetrievalTool()])
|
112 |
-
|
113 |
-
class TestingAgent(SpecializedAgent):
|
114 |
-
def __init__(self):
|
115 |
-
super().__init__("QAEngineer", "Creating and executing test plans", [CodeGenerationTool(), TextGenerationTool()])
|
116 |
-
|
117 |
-
class DeploymentAgent(SpecializedAgent):
|
118 |
-
def __init__(self):
|
119 |
-
super().__init__("DevOpsEngineer", "Handling deployment and infrastructure", [CodeGenerationTool(), TextGenerationTool()])
|
120 |
-
|
121 |
-
# --- Application Building Sequence ---
|
122 |
-
class ApplicationBuilder:
|
123 |
-
def __init__(self):
|
124 |
-
self.agents = [
|
125 |
-
RequirementsAgent(),
|
126 |
-
ArchitectureAgent(),
|
127 |
-
FrontendAgent(),
|
128 |
-
BackendAgent(),
|
129 |
-
DatabaseAgent(),
|
130 |
-
TestingAgent(),
|
131 |
-
DeploymentAgent()
|
132 |
-
]
|
133 |
-
self.project_state = {}
|
134 |
-
|
135 |
-
def build_application(self, project_description):
|
136 |
-
st.write("Starting application building process...")
|
137 |
for agent in self.agents:
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
return task
|
160 |
-
|
161 |
-
return f"Contribute to the project: {project_description}"
|
162 |
-
|
163 |
-
# --- Streamlit App ---
|
164 |
-
st.title("CODEFUSSION ☄ - Full-Stack Application Builder")
|
165 |
-
|
166 |
-
project_description = st.text_area("Enter your project description:")
|
167 |
-
|
168 |
-
if st.button("Build Application"):
|
169 |
-
if project_description:
|
170 |
-
app_builder = ApplicationBuilder()
|
171 |
-
app_builder.build_application(project_description)
|
172 |
-
else:
|
173 |
-
st.write("Please enter a project description.")
|
174 |
-
|
175 |
-
# Display information about the agents
|
176 |
-
st.sidebar.title("Agent Information")
|
177 |
-
app_builder = ApplicationBuilder()
|
178 |
-
for agent in app_builder.agents:
|
179 |
-
st.sidebar.write(f"--- {agent.name} ---")
|
180 |
-
st.sidebar.write(f"Role: {agent.role}")
|
181 |
-
st.sidebar.write("Tools:")
|
182 |
-
for tool in agent.tools:
|
183 |
-
st.sidebar.write(f"- {tool.name}")
|
184 |
-
st.sidebar.write("")
|
|
|
1 |
import streamlit as st
|
2 |
+
from huggingface_hub import HfApi
|
3 |
+
import os
|
4 |
+
import gradio as gr
|
5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
6 |
+
from typing import List, Dict
|
7 |
+
from supplemental import Agent, Tool, CodeGenerationTool, DataRetrievalTool, TextGenerationTool, CodeExecutionTool, CodeDebuggingTool, CodeSummarizationTool, CodeTranslationTool, CodeOptimizationTool, CodeDocumentationTool, ImageGenerationTool, ImageEditingTool, ImageAnalysisTool, Workflow, EnhancedAIAgent
|
8 |
+
|
9 |
+
st.title("CODEFUSSION ☄")
|
10 |
+
|
11 |
+
# Access Hugging Face API key from secrets
|
12 |
+
hf_token = st.secrets["hf_token"]
|
13 |
+
if not hf_token:
|
14 |
+
st.error("Hugging Face API key not found. Please make sure it is set in the secrets.")
|
15 |
+
|
16 |
+
# --- Agent Pool ---
|
17 |
+
agent_pool = {
|
18 |
+
"IdeaIntake": EnhancedAIAgent("IdeaIntake", "Idea Intake", ["Data Retrieval", "Code Generation", "Text Generation"], "bigcode/starcoder"),
|
19 |
+
"CodeBuilder": EnhancedAIAgent("CodeBuilder", "Code Builder", ["Code Generation", "Code Debugging", "Code Optimization"], "bigcode/starcoder"),
|
20 |
+
"ImageCreator": EnhancedAIAgent("ImageCreator", "Image Creator", ["Image Generation", "Image Editing"], "bigcode/starcoder"),
|
21 |
+
}
|
22 |
+
|
23 |
+
# --- Workflow Definitions ---
|
24 |
+
class Workflow:
|
25 |
+
def __init__(self, name, agents, task, description):
|
26 |
self.name = name
|
27 |
+
self.agents = agents
|
28 |
+
self.task = task
|
29 |
+
self.description = description
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
def run(self, prompt, context):
|
32 |
+
# Workflow execution logic
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
for agent in self.agents:
|
34 |
+
action = agent.act(prompt, context)
|
35 |
+
# Execute the tool
|
36 |
+
if action.get("tool"):
|
37 |
+
tool = next((t for t in agent.tools if t.name == action["tool"]), None)
|
38 |
+
if tool:
|
39 |
+
output = tool.run(action["arguments"])
|
40 |
+
# Update context
|
41 |
+
context.update(output)
|
42 |
+
|
43 |
+
# Example usage
|
44 |
+
workflow = Workflow(
|
45 |
+
name="Example Workflow",
|
46 |
+
agents=[agent_pool["IdeaIntake"], agent_pool["CodeBuilder"]],
|
47 |
+
task="Generate and debug code",
|
48 |
+
description="A workflow to generate and debug code using AI agents."
|
49 |
+
)
|
50 |
+
|
51 |
+
context = {}
|
52 |
+
prompt = "Create a Python function to add two numbers."
|
53 |
+
workflow.run(prompt, context)
|
54 |
+
st.write(context)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|