File size: 7,046 Bytes
a98a37e
3266cac
a0a17b8
4ebd6c7
 
 
a0a17b8
4ebd6c7
 
 
 
b0944ea
4ebd6c7
 
 
 
c6d7c50
4ebd6c7
 
c6d7c50
 
9f232dd
4ebd6c7
9f232dd
 
 
9cf63c4
4ebd6c7
9f232dd
9cf63c4
 
 
 
 
 
 
 
9f232dd
4ebd6c7
 
9cf63c4
9f232dd
4ebd6c7
 
f0a0e00
4ebd6c7
9f232dd
4ebd6c7
 
 
 
 
20250e3
4ebd6c7
 
 
20250e3
4ebd6c7
 
20250e3
4ebd6c7
 
20250e3
 
4ebd6c7
 
9cf63c4
 
 
4ebd6c7
9cf63c4
 
9f232dd
035579e
4ebd6c7
f0a0e00
 
 
4ebd6c7
9cf63c4
 
 
 
 
 
 
 
 
 
 
 
 
 
4ebd6c7
9cf63c4
 
 
 
 
 
 
 
 
f0a0e00
9f232dd
b0944ea
 
f0a0e00
b0944ea
4ebd6c7
 
 
 
 
 
b0944ea
4ebd6c7
 
 
b0944ea
4ebd6c7
 
 
 
 
 
 
 
 
b0944ea
4ebd6c7
 
 
d76e388
4ebd6c7
 
 
f0a0e00
4ebd6c7
 
 
 
 
 
20250e3
4ebd6c7
20250e3
4ebd6c7
 
 
20250e3
4ebd6c7
 
 
 
 
 
 
 
 
 
 
5e6f07a
 
 
4ebd6c7
 
 
 
 
 
 
 
 
9cf63c4
 
 
4ebd6c7
1158227
4ebd6c7
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import os

import streamlit as st
import subprocess
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer, AutoModel, RagRetriever, AutoModelForSeq2SeqLM
import black
from pylint import lint
import sys
import torch
from huggingface_hub import hf_hub_url, cached_download, HfApi
import base64

# Set your Hugging Face API key here
# hf_token = "YOUR_HUGGING_FACE_API_KEY"  # Replace with your actual token
# Get Hugging Face token from secrets.toml - this line should already be in the main code
hf_token = st.secrets["huggingface"]["hf_token"]

HUGGING_FACE_REPO_URL = "https://huggingface.co/spaces/acecalisto3/DevToolKit"
PROJECT_ROOT = "projects"
    return refined_response

class AIAgent:
    def __init__(self, name, description, skills, hf_api=None):
        self.name = name
        self.description = description
        self.skills = skills
        self._hf_api = hf_api
        self._hf_token = hf_token  # Store the token here

    @property
    def hf_api(self):
        if not self._hf_api and self.has_valid_hf_token():
            self._hf_api = HfApi(token=self._hf_token)
        return self._hf_api

    def has_valid_hf_token(self):
        return bool(self._hf_token)

    async def autonomous_build(self, chat_history, workspace_projects, project_name, selected_model, hf_token):
        self._hf_token = hf_token
        # Continuation of previous methods

        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()])

            st.error(f"Build Error: {e}")

        return summary, next_step
    
    def deploy_built_space_to_hf(self):
        if not self._hf_api or not self._hf_token:
            raise ValueError("Cannot deploy the Space since no valid Hugoging Face API connection was established.")

        # Assuming you have a function to get the files for your Space
        repository_name = f"my-awesome-space_{datetime.now().timestamp()}" 
        files = get_built_space_files() # Placeholder - you'll need to define this function

        # Create the Space
        create_space(self.hf_api, repository_name, "Description", True, files) 

        st.markdown("## Congratulations! Successfully deployed Space 🚀 ##")
        st.markdown(f"[Check out your new Space here](https://huggingface.co/spaces/{repository_name})")


# Add any missing functions from your original code (e.g., get_built_space_files)
def get_built_space_files():
    # Replace with your logic to gather the files you want to deploy
    return {
        "app.py": "# Your Streamlit app code here",
        "requirements.txt": "streamlit\ntransformers" 
        # Add other files as needed
    }

def save_agent_to_file(agent):
    """Saves the agent's prompt to a file."""
    st.session_state.workspace_projects[project_name]['files'].append(file_name)
    return f"Code added to '{file_name}' in project '{project_name}'."

def create_space(api, name, description, public, files, entrypoint="launch.py"):
    url = f"{hf_hub_url()}spaces/{name}/prepare-repo"
    headers = {"Authorization": f"Bearer {api.access_token}"}
    payload = {
        "public": public,
        "gitignore_template": "web",
        "default_branch": "main",
        "archived": False,
        "files": []
    }
    for filename, contents in files.items():
        data = {
            "content": contents,
            "path": filename,
            "encoding": "utf-8",
            "mode": "overwrite" if "#\{random.randint(0, 1)\}" not in contents else "merge",
        }
        payload["files"].append(data)
    response = requests.post(url, json=payload, headers=headers)
    response.raise_for_status()
    location = response.headers.get("Location")
    # wait_for_processing(location, api)  # You might need to implement this if it's not already defined

    return Repository(name=name, api=api)

# Streamlit App
st.title("AI Agent Creator")

elif app_mode == "Workspace Chat App":
    # Workspace Chat App
    st.header("Workspace Chat App")
def get_built_space_files():
    """
    Gathers the necessary files for the Hugging Face Space, 
    handling different project structures and file types.
    """
    files = {}

    # Get the current project name (adjust as needed)
    project_name = st.session_state.get('project_name', 'my_project') 
    project_path = os.path.join(PROJECT_ROOT, project_name)

    # Define a list of files/directories to search for
    targets = [
        "app.py", 
        "requirements.txt",
        "Dockerfile",  
        "docker-compose.yml", # Example YAML file
        "src",          # Example subdirectory
        "assets"        # Another example subdirectory
    ]

    # Iterate through the targets
    for target in targets:
        target_path = os.path.join(project_path, target)

        # If the target is a file, add it to the files dictionary
        if os.path.isfile(target_path):
            add_file_to_dictionary(files, target_path)

        # If the target is a directory, recursively search for files within it
        elif os.path.isdir(target_path):
            for root, _, filenames in os.walk(target_path):
                for filename in filenames:
                    file_path = os.path.join(root, filename)
                    add_file_to_dictionary(files, file_path)

    return files

def add_file_to_dictionary(files, file_path):
    """Helper function to add a file to the files dictionary."""
    filename = os.path.relpath(file_path, PROJECT_ROOT) # Get relative path

    # Handle text and binary files
    if filename.endswith((".py", ".txt", ".json", ".html", ".css", ".yml", ".yaml")):
        with open(file_path, "r") as f:
            files[filename] = f.read()
    else:
        with open(file_path, "rb") as f:
            file_content = f.read()
            files[filename] = base64.b64encode(file_content).decode("utf-8")
    # Project Workspace Creation
    st.subheader("Create a New Project")
    project_name = st.text_input("Enter project name:")
        st.write(summary)
        st.write("Next Step:")
        st.write(next_step)
    
    # Using the modified and extended class and functions, update the callback for the 'Automate' button in the Streamlit UI:
    if st.button("Automate", args=(hf_token,)):
    agent = AIAgent(selected_agent, "", [])  # Load the agent without skills for now
    summary, next_step = agent.autonomous_build(st.session_state.chat_history, st.session_state.workspace_projects, project_name, selected_model, hf_token)
    st.write("Autonomous Build Summary:")
    st.write(summary)
    st.write("Next Step:")
    st.write(next_step)

    # If everything went well, proceed to deploy the Space
    if agent._hf_api and agent.has_valid_hf_token():
        agent.deploy_built_space_to_hf() 
        # Use the hf_token to interact with the Hugging Face API
        api = HfApi(token=hf_token)
        # Function to create a Space on Hugging Face