import streamlit as st from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer, RagRetriever, AutoModelForSeq2SeqLM import os import subprocess import black from pylint import lint from io import StringIO import sys import torch from huggingface_hub import hf_hub_url, cached_download, HfApi # Access Hugging Face API key from secrets hf_token = st.secrets["hf_token"] if not hf_token: st.error("Hugging Face API key not found. Please make sure it is set in the secrets.") HUGGING_FACE_REPO_URL = "https://huggingface.co/spaces/acecalisto3/DevToolKit" PROJECT_ROOT = "projects" AGENT_DIRECTORY = "agents" AVAILABLE_CODE_GENERATIVE_MODELS = ["bigcode/starcoder", "Salesforce/codegen-350M-mono", "microsoft/CodeGPT-small"] # Global state to manage communication between Tool Box and Workspace Chat App if 'chat_history' not in st.session_state: st.session_state.chat_history = [] if 'terminal_history' not in st.session_state: st.session_state.terminal_history = [] if 'workspace_projects' not in st.session_state: st.session_state.workspace_projects = {} if 'available_agents' not in st.session_state: st.session_state.available_agents = [] # Initialize the session state variable as an empty string st.session_state.user_input = "" # Create the text input widget user_input = st.text_input("Enter your text:", "Initial text") # Use the get() method to get the current value of the widget and update it if st.button("Update"): st.session_state.user_input = user_input # AI Guide Toggle ai_guide_level = st.sidebar.radio("AI Guide Level", ["Full Assistance", "Partial Assistance", "No Assistance"]) class AIAgent: def __init__(self, name, description, skills): self.name = name self.description = description self.skills = skills self._hf_api = HfApi() # Initialize HfApi here def create_agent_prompt(self): skills_str = '\n'.join([f"* {skill}" for skill in self.skills]) agent_prompt = f""" As an elite expert developer, my name is {self.name}. I possess a comprehensive understanding of the following areas: {skills_str} 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. """ return agent_prompt def autonomous_build(self, chat_history, workspace_projects, project_name, selected_model, hf_token): summary = "Chat History:\n" + "\n".join([f"User: {u}\nAgent: {a}" for u, a in chat_history]) summary += "\n\nWorkspace Projects:\n" + "\n".join([f"{p}: {details}" for p, details in workspace_projects.items()]) next_step = "Based on the current state, the next logical step is to implement the main application logic." return summary, next_step def deploy_built_space_to_hf(self): # Assuming you have a function that generates the space content space_content = generate_space_content(project_name) repository = self._hf_api.create_repo( repo_id=project_name, private=True, token=hf_token, exist_ok=True, space_sdk="streamlit" ) self._hf_api.upload_file( path_or_fileobj=space_content, path_in_repo="app.py", repo_id=project_name, repo_type="space", token=hf_token ) return repository def has_valid_hf_token(self): return self._hf_api.whoami(token=hf_token) is not None def process_input(input_text): chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium", tokenizer="microsoft/DialoGPT-medium") response = chatbot(input_text, max_length=50, num_return_sequences=1)[0]['generated_text'] return response def run_code(code): try: result = subprocess.run(code, shell=True, capture_output=True, text=True) return result.stdout except Exception as e: return str(e) def workspace_interface(project_name): project_path = os.path.join(PROJECT_ROOT, project_name) if not os.path.exists(project_path): os.makedirs(project_path) st.session_state.workspace_projects[project_name] = {'files': []} return f"Project '{project_name}' created successfully." else: return f"Project '{project_name}' already exists." def add_code_to_workspace(project_name, code, file_name): project_path = os.path.join(PROJECT_ROOT, project_name) if not os.path.exists(project_path): return f"Project '{project_name}' does not exist." file_path = os.path.join(project_path, file_name) with open(file_path, "w") as file: file.write(code) st.session_state.workspace_projects[project_name]['files'].append(file_name) return f"Code added to '{file_name}' in project '{project_name}'." def display_chat_history(chat_history): return "\n".join([f"User: {u}\nAgent: {a}" for u, a in chat_history]) def display_workspace_projects(workspace_projects): return "\n".join([f"{p}: {details}" for p, details in workspace_projects.items()]) def generate_space_content(project_name): # Logic to generate the Streamlit app content based on project_name # ... (This is where you'll need to implement the actual code generation) return "import streamlit as st\nst.title('My Streamlit App')\nst.write('Hello, world!')" # Function to display the AI Guide chat def display_ai_guide_chat(chat_history): st.markdown("