Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,24 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import os
|
| 4 |
import subprocess
|
| 5 |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
| 6 |
from huggingface_hub import HfApi
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
huggingface_token = huggingface_data["huggingface_json"]
|
| 13 |
-
except Exception as e:
|
| 14 |
-
st.error(f"Unable to load secrets: {str(e)}")
|
| 15 |
-
raise
|
| 16 |
|
| 17 |
PROJECT_ROOT = "projects"
|
| 18 |
AGENT_DIRECTORY = "agents"
|
|
@@ -36,13 +42,13 @@ class AIAgent:
|
|
| 36 |
self.name = name
|
| 37 |
self.description = description
|
| 38 |
self.skills = skills
|
| 39 |
-
self._hf_api = HfApi(token=huggingface_token)
|
| 40 |
|
| 41 |
def create_agent_prompt(self):
|
| 42 |
skills_str = '\n'.join([f"* {skill}" for skill in self.skills])
|
| 43 |
agent_prompt = f"""
|
| 44 |
As an elite expert developer, my name is {self.name}. I possess a comprehensive understanding of the following areas:
|
| 45 |
{skills_str}
|
|
|
|
| 46 |
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.
|
| 47 |
"""
|
| 48 |
return agent_prompt
|
|
@@ -54,12 +60,8 @@ I am confident that I can leverage my expertise to assist you in developing and
|
|
| 54 |
return summary, next_step
|
| 55 |
|
| 56 |
def deploy_built_space_to_hf(self):
|
| 57 |
-
# Implement logic to deploy the built space to Hugging Face Spaces
|
| 58 |
pass
|
| 59 |
|
| 60 |
-
def has_valid_hf_token(self):
|
| 61 |
-
return self._hf_api.whoami() is not None
|
| 62 |
-
|
| 63 |
def process_input(input_text):
|
| 64 |
chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium", tokenizer="microsoft/DialoGPT-medium")
|
| 65 |
response = chatbot(input_text, max_length=50, num_return_sequences=1)[0]['generated_text']
|
|
@@ -99,86 +101,74 @@ def display_workspace_projects(workspace_projects):
|
|
| 99 |
return "\n".join([f"{p}: {details}" for p, details in workspace_projects.items()])
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
-
st.
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
st.
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
st.
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
st.
|
| 157 |
-
st.
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
project_name = project_name_input
|
| 171 |
-
selected_model = selected_model
|
| 172 |
-
agent = AIAgent("Code Architect", "I am an expert in code generation and deployment.", ["Code Generation", "Deployment"])
|
| 173 |
-
summary, next_step = agent.autonomous_build(st.session_state.chat_history, st.session_state.workspace_projects, project_name, selected_model, huggingface_token)
|
| 174 |
-
st.write("Autonomous Build Summary:")
|
| 175 |
-
st.write(summary)
|
| 176 |
-
st.write("Next Step:")
|
| 177 |
-
st.write(next_step)
|
| 178 |
-
if agent._hf_api and agent.has_valid_hf_token():
|
| 179 |
-
repository = agent.deploy_built_space_to_hf()
|
| 180 |
-
st.markdown("## Congratulations! Successfully deployed Space �� ##")
|
| 181 |
-
st.markdown("[Check out your new Space here](hf.co/" + repository.name + ")")
|
| 182 |
|
| 183 |
# CSS for styling
|
| 184 |
st.markdown("""
|
|
@@ -191,14 +181,17 @@ if __name__ == "__main__":
|
|
| 191 |
margin: 0;
|
| 192 |
padding: 0;
|
| 193 |
}
|
|
|
|
| 194 |
h1, h2, h3, h4, h5, h6 {
|
| 195 |
color: #333;
|
| 196 |
}
|
|
|
|
| 197 |
.container {
|
| 198 |
width: 90%;
|
| 199 |
margin: 0 auto;
|
| 200 |
padding: 20px;
|
| 201 |
}
|
|
|
|
| 202 |
/* Navigation Sidebar */
|
| 203 |
.sidebar {
|
| 204 |
background-color: #2c3e50;
|
|
@@ -211,21 +204,25 @@ if __name__ == "__main__":
|
|
| 211 |
width: 250px;
|
| 212 |
overflow-y: auto;
|
| 213 |
}
|
|
|
|
| 214 |
.sidebar a {
|
| 215 |
color: #ecf0f1;
|
| 216 |
text-decoration: none;
|
| 217 |
display: block;
|
| 218 |
padding: 10px 0;
|
| 219 |
}
|
|
|
|
| 220 |
.sidebar a:hover {
|
| 221 |
background-color: #34495e;
|
| 222 |
border-radius: 5px;
|
| 223 |
}
|
|
|
|
| 224 |
/* Main Content */
|
| 225 |
.main-content {
|
| 226 |
margin-left: 270px;
|
| 227 |
padding: 20px;
|
| 228 |
}
|
|
|
|
| 229 |
/* Buttons */
|
| 230 |
button {
|
| 231 |
background-color: #3498db;
|
|
@@ -236,9 +233,11 @@ if __name__ == "__main__":
|
|
| 236 |
cursor: pointer;
|
| 237 |
font-size: 16px;
|
| 238 |
}
|
|
|
|
| 239 |
button:hover {
|
| 240 |
background-color: #2980b9;
|
| 241 |
}
|
|
|
|
| 242 |
/* Text Areas and Inputs */
|
| 243 |
textarea, input[type="text"] {
|
| 244 |
width: 100%;
|
|
@@ -248,10 +247,12 @@ if __name__ == "__main__":
|
|
| 248 |
border-radius: 5px;
|
| 249 |
box-sizing: border-box;
|
| 250 |
}
|
|
|
|
| 251 |
textarea:focus, input[type="text"]:focus {
|
| 252 |
border-color: #3498db;
|
| 253 |
outline: none;
|
| 254 |
}
|
|
|
|
| 255 |
/* Terminal Output */
|
| 256 |
.code-output {
|
| 257 |
background-color: #1e1e1e;
|
|
@@ -260,6 +261,7 @@ if __name__ == "__main__":
|
|
| 260 |
border-radius: 5px;
|
| 261 |
font-family: 'Courier New', Courier, monospace;
|
| 262 |
}
|
|
|
|
| 263 |
/* Chat History */
|
| 264 |
.chat-history {
|
| 265 |
background-color: #ecf0f1;
|
|
@@ -268,17 +270,21 @@ if __name__ == "__main__":
|
|
| 268 |
max-height: 300px;
|
| 269 |
overflow-y: auto;
|
| 270 |
}
|
|
|
|
| 271 |
.chat-message {
|
| 272 |
margin-bottom: 10px;
|
| 273 |
}
|
|
|
|
| 274 |
.chat-message.user {
|
| 275 |
text-align: right;
|
| 276 |
color: #3498db;
|
| 277 |
}
|
|
|
|
| 278 |
.chat-message.agent {
|
| 279 |
text-align: left;
|
| 280 |
color: #e74c3c;
|
| 281 |
}
|
|
|
|
| 282 |
/* Project Management */
|
| 283 |
.project-list {
|
| 284 |
background-color: #ecf0f1;
|
|
@@ -287,13 +293,16 @@ if __name__ == "__main__":
|
|
| 287 |
max-height: 300px;
|
| 288 |
overflow-y: auto;
|
| 289 |
}
|
|
|
|
| 290 |
.project-item {
|
| 291 |
margin-bottom: 10px;
|
| 292 |
}
|
|
|
|
| 293 |
.project-item a {
|
| 294 |
color: #3498db;
|
| 295 |
text-decoration: none;
|
| 296 |
}
|
|
|
|
| 297 |
.project-item a:hover {
|
| 298 |
text-decoration: underline;
|
| 299 |
}
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
|
| 4 |
+
# Access Hugging Face API key from secrets
|
| 5 |
+
hf_token = st.secrets["huggingface"]["hf_token"]
|
| 6 |
+
if not hf_token:
|
| 7 |
+
st.error("Hugging Face API key not found. Please make sure it is set in the secrets.")
|
| 8 |
+
Example Code Using Secrets
|
| 9 |
+
Here is the updated code snippet using the secrets from secrets.toml:
|
| 10 |
+
|
| 11 |
+
PYTHON
|
| 12 |
import streamlit as st
|
| 13 |
import os
|
| 14 |
import subprocess
|
| 15 |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
| 16 |
from huggingface_hub import HfApi
|
| 17 |
|
| 18 |
+
# Access Hugging Face API key from secrets
|
| 19 |
+
hf_token = st.secrets["huggingface"]["hf_token"]
|
| 20 |
+
if not hf_token:
|
| 21 |
+
st.error("Hugging Face API key not found. Please make sure it is set in the secrets.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
PROJECT_ROOT = "projects"
|
| 24 |
AGENT_DIRECTORY = "agents"
|
|
|
|
| 42 |
self.name = name
|
| 43 |
self.description = description
|
| 44 |
self.skills = skills
|
|
|
|
| 45 |
|
| 46 |
def create_agent_prompt(self):
|
| 47 |
skills_str = '\n'.join([f"* {skill}" for skill in self.skills])
|
| 48 |
agent_prompt = f"""
|
| 49 |
As an elite expert developer, my name is {self.name}. I possess a comprehensive understanding of the following areas:
|
| 50 |
{skills_str}
|
| 51 |
+
|
| 52 |
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.
|
| 53 |
"""
|
| 54 |
return agent_prompt
|
|
|
|
| 60 |
return summary, next_step
|
| 61 |
|
| 62 |
def deploy_built_space_to_hf(self):
|
|
|
|
| 63 |
pass
|
| 64 |
|
|
|
|
|
|
|
|
|
|
| 65 |
def process_input(input_text):
|
| 66 |
chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium", tokenizer="microsoft/DialoGPT-medium")
|
| 67 |
response = chatbot(input_text, max_length=50, num_return_sequences=1)[0]['generated_text']
|
|
|
|
| 101 |
return "\n".join([f"{p}: {details}" for p, details in workspace_projects.items()])
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|
| 104 |
+
st.sidebar.title("Navigation")
|
| 105 |
+
app_mode = st.sidebar.selectbox("Choose the app mode", ["Home", "Terminal", "Explorer", "Code Editor", "Build & Deploy"])
|
| 106 |
+
|
| 107 |
+
if app_mode == "Home":
|
| 108 |
+
st.title("Welcome to AI-Guided Development")
|
| 109 |
+
st.write("This application helps you build and deploy applications with the assistance of an AI Guide.")
|
| 110 |
+
st.write("Toggle the AI Guide from the sidebar to choose the level of assistance you need.")
|
| 111 |
+
|
| 112 |
+
elif app_mode == "Terminal":
|
| 113 |
+
st.header("Terminal")
|
| 114 |
+
terminal_input = st.text_input("Enter a command:")
|
| 115 |
+
if st.button("Run"):
|
| 116 |
+
output = run_code(terminal_input)
|
| 117 |
+
st.session_state.terminal_history.append((terminal_input, output))
|
| 118 |
+
st.code(output, language="bash")
|
| 119 |
+
if ai_guide_level != "No Assistance":
|
| 120 |
+
st.write("Run commands here to add packages to your project. For example: `pip install <package-name>`.")
|
| 121 |
+
if terminal_input and "install" in terminal_input:
|
| 122 |
+
package_name = terminal_input.split("install")[-1].strip()
|
| 123 |
+
st.write(f"Package `{package_name}` will be added to your project.")
|
| 124 |
+
|
| 125 |
+
elif app_mode == "Explorer":
|
| 126 |
+
st.header("Explorer")
|
| 127 |
+
uploaded_file = st.file_uploader("Upload a file", type=["py"])
|
| 128 |
+
if uploaded_file:
|
| 129 |
+
file_details = {"FileName": uploaded_file.name, "FileType": uploaded_file.type}
|
| 130 |
+
st.write(file_details)
|
| 131 |
+
save_path = os.path.join(PROJECT_ROOT, uploaded_file.name)
|
| 132 |
+
with open(save_path, "wb") as f:
|
| 133 |
+
f.write(uploaded_file.getbuffer())
|
| 134 |
+
st.success(f"File {uploaded_file.name} saved successfully!")
|
| 135 |
+
|
| 136 |
+
st.write("Drag and drop files into the 'app' folder.")
|
| 137 |
+
for project, details in st.session_state.workspace_projects.items():
|
| 138 |
+
st.write(f"Project: {project}")
|
| 139 |
+
for file in details['files']:
|
| 140 |
+
st.write(f" - {file}")
|
| 141 |
+
if st.button(f"Move {file} to app folder"):
|
| 142 |
+
# Logic to move file to 'app' folder
|
| 143 |
+
pass
|
| 144 |
+
if ai_guide_level != "No Assistance":
|
| 145 |
+
st.write("You can upload files and move them into the 'app' folder for building your application.")
|
| 146 |
+
|
| 147 |
+
elif app_mode == "Code Editor":
|
| 148 |
+
st.header("Code Editor")
|
| 149 |
+
code_editor = st.text_area("Write your code:", height=300)
|
| 150 |
+
if st.button("Save Code"):
|
| 151 |
+
# Logic to save code
|
| 152 |
+
pass
|
| 153 |
+
if ai_guide_level != "No Assistance":
|
| 154 |
+
st.write("The function `foo()` requires the `bar` package. Add it to `requirements.txt`.")
|
| 155 |
+
|
| 156 |
+
elif app_mode == "Build & Deploy":
|
| 157 |
+
st.header("Build & Deploy")
|
| 158 |
+
project_name_input = st.text_input("Enter Project Name for Automation:")
|
| 159 |
+
if st.button("Automate"):
|
| 160 |
+
selected_agent = st.selectbox("Select an AI agent", st.session_state.available_agents)
|
| 161 |
+
selected_model = st.selectbox("Select a code-generative model", AVAILABLE_CODE_GENERATIVE_MODELS)
|
| 162 |
+
agent = AIAgent(selected_agent, "", []) # Load the agent without skills for now
|
| 163 |
+
summary, next_step = agent.autonomous_build(st.session_state.chat_history, st.session_state.workspace_projects, project_name_input, selected_model, hf_token)
|
| 164 |
+
st.write("Autonomous Build Summary:")
|
| 165 |
+
st.write(summary)
|
| 166 |
+
st.write("Next Step:")
|
| 167 |
+
st.write(next_step)
|
| 168 |
+
if agent._hf_api and agent.has_valid_hf_token():
|
| 169 |
+
repository = agent.deploy_built_space_to_hf()
|
| 170 |
+
st.markdown("## Congratulations! Successfully deployed Space 🚀 ##")
|
| 171 |
+
st.markdown("[Check out your new Space here](hf.co/" + repository.name + ")")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
# CSS for styling
|
| 174 |
st.markdown("""
|
|
|
|
| 181 |
margin: 0;
|
| 182 |
padding: 0;
|
| 183 |
}
|
| 184 |
+
|
| 185 |
h1, h2, h3, h4, h5, h6 {
|
| 186 |
color: #333;
|
| 187 |
}
|
| 188 |
+
|
| 189 |
.container {
|
| 190 |
width: 90%;
|
| 191 |
margin: 0 auto;
|
| 192 |
padding: 20px;
|
| 193 |
}
|
| 194 |
+
|
| 195 |
/* Navigation Sidebar */
|
| 196 |
.sidebar {
|
| 197 |
background-color: #2c3e50;
|
|
|
|
| 204 |
width: 250px;
|
| 205 |
overflow-y: auto;
|
| 206 |
}
|
| 207 |
+
|
| 208 |
.sidebar a {
|
| 209 |
color: #ecf0f1;
|
| 210 |
text-decoration: none;
|
| 211 |
display: block;
|
| 212 |
padding: 10px 0;
|
| 213 |
}
|
| 214 |
+
|
| 215 |
.sidebar a:hover {
|
| 216 |
background-color: #34495e;
|
| 217 |
border-radius: 5px;
|
| 218 |
}
|
| 219 |
+
|
| 220 |
/* Main Content */
|
| 221 |
.main-content {
|
| 222 |
margin-left: 270px;
|
| 223 |
padding: 20px;
|
| 224 |
}
|
| 225 |
+
|
| 226 |
/* Buttons */
|
| 227 |
button {
|
| 228 |
background-color: #3498db;
|
|
|
|
| 233 |
cursor: pointer;
|
| 234 |
font-size: 16px;
|
| 235 |
}
|
| 236 |
+
|
| 237 |
button:hover {
|
| 238 |
background-color: #2980b9;
|
| 239 |
}
|
| 240 |
+
|
| 241 |
/* Text Areas and Inputs */
|
| 242 |
textarea, input[type="text"] {
|
| 243 |
width: 100%;
|
|
|
|
| 247 |
border-radius: 5px;
|
| 248 |
box-sizing: border-box;
|
| 249 |
}
|
| 250 |
+
|
| 251 |
textarea:focus, input[type="text"]:focus {
|
| 252 |
border-color: #3498db;
|
| 253 |
outline: none;
|
| 254 |
}
|
| 255 |
+
|
| 256 |
/* Terminal Output */
|
| 257 |
.code-output {
|
| 258 |
background-color: #1e1e1e;
|
|
|
|
| 261 |
border-radius: 5px;
|
| 262 |
font-family: 'Courier New', Courier, monospace;
|
| 263 |
}
|
| 264 |
+
|
| 265 |
/* Chat History */
|
| 266 |
.chat-history {
|
| 267 |
background-color: #ecf0f1;
|
|
|
|
| 270 |
max-height: 300px;
|
| 271 |
overflow-y: auto;
|
| 272 |
}
|
| 273 |
+
|
| 274 |
.chat-message {
|
| 275 |
margin-bottom: 10px;
|
| 276 |
}
|
| 277 |
+
|
| 278 |
.chat-message.user {
|
| 279 |
text-align: right;
|
| 280 |
color: #3498db;
|
| 281 |
}
|
| 282 |
+
|
| 283 |
.chat-message.agent {
|
| 284 |
text-align: left;
|
| 285 |
color: #e74c3c;
|
| 286 |
}
|
| 287 |
+
|
| 288 |
/* Project Management */
|
| 289 |
.project-list {
|
| 290 |
background-color: #ecf0f1;
|
|
|
|
| 293 |
max-height: 300px;
|
| 294 |
overflow-y: auto;
|
| 295 |
}
|
| 296 |
+
|
| 297 |
.project-item {
|
| 298 |
margin-bottom: 10px;
|
| 299 |
}
|
| 300 |
+
|
| 301 |
.project-item a {
|
| 302 |
color: #3498db;
|
| 303 |
text-decoration: none;
|
| 304 |
}
|
| 305 |
+
|
| 306 |
.project-item a:hover {
|
| 307 |
text-decoration: underline;
|
| 308 |
}
|