Spaces:
Build error
Build error
import os | |
import streamlit as st | |
from langchain_community.utilities import GoogleSearchAPIWrapper | |
from langchain_google_generativeai import GoogleGenerativeAI | |
from langchain.llms import GooglePalm | |
from langchain.chains import ConversationChain | |
from langchain.memory import ConversationBufferMemory | |
import subprocess | |
import git | |
import logging | |
# Configure logging | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
# API Key Input | |
if "GOOGLE_API_KEY" not in st.session_state: | |
st.session_state.GOOGLE_API_KEY = "" | |
st.header("Enter your Google Search API Key") | |
st.session_state.GOOGLE_API_KEY = st.text_input("API Key:", value=st.session_state.GOOGLE_API_KEY, type="password") | |
# Initialize Google Search API Wrapper (only if API key is provided) | |
if st.session_state.GOOGLE_API_KEY: | |
search = GoogleSearchAPIWrapper(google_api_key=st.session_state.GOOGLE_API_KEY) | |
# Agents | |
agents = { | |
"WEB_DEV": { | |
"description": "Expert in web development technologies and frameworks.", | |
"skills": ["HTML", "CSS", "JavaScript", "React", "Vue.js", "Flask", "Django", "Node.js", "Express.js"], | |
"system_prompt": "You are a web development expert. Your goal is to assist the user in building and deploying web applications. Provide code snippets, explanations, and guidance on best practices.", | |
}, | |
"AI_SYSTEM_PROMPT": { | |
"description": "Expert in designing and implementing AI systems.", | |
"skills": ["Machine Learning", "Deep Learning", "Natural Language Processing", "Computer Vision", "Reinforcement Learning"], | |
"system_prompt": "You are an AI system expert. Your goal is to assist the user in designing and implementing AI systems. Provide code snippets, explanations, and guidance on best practices.", | |
}, | |
"PYTHON_CODE_DEV": { | |
"description": "Expert in Python programming and development.", | |
"skills": ["Python", "Data Structures", "Algorithms", "Object-Oriented Programming", "Functional Programming"], | |
"system_prompt": "You are a Python code development expert. Your goal is to assist the user in writing and debugging Python code. Provide code snippets, explanations, and guidance on best practices.", | |
}, | |
"CODE_REVIEW_ASSISTANT": { | |
"description": "Expert in code review and quality assurance.", | |
"skills": ["Code Style", "Best Practices", "Security", "Performance", "Maintainability"], | |
"system_prompt": "You are a code review expert. Your goal is to assist the user in reviewing and improving their code. Provide feedback on code quality, style, and best practices.", | |
}, | |
} | |
# Session State | |
if "workspace_projects" not in st.session_state: | |
st.session_state.workspace_projects = {} | |
if "chat_history" not in st.session_state: | |
st.session_state.chat_history = [] | |
if "active_agent" not in st.session_state: | |
st.session_state.active_agent = None | |
if "selected_agents" not in st.session_state: | |
st.session_state.selected_agents = [] | |
if "current_project" not in st.session_state: | |
st.session_state.current_project = None | |
# Helper Functions | |
def add_code_to_workspace(project_name: str, code: str, file_name: str): | |
if project_name in st.session_state.workspace_projects: | |
st.session_state.workspace_projects[project_name]['files'].append({'file_name': file_name, 'code': code}) | |
return f"Added code to {file_name} in project {project_name}" | |
else: | |
return f"Project {project_name} does not exist" | |
def terminal_interface(command: str, project_name: str): | |
try: | |
if project_name in st.session_state.workspace_projects: | |
result = subprocess.run(command, cwd=project_name, shell=True, capture_output=True, text=True) | |
return result.stdout + result.stderr | |
else: | |
return f"Project {project_name} does not exist" | |
except FileNotFoundError: | |
return f"Error: Command not found. Please check your command." | |
except Exception as e: | |
logging.error(f"An error occurred: {e}") | |
return f"An unexpected error occurred while running the command." | |
def get_agent_response(message: str, system_prompt: str): | |
llm = GoogleGenerativeAI(google_api_key=st.session_state.GOOGLE_API_KEY) # Use GoogleGenerativeAI | |
memory = ConversationBufferMemory() | |
conversation = ConversationChain(llm=llm, memory=memory) | |
full_prompt = f"{system_prompt}\n{message}" | |
response = conversation.run(full_prompt) | |
return response | |
def display_agent_info(agent_name: str): | |
agent = agents[agent_name] | |
st.sidebar.subheader(f"Active Agent: {agent_name}") | |
st.sidebar.write(f"Description: {agent['description']}") | |
st.sidebar.write(f"Skills: {', '.join(agent['skills'])}") | |
def display_workspace_projects(): | |
st.subheader("Workspace Projects") | |
for project_name, project_data in st.session_state.workspace_projects.items(): | |
with st.expander(project_name): | |
for file in project_data['files']: | |
st.text(file['file_name']) | |
st.code(file['code'], language="python") | |
def display_chat_history(): | |
st.subheader("Chat History") | |
for message in st.session_state.chat_history: | |
st.text(message) | |
def run_autonomous_build(selected_agents: list[str], project_name: str): | |
st.info("Starting autonomous build process...") | |
for agent in selected_agents: | |
st.write(f"Agent {agent} is working on the project...") | |
prompt = f"Generate Python code for a simple web application using Flask framework in project {project_name}. Include instructions for running the application." | |
code = get_agent_response(prompt, agents[agent]['system_prompt']) | |
add_code_to_workspace(project_name, code, f"{agent.lower()}_app.py") | |
st.write(f"Agent {agent} has completed its task.") | |
st.success("Autonomous build process completed!") | |
def collaborative_agent_example(selected_agents: list[str], project_name: str, task: str): | |
st.info(f"Starting collaborative task: {task}") | |
responses = {} | |
for agent in selected_agents: | |
st.write(f"Agent {agent} is working on the task...") | |
response = get_agent_response(task, agents[agent]['system_prompt']) | |
responses[agent] = response | |
combined_response = combine_and_process_responses(responses, task) | |
st.success("Collaborative task completed!") | |
st.write(combined_response) | |
def combine_and_process_responses(responses: dict[str, str], task: str) -> str: | |
combined = "\n\n".join([f"{agent}: {response}" for agent, response in responses.items()]) | |
return f"Combined response for task '{task}':\n\n{combined}" | |
# Streamlit UI | |
st.title("DevToolKit: AI-Powered Development Environment") | |
# Project Management | |
st.header("Project Management") | |
project_name = st.text_input("Enter project name:") | |
if st.button("Create Project"): | |
if project_name and project_name not in st.session_state.workspace_projects: | |
st.session_state.workspace_projects[project_name] = {'files': []} | |
st.success(f"Created project: {project_name}") | |
os.makedirs(project_name, exist_ok=True) | |
elif project_name in st.session_state.workspace_projects: | |
st.warning(f"Project {project_name} already exists") | |
else: | |
st.warning("Please enter a project name") | |
# Code Editor | |
st.subheader("Code Editor") | |
if st.session_state.workspace_projects: | |
selected_project = st.selectbox("Select project", list(st.session_state.workspace_projects.keys())) | |
if selected_project: | |
files = [file['file_name'] for file in st.session_state.workspace_projects[selected_project]['files']] | |
selected_file = st.selectbox("Select file to edit", files) if files else None | |
if selected_file: | |
file_content = next((file['code'] for file in st.session_state.workspace_projects[selected_project]['files'] | |
if file['file_name'] == selected_file), "") | |
edited_code = st.text_area("Edit code", value=file_content, height=300) | |
if st.button("Save Changes"): | |
for file in st.session_state.workspace_projects[selected_project]['files']: | |
if file['file_name'] == selected_file: | |
file['code'] = edited_code | |
file_path = os.path.join(selected_project, selected_file) | |
with open(file_path, "w") as f: | |
f.write(edited_code) | |
st.success("Changes saved successfully!") | |
break | |
else: | |
st.info("No files in the project. Use the chat interface to generate code.") | |
else: | |
st.info("No projects created yet. Create a project to start coding.") | |
# Terminal Interface | |
st.subheader("Terminal (Workspace Context)") | |
if st.session_state.workspace_projects: | |
selected_project = st.selectbox("Select project for terminal", | |
list(st.session_state.workspace_projects.keys()), | |
key="terminal_project_select") | |
terminal_input = st.text_input("Enter a command within the workspace:") | |
if st.button("Run Command"): | |
terminal_output = terminal_interface(terminal_input, selected_project) | |
st.code(terminal_output, language="bash") | |
else: | |
st.info("No projects created yet. Create a project to use the terminal.") | |
# Chat Interface | |
st.subheader("Chat with AI Agents") | |
selected_agents = st.multiselect("Select AI agents", list(agents.keys()), key="agent_select") | |
st.session_state.selected_agents = selected_agents | |
agent_chat_input = st.text_area("Enter your message for the agents:", key="agent_input") | |
if st.button("Send to Agents", key="agent_send"): | |
if selected_agents and agent_chat_input: | |
responses = {} | |
for agent in selected_agents: | |
response = get_agent_response(agent_chat_input, agents[agent]['system_prompt']) | |
responses[agent] = response | |
st.session_state.chat_history.append(f"User: {agent_chat_input}") | |
for agent, response in responses.items(): | |
st.session_state.chat_history.append(f"{agent}: {response}") | |
st.text_area("Chat History", value='\n'.join(st.session_state.chat_history), height=300) | |
else: | |
st.warning("Please select at least one agent and enter a message.") | |
# Agent Control | |
st.subheader("Agent Control") | |
for agent_name in agents: | |
agent = agents[agent_name] | |
with st.expander(f"{agent_name} ({agent['description']})"): | |
if st.button(f"Activate {agent_name}", key=f"activate_{agent_name}"): | |
st.session_state.active_agent = agent_name | |
st.success(f"{agent_name} activated.") | |
if st.button(f"Deactivate {agent_name}", key=f"deactivate_{agent_name}"): | |
st.session_state.active_agent = None | |
st.success(f"{agent_name} deactivated.") | |
# Automate Build Process | |
st.subheader("Automate Build Process") | |
if st.button("Automate"): | |
if st.session_state.selected_agents and project_name: | |
run_autonomous_build(st.session_state.selected_agents, project_name) | |
else: | |
st.warning("Please select at least one agent and create a project.") | |
# Version Control | |
st.subheader("Version Control") | |
repo_url = st.text_input("Enter repository URL:") | |
if st.button("Clone Repository"): | |
if repo_url and project_name: | |
try: | |
git.Repo.clone_from(repo_url, project_name) | |
st.success(f"Repository cloned successfully to {project_name}") | |
except git.GitCommandError as e: | |
st.error(f"Error cloning repository: {e}") | |
else: | |
st.warning("Please enter a repository URL and create a project.") | |
# Collaborative Agent Example | |
st.subheader("Collaborative Agent Example") | |
collab_agents = st.multiselect("Select AI agents for collaboration", | |
list(agents.keys()), | |
key="collab_agent_select") | |
collab_project = st.text_input("Enter project name for collaboration:") | |
collab_task = st.text_input("Enter collaborative task:") | |
if st.button("Start Collaborative Task"): | |
if collab_agents and collab_project and collab_task: | |
collaborative_agent_example(collab_agents, collab_project, collab_task) | |
else: | |
st.warning("Please select agents, enter a project name, and a task.") | |
else: | |
st.warning("Please enter your Google Search API Key to continue.") |