Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,14 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
-
import
|
| 4 |
-
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer, AutoModel, RagRetriever, AutoModelForSeq2SeqLM
|
| 5 |
-
import black
|
| 6 |
-
from pylint import lint
|
| 7 |
-
from io import StringIO
|
| 8 |
-
import sys
|
| 9 |
-
import torch
|
| 10 |
-
from huggingface_hub import hf_hub_url, cached_download, HfApi
|
| 11 |
-
from datetime import datetime
|
| 12 |
|
| 13 |
# Set your Hugging Face API key here
|
| 14 |
hf_token = "YOUR_HUGGING_FACE_API_KEY" # Replace with your actual token
|
| 15 |
|
| 16 |
-
HUGGING_FACE_REPO_URL = "https://huggingface.co/spaces/acecalisto3/DevToolKit"
|
| 17 |
PROJECT_ROOT = "projects"
|
| 18 |
AGENT_DIRECTORY = "agents"
|
|
|
|
| 19 |
|
| 20 |
# Global state to manage communication between Tool Box and Workspace Chat App
|
| 21 |
if 'chat_history' not in st.session_state:
|
|
@@ -26,93 +19,51 @@ if 'workspace_projects' not in st.session_state:
|
|
| 26 |
st.session_state.workspace_projects = {}
|
| 27 |
if 'available_agents' not in st.session_state:
|
| 28 |
st.session_state.available_agents = []
|
| 29 |
-
if 'current_state' not in st.session_state:
|
| 30 |
-
st.session_state.current_state = {
|
| 31 |
-
'toolbox': {},
|
| 32 |
-
'workspace_chat': {}
|
| 33 |
-
}
|
| 34 |
-
|
| 35 |
-
# List of top downloaded free code-generative models from Hugging Face Hub
|
| 36 |
-
AVAILABLE_CODE_GENERATIVE_MODELS = [
|
| 37 |
-
"bigcode/starcoder", # Popular and powerful
|
| 38 |
-
"Salesforce/codegen-350M-mono", # Smaller, good for quick tasks
|
| 39 |
-
"microsoft/CodeGPT-small", # Smaller, good for quick tasks
|
| 40 |
-
"google/flan-t5-xl", # Powerful, good for complex tasks
|
| 41 |
-
"facebook/bart-large-cnn", # Good for text-to-code tasks
|
| 42 |
-
]
|
| 43 |
-
|
| 44 |
-
# Load pre-trained RAG retriever
|
| 45 |
-
rag_retriever = RagRetriever.from_pretrained("neural-bridge/rag-dataset-1200") # Use a Hugging Face RAG model
|
| 46 |
-
|
| 47 |
-
# Load pre-trained chat model
|
| 48 |
-
chat_model = AutoModelForSeq2SeqLM.from_pretrained("microsoft/DialoGPT-medium") # Use a Hugging Face chat model
|
| 49 |
-
|
| 50 |
-
# Load tokenizer
|
| 51 |
-
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
| 52 |
|
| 53 |
class AIAgent:
|
| 54 |
-
def __init__(self, name, description, skills
|
| 55 |
self.name = name
|
| 56 |
self.description = description
|
| 57 |
self.skills = skills
|
| 58 |
-
self._hf_api = hf_api
|
| 59 |
-
self._hf_token = hf_token # Store the token here
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
def
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 73 |
|
| 74 |
def deploy_built_space_to_hf(self):
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
model_name=repository_name,
|
| 90 |
-
repo_id=repository_name,
|
| 91 |
-
model_card={},
|
| 92 |
-
library_card={}
|
| 93 |
-
)
|
| 94 |
-
print("Space published:", publishing_response)
|
| 95 |
-
|
| 96 |
-
def process_input(user_input):
|
| 97 |
-
# Input pipeline: Tokenize and preprocess user input
|
| 98 |
-
input_ids = tokenizer(user_input, return_tensors="pt").input_ids
|
| 99 |
-
attention_mask = tokenizer(user_input, return_tensors="pt").attention_mask
|
| 100 |
-
|
| 101 |
-
# RAG model: Generate response
|
| 102 |
-
with torch.no_grad():
|
| 103 |
-
output = rag_retriever(input_ids, attention_mask=attention_mask)
|
| 104 |
-
response = output.generator_outputs[0].sequences[0]
|
| 105 |
-
|
| 106 |
-
# Chat model: Refine response
|
| 107 |
-
chat_input = tokenizer(response, return_tensors="pt")
|
| 108 |
-
chat_input["input_ids"] = chat_input["input_ids"].unsqueeze(0)
|
| 109 |
-
chat_input["attention_mask"] = chat_input["attention_mask"].unsqueeze(0)
|
| 110 |
-
with torch.no_grad():
|
| 111 |
-
chat_output = chat_model(**chat_input)
|
| 112 |
-
refined_response = chat_output.sequences[0]
|
| 113 |
-
|
| 114 |
-
# Output pipeline: Return final response
|
| 115 |
-
return refined_response
|
| 116 |
|
| 117 |
def workspace_interface(project_name):
|
| 118 |
project_path = os.path.join(PROJECT_ROOT, project_name)
|
|
@@ -134,130 +85,15 @@ def add_code_to_workspace(project_name, code, file_name):
|
|
| 134 |
st.session_state.workspace_projects[project_name]['files'].append(file_name)
|
| 135 |
return f"Code added to '{file_name}' in project '{project_name}'."
|
| 136 |
|
| 137 |
-
def
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
chat_history = ""
|
| 147 |
-
for user_input, response in history:
|
| 148 |
-
chat_history += f"User: {user_input}\nAgent: {response}\n\n"
|
| 149 |
-
return chat_history
|
| 150 |
-
|
| 151 |
-
def display_workspace_projects(projects):
|
| 152 |
-
workspace_projects = ""
|
| 153 |
-
for project, details in projects.items():
|
| 154 |
-
workspace_projects += f"Project: {project}\nFiles:\n"
|
| 155 |
-
for file in details['files']:
|
| 156 |
-
workspace_projects += f" - {file}\n"
|
| 157 |
-
return workspace_projects
|
| 158 |
-
|
| 159 |
-
# Streamlit App
|
| 160 |
-
st.title("AI Agent Creator")
|
| 161 |
-
|
| 162 |
-
# Sidebar navigation
|
| 163 |
-
st.sidebar.title("Navigation")
|
| 164 |
-
app_mode = st.sidebar.selectbox("Choose the app mode", ["AI Agent Creator", "Tool Box", "Workspace Chat App"])
|
| 165 |
-
|
| 166 |
-
if app_mode == "AI Agent Creator":
|
| 167 |
-
# AI Agent Creator
|
| 168 |
-
st.header("Create an AI Agent from Text")
|
| 169 |
-
|
| 170 |
-
st.subheader("From Text")
|
| 171 |
-
agent_name = st.text_input("Enter agent name:")
|
| 172 |
-
text_input = st.text_area("Enter skills (one per line):")
|
| 173 |
-
if st.button("Create Agent"):
|
| 174 |
-
skills = text_input.split('\n')
|
| 175 |
-
agent = AIAgent(agent_name, "AI agent created from text input", skills)
|
| 176 |
-
st.success(f"Agent '{agent_name}' created and saved successfully.")
|
| 177 |
-
st.session_state.available_agents.append(agent_name)
|
| 178 |
-
|
| 179 |
-
elif app_mode == "Tool Box":
|
| 180 |
-
# Tool Box
|
| 181 |
-
st.header("AI-Powered Tools")
|
| 182 |
-
|
| 183 |
-
# Chat Interface
|
| 184 |
-
st.subheader("Chat with CodeCraft")
|
| 185 |
-
chat_input = st.text_area("Enter your message:")
|
| 186 |
-
if st.button("Send"):
|
| 187 |
-
response = process_input(chat_input)
|
| 188 |
-
st.session_state.chat_history.append((chat_input, response))
|
| 189 |
-
st.write(f"CodeCraft: {response}")
|
| 190 |
-
|
| 191 |
-
# Terminal Interface
|
| 192 |
-
st.subheader("Terminal")
|
| 193 |
-
terminal_input = st.text_input("Enter a command:")
|
| 194 |
-
if st.button("Run"):
|
| 195 |
-
output = run_code(terminal_input)
|
| 196 |
-
st.session_state.terminal_history.append((terminal_input, output))
|
| 197 |
-
st.code(output, language="bash")
|
| 198 |
-
|
| 199 |
-
# Project Management
|
| 200 |
-
st.subheader("Project Management")
|
| 201 |
-
project_name_input = st.text_input("Enter Project Name:")
|
| 202 |
-
if st.button("Create Project"):
|
| 203 |
-
status = workspace_interface(project_name_input)
|
| 204 |
-
st.write(status)
|
| 205 |
-
|
| 206 |
-
code_to_add = st.text_area("Enter Code to Add to Workspace:", height=150)
|
| 207 |
-
file_name_input = st.text_input("Enter File Name (e.g., 'app.py'):")
|
| 208 |
-
if st.button("Add Code"):
|
| 209 |
-
status = add_code_to_workspace(project_name_input, code_to_add, file_name_input)
|
| 210 |
-
st.write(status)
|
| 211 |
-
|
| 212 |
-
# Display Chat History
|
| 213 |
-
st.subheader("Chat History")
|
| 214 |
-
chat_history = display_chat_history(st.session_state.chat_history)
|
| 215 |
-
st.text_area("Chat History", value=chat_history, height=200)
|
| 216 |
-
|
| 217 |
-
# Display Workspace Projects
|
| 218 |
-
st.subheader("Workspace Projects")
|
| 219 |
-
workspace_projects = display_workspace_projects(st.session_state.workspace_projects)
|
| 220 |
-
st.text_area("Workspace Projects", value=workspace_projects, height=200)
|
| 221 |
-
|
| 222 |
-
elif app_mode == "Workspace Chat App":
|
| 223 |
-
# Workspace Chat App
|
| 224 |
-
st.header("Workspace Chat App")
|
| 225 |
-
|
| 226 |
-
# Chat Interface with AI Agents
|
| 227 |
-
st.subheader("Chat with AI Agents")
|
| 228 |
-
selected_agent = st.selectbox("Select an AI agent", st.session_state.available_agents)
|
| 229 |
-
agent_chat_input = st.text_area("Enter your message for the agent:")
|
| 230 |
-
if st.button("Send to Agent"):
|
| 231 |
-
response = process_input(agent_chat_input)
|
| 232 |
-
st.session_state.chat_history.append((agent_chat_input, response))
|
| 233 |
-
st.write(f"{selected_agent}: {response}")
|
| 234 |
-
|
| 235 |
-
# Code Generation
|
| 236 |
-
st.subheader("Code Generation")
|
| 237 |
-
code_idea = st.text_input("Enter your code idea:")
|
| 238 |
-
selected_model = st.selectbox("Select a code-generative model", AVAILABLE_CODE_GENERATIVE_MODELS)
|
| 239 |
-
if st.button("Generate Code"):
|
| 240 |
-
generated_code = run_code(code_idea)
|
| 241 |
-
st.code(generated_code, language="python")
|
| 242 |
-
|
| 243 |
-
# Automate Build Process
|
| 244 |
-
st.subheader("Automate Build Process")
|
| 245 |
-
if st.button("Automate"):
|
| 246 |
-
agent = AIAgent(selected_agent, "", []) # Load the agent without skills for now
|
| 247 |
-
summary, next_step = agent.autonomous_build(st.session_state.chat_history, st.session_state.workspace_projects, project_name, selected_model, hf_token)
|
| 248 |
-
st.write("Autonomous Build Summary:")
|
| 249 |
-
st.write(summary)
|
| 250 |
-
st.write("Next Step:")
|
| 251 |
-
st.write(next_step)
|
| 252 |
-
|
| 253 |
-
if agent._hf_api and agent.has_valid_hf_token():
|
| 254 |
-
repository = agent.deploy_built_space_to_hf()
|
| 255 |
-
st.markdown("## Congratulations! Successfully deployed Space 🚀 ##")
|
| 256 |
-
st.markdown("[Check out your new Space here](hf.co/" + repository.name + ")")
|
| 257 |
-
|
| 258 |
-
if __name__ == "__main__":
|
| 259 |
-
st.sidebar.title("Navigation")
|
| 260 |
-
app_mode = st.sidebar.selectbox("Choose the app mode", ["AI Agent Creator", "Tool Box", "Workspace Chat App"])
|
| 261 |
|
| 262 |
if app_mode == "AI Agent Creator":
|
| 263 |
# AI Agent Creator
|
|
@@ -333,14 +169,15 @@ elif app_mode == "Workspace Chat App":
|
|
| 333 |
code_idea = st.text_input("Enter your code idea:")
|
| 334 |
selected_model = st.selectbox("Select a code-generative model", AVAILABLE_CODE_GENERATIVE_MODELS)
|
| 335 |
if st.button("Generate Code"):
|
| 336 |
-
|
|
|
|
| 337 |
st.code(generated_code, language="python")
|
| 338 |
|
| 339 |
# Automate Build Process
|
| 340 |
st.subheader("Automate Build Process")
|
| 341 |
if st.button("Automate"):
|
| 342 |
agent = AIAgent(selected_agent, "", []) # Load the agent without skills for now
|
| 343 |
-
summary, next_step = agent.autonomous_build(st.session_state.chat_history, st.session_state.workspace_projects,
|
| 344 |
st.write("Autonomous Build Summary:")
|
| 345 |
st.write(summary)
|
| 346 |
st.write("Next Step:")
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import os
|
| 3 |
import subprocess
|
| 4 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer, HfApi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Set your Hugging Face API key here
|
| 7 |
hf_token = "YOUR_HUGGING_FACE_API_KEY" # Replace with your actual token
|
| 8 |
|
|
|
|
| 9 |
PROJECT_ROOT = "projects"
|
| 10 |
AGENT_DIRECTORY = "agents"
|
| 11 |
+
AVAILABLE_CODE_GENERATIVE_MODELS = ["bigcode/starcoder", "Salesforce/codegen-350M-mono", "microsoft/CodeGPT-small"]
|
| 12 |
|
| 13 |
# Global state to manage communication between Tool Box and Workspace Chat App
|
| 14 |
if 'chat_history' not in st.session_state:
|
|
|
|
| 19 |
st.session_state.workspace_projects = {}
|
| 20 |
if 'available_agents' not in st.session_state:
|
| 21 |
st.session_state.available_agents = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
class AIAgent:
|
| 24 |
+
def __init__(self, name, description, skills):
|
| 25 |
self.name = name
|
| 26 |
self.description = description
|
| 27 |
self.skills = skills
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
def create_agent_prompt(self):
|
| 30 |
+
skills_str = '\n'.join([f"* {skill}" for skill in self.skills])
|
| 31 |
+
agent_prompt = f"""
|
| 32 |
+
As an elite expert developer, my name is {self.name}. I possess a comprehensive understanding of the following areas:
|
| 33 |
+
{skills_str}
|
| 34 |
+
|
| 35 |
+
I am confident that I can leverage my expertise to assist you in developing and deploying cutting-edge web applications. Please feel free to ask any questions or present any challenges you may encounter.
|
| 36 |
+
"""
|
| 37 |
+
return agent_prompt
|
| 38 |
|
| 39 |
+
def autonomous_build(self, chat_history, workspace_projects, project_name, selected_model, hf_token):
|
| 40 |
+
"""
|
| 41 |
+
Autonomous build logic that continues based on the state of chat history and workspace projects.
|
| 42 |
+
"""
|
| 43 |
+
# Example logic: Generate a summary of chat history and workspace state
|
| 44 |
+
summary = "Chat History:\n" + "\n".join([f"User: {u}\nAgent: {a}" for u, a in chat_history])
|
| 45 |
+
summary += "\n\nWorkspace Projects:\n" + "\n".join([f"{p}: {details}" for p, details in workspace_projects.items()])
|
| 46 |
|
| 47 |
+
# Example: Generate the next logical step in the project
|
| 48 |
+
next_step = "Based on the current state, the next logical step is to implement the main application logic."
|
| 49 |
+
|
| 50 |
+
return summary, next_step
|
| 51 |
|
| 52 |
def deploy_built_space_to_hf(self):
|
| 53 |
+
# Implement deployment logic here
|
| 54 |
+
pass
|
| 55 |
+
|
| 56 |
+
def process_input(input_text):
|
| 57 |
+
chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium", tokenizer="microsoft/DialoGPT-medium")
|
| 58 |
+
response = chatbot(input_text, max_length=50, num_return_sequences=1)[0]['generated_text']
|
| 59 |
+
return response
|
| 60 |
+
|
| 61 |
+
def run_code(code):
|
| 62 |
+
try:
|
| 63 |
+
result = subprocess.run(code, shell=True, capture_output=True, text=True)
|
| 64 |
+
return result.stdout
|
| 65 |
+
except Exception as e:
|
| 66 |
+
return str(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
def workspace_interface(project_name):
|
| 69 |
project_path = os.path.join(PROJECT_ROOT, project_name)
|
|
|
|
| 85 |
st.session_state.workspace_projects[project_name]['files'].append(file_name)
|
| 86 |
return f"Code added to '{file_name}' in project '{project_name}'."
|
| 87 |
|
| 88 |
+
def display_chat_history(chat_history):
|
| 89 |
+
return "\n".join([f"User: {u}\nAgent: {a}" for u, a in chat_history])
|
| 90 |
+
|
| 91 |
+
def display_workspace_projects(workspace_projects):
|
| 92 |
+
return "\n".join([f"{p}: {details}" for p, details in workspace_projects.items()])
|
| 93 |
+
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
st.sidebar.title("Navigation")
|
| 96 |
+
app_mode = st.sidebar.selectbox("Choose the app mode", ["AI Agent Creator", "Tool Box", "Workspace Chat App"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
if app_mode == "AI Agent Creator":
|
| 99 |
# AI Agent Creator
|
|
|
|
| 169 |
code_idea = st.text_input("Enter your code idea:")
|
| 170 |
selected_model = st.selectbox("Select a code-generative model", AVAILABLE_CODE_GENERATIVE_MODELS)
|
| 171 |
if st.button("Generate Code"):
|
| 172 |
+
generator = pipeline("text-generation", model=selected_model, tokenizer=selected_model)
|
| 173 |
+
generated_code = generator(code_idea, max_length=150, num_return_sequences=1)[0]['generated_text']
|
| 174 |
st.code(generated_code, language="python")
|
| 175 |
|
| 176 |
# Automate Build Process
|
| 177 |
st.subheader("Automate Build Process")
|
| 178 |
if st.button("Automate"):
|
| 179 |
agent = AIAgent(selected_agent, "", []) # Load the agent without skills for now
|
| 180 |
+
summary, next_step = agent.autonomous_build(st.session_state.chat_history, st.session_state.workspace_projects, project_name_input, selected_model, hf_token)
|
| 181 |
st.write("Autonomous Build Summary:")
|
| 182 |
st.write(summary)
|
| 183 |
st.write("Next Step:")
|