Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import os
|
| 2 |
import subprocess
|
| 3 |
import streamlit as st
|
| 4 |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
|
@@ -43,8 +42,8 @@ I am confident that I can leverage my expertise to assist you in developing and
|
|
| 43 |
|
| 44 |
def autonomous_build(self, chat_history, workspace_projects):
|
| 45 |
"""
|
| 46 |
-
Autonomous build logic
|
| 47 |
-
|
| 48 |
"""
|
| 49 |
summary = "Chat History:\n" + "\n".join([f"User: {u}\nAgent: {a}" for u, a in chat_history])
|
| 50 |
summary += "\n\nWorkspace Projects:\n" + "\n".join(
|
|
@@ -56,7 +55,7 @@ I am confident that I can leverage my expertise to assist you in developing and
|
|
| 56 |
|
| 57 |
|
| 58 |
def save_agent_to_file(agent):
|
| 59 |
-
"""Saves the agent's
|
| 60 |
if not os.path.exists(AGENT_DIRECTORY):
|
| 61 |
os.makedirs(AGENT_DIRECTORY)
|
| 62 |
file_path = os.path.join(AGENT_DIRECTORY, f"{agent.name}.txt")
|
|
@@ -67,7 +66,8 @@ def save_agent_to_file(agent):
|
|
| 67 |
file.write(f"Agent Name: {agent.name}\nDescription: {agent.description}")
|
| 68 |
st.session_state.available_agents.append(agent.name)
|
| 69 |
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
|
| 73 |
def load_agent_prompt(agent_name):
|
|
@@ -82,6 +82,7 @@ def load_agent_prompt(agent_name):
|
|
| 82 |
|
| 83 |
|
| 84 |
def create_agent_from_text(name, text):
|
|
|
|
| 85 |
skills = text.split('\n')
|
| 86 |
agent = AIAgent(name, "AI agent created from text input.", skills)
|
| 87 |
save_agent_to_file(agent)
|
|
@@ -94,7 +95,7 @@ def chat_interface_with_agent(input_text, agent_name):
|
|
| 94 |
if agent_prompt is None:
|
| 95 |
return f"Agent {agent_name} not found."
|
| 96 |
|
| 97 |
-
# Load the GPT-2 model
|
| 98 |
model_name = "gpt2"
|
| 99 |
try:
|
| 100 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
@@ -103,19 +104,20 @@ def chat_interface_with_agent(input_text, agent_name):
|
|
| 103 |
except EnvironmentError as e:
|
| 104 |
return f"Error loading model: {e}"
|
| 105 |
|
| 106 |
-
# Combine
|
| 107 |
combined_input = f"{agent_prompt}\n\nUser: {input_text}\nAgent:"
|
| 108 |
-
|
| 109 |
-
# Truncate input text to avoid exceeding the model's maximum length
|
| 110 |
-
max_input_length = 900
|
| 111 |
input_ids = tokenizer.encode(combined_input, return_tensors="pt")
|
| 112 |
if input_ids.shape[1] > max_input_length:
|
| 113 |
input_ids = input_ids[:, :max_input_length]
|
| 114 |
|
| 115 |
-
# Generate
|
| 116 |
outputs = model.generate(
|
| 117 |
-
input_ids,
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
| 119 |
)
|
| 120 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 121 |
return response
|
|
@@ -123,7 +125,7 @@ def chat_interface_with_agent(input_text, agent_name):
|
|
| 123 |
|
| 124 |
# Basic chat interface (no agent)
|
| 125 |
def chat_interface(input_text):
|
| 126 |
-
# Load the GPT-2 model
|
| 127 |
model_name = "gpt2"
|
| 128 |
try:
|
| 129 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
@@ -132,13 +134,14 @@ def chat_interface(input_text):
|
|
| 132 |
except EnvironmentError as e:
|
| 133 |
return f"Error loading model: {e}"
|
| 134 |
|
| 135 |
-
# Generate
|
| 136 |
outputs = generator(input_text, max_new_tokens=50, num_return_sequences=1, do_sample=True)
|
| 137 |
response = outputs[0]['generated_text']
|
| 138 |
return response
|
| 139 |
|
| 140 |
|
| 141 |
def workspace_interface(project_name):
|
|
|
|
| 142 |
project_path = os.path.join(PROJECT_ROOT, project_name)
|
| 143 |
if not os.path.exists(PROJECT_ROOT):
|
| 144 |
os.makedirs(PROJECT_ROOT)
|
|
@@ -146,13 +149,15 @@ def workspace_interface(project_name):
|
|
| 146 |
os.makedirs(project_path)
|
| 147 |
st.session_state.workspace_projects[project_name] = {"files": []}
|
| 148 |
st.session_state.current_state['workspace_chat']['project_name'] = project_name
|
| 149 |
-
|
|
|
|
| 150 |
return f"Project {project_name} created successfully."
|
| 151 |
else:
|
| 152 |
return f"Project {project_name} already exists."
|
| 153 |
|
| 154 |
|
| 155 |
def add_code_to_workspace(project_name, code, file_name):
|
|
|
|
| 156 |
project_path = os.path.join(PROJECT_ROOT, project_name)
|
| 157 |
if os.path.exists(project_path):
|
| 158 |
file_path = os.path.join(project_path, file_name)
|
|
@@ -160,13 +165,14 @@ def add_code_to_workspace(project_name, code, file_name):
|
|
| 160 |
file.write(code)
|
| 161 |
st.session_state.workspace_projects[project_name]["files"].append(file_name)
|
| 162 |
st.session_state.current_state['workspace_chat']['added_code'] = {"file_name": file_name, "code": code}
|
| 163 |
-
|
|
|
|
| 164 |
return f"Code added to {file_name} in project {project_name} successfully."
|
| 165 |
else:
|
| 166 |
return f"Project {project_name} does not exist."
|
| 167 |
|
| 168 |
-
|
| 169 |
def terminal_interface(command, project_name=None):
|
|
|
|
| 170 |
if project_name:
|
| 171 |
project_path = os.path.join(PROJECT_ROOT, project_name)
|
| 172 |
if not os.path.exists(project_path):
|
|
@@ -174,6 +180,7 @@ def terminal_interface(command, project_name=None):
|
|
| 174 |
result = subprocess.run(command, cwd=project_path, shell=True, capture_output=True, text=True)
|
| 175 |
else:
|
| 176 |
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
|
|
|
| 177 |
if result.returncode == 0:
|
| 178 |
st.session_state.current_state['toolbox']['terminal_output'] = result.stdout
|
| 179 |
return result.stdout
|
|
@@ -183,6 +190,7 @@ def terminal_interface(command, project_name=None):
|
|
| 183 |
|
| 184 |
|
| 185 |
def summarize_text(text):
|
|
|
|
| 186 |
summarizer = pipeline("summarization")
|
| 187 |
summary = summarizer(text, max_length=100, min_length=25, do_sample=False)
|
| 188 |
st.session_state.current_state['toolbox']['summary'] = summary[0]['summary_text']
|
|
@@ -190,6 +198,7 @@ def summarize_text(text):
|
|
| 190 |
|
| 191 |
|
| 192 |
def sentiment_analysis(text):
|
|
|
|
| 193 |
analyzer = pipeline("sentiment-analysis")
|
| 194 |
sentiment = analyzer(text)
|
| 195 |
st.session_state.current_state['toolbox']['sentiment'] = sentiment[0]
|
|
@@ -197,15 +206,15 @@ def sentiment_analysis(text):
|
|
| 197 |
|
| 198 |
|
| 199 |
def code_editor_interface(code):
|
| 200 |
-
"""Formats and lints Python code
|
| 201 |
try:
|
| 202 |
formatted_code = black.format_str(code, mode=black.FileMode())
|
| 203 |
lint_result = StringIO()
|
| 204 |
lint.Run([
|
| 205 |
-
'--disable=C0114,C0115,C0116',
|
| 206 |
'--output-format=text',
|
| 207 |
-
'--reports=n',
|
| 208 |
-
'-'
|
| 209 |
], exit=False, do_exit=False)
|
| 210 |
lint_message = lint_result.getvalue()
|
| 211 |
return formatted_code, lint_message
|
|
@@ -214,12 +223,7 @@ def code_editor_interface(code):
|
|
| 214 |
|
| 215 |
|
| 216 |
def translate_code(code, input_language, output_language):
|
| 217 |
-
"""
|
| 218 |
-
Translates code using the Hugging Face translation pipeline.
|
| 219 |
-
|
| 220 |
-
Note: This is a basic example and may not be suitable for all code translation tasks.
|
| 221 |
-
Consider using more specialized tools for complex code translation.
|
| 222 |
-
"""
|
| 223 |
try:
|
| 224 |
translator = pipeline("translation", model=f"{input_language}-to-{output_language}")
|
| 225 |
translated_code = translator(code, max_length=10000)[0]['translation_text']
|
|
@@ -230,13 +234,13 @@ def translate_code(code, input_language, output_language):
|
|
| 230 |
|
| 231 |
|
| 232 |
def generate_code(code_idea):
|
| 233 |
-
"""Generates code using
|
| 234 |
try:
|
| 235 |
-
generator = pipeline('text-generation', model='gpt2')
|
| 236 |
generated_code = generator(f"```python\n{code_idea}\n```", max_length=1000, num_return_sequences=1)[0][
|
| 237 |
'generated_text']
|
| 238 |
|
| 239 |
-
# Extract code from the generated text
|
| 240 |
start_index = generated_code.find("```python") + len("```python")
|
| 241 |
end_index = generated_code.find("```", start_index)
|
| 242 |
if start_index != -1 and end_index != -1:
|
|
@@ -249,7 +253,9 @@ def generate_code(code_idea):
|
|
| 249 |
|
| 250 |
|
| 251 |
def commit_and_push_changes(commit_message):
|
| 252 |
-
"""Commits and pushes changes
|
|
|
|
|
|
|
| 253 |
commands = [
|
| 254 |
"git add .",
|
| 255 |
f"git commit -m '{commit_message}'",
|
|
@@ -262,7 +268,7 @@ def commit_and_push_changes(commit_message):
|
|
| 262 |
break
|
| 263 |
|
| 264 |
|
| 265 |
-
# Streamlit App
|
| 266 |
st.title("AI Agent Creator")
|
| 267 |
|
| 268 |
# Sidebar navigation
|
|
@@ -270,10 +276,7 @@ st.sidebar.title("Navigation")
|
|
| 270 |
app_mode = st.sidebar.selectbox("Choose the app mode", ["AI Agent Creator", "Tool Box", "Workspace Chat App"])
|
| 271 |
|
| 272 |
if app_mode == "AI Agent Creator":
|
| 273 |
-
# AI Agent Creator
|
| 274 |
st.header("Create an AI Agent from Text")
|
| 275 |
-
|
| 276 |
-
st.subheader("From Text")
|
| 277 |
agent_name = st.text_input("Enter agent name:")
|
| 278 |
text_input = st.text_area("Enter skills (one per line):")
|
| 279 |
if st.button("Create Agent"):
|
|
@@ -282,23 +285,20 @@ if app_mode == "AI Agent Creator":
|
|
| 282 |
st.session_state.available_agents.append(agent_name)
|
| 283 |
|
| 284 |
elif app_mode == "Tool Box":
|
| 285 |
-
# Tool Box
|
| 286 |
st.header("AI-Powered Tools")
|
| 287 |
|
| 288 |
-
# Chat Interface
|
| 289 |
st.subheader("Chat with CodeCraft")
|
| 290 |
chat_input = st.text_area("Enter your message:")
|
| 291 |
if st.button("Send"):
|
| 292 |
if chat_input.startswith("@"):
|
| 293 |
-
agent_name = chat_input.split(" ")[0][1:]
|
| 294 |
-
chat_input = " ".join(chat_input.split(" ")[1:])
|
| 295 |
chat_response = chat_interface_with_agent(chat_input, agent_name)
|
| 296 |
else:
|
| 297 |
chat_response = chat_interface(chat_input)
|
| 298 |
st.session_state.chat_history.append((chat_input, chat_response))
|
| 299 |
st.write(f"CodeCraft: {chat_response}")
|
| 300 |
|
| 301 |
-
# Terminal Interface
|
| 302 |
st.subheader("Terminal")
|
| 303 |
terminal_input = st.text_input("Enter a command:")
|
| 304 |
if st.button("Run"):
|
|
@@ -306,7 +306,6 @@ elif app_mode == "Tool Box":
|
|
| 306 |
st.session_state.terminal_history.append((terminal_input, terminal_output))
|
| 307 |
st.code(terminal_output, language="bash")
|
| 308 |
|
| 309 |
-
# Code Editor Interface
|
| 310 |
st.subheader("Code Editor")
|
| 311 |
code_editor = st.text_area("Write your code:", height=300)
|
| 312 |
if st.button("Format & Lint"):
|
|
@@ -314,37 +313,32 @@ elif app_mode == "Tool Box":
|
|
| 314 |
st.code(formatted_code, language="python")
|
| 315 |
st.info(lint_message)
|
| 316 |
|
| 317 |
-
# Text Summarization Tool
|
| 318 |
st.subheader("Summarize Text")
|
| 319 |
text_to_summarize = st.text_area("Enter text to summarize:")
|
| 320 |
if st.button("Summarize"):
|
| 321 |
summary = summarize_text(text_to_summarize)
|
| 322 |
st.write(f"Summary: {summary}")
|
| 323 |
|
| 324 |
-
# Sentiment Analysis Tool
|
| 325 |
st.subheader("Sentiment Analysis")
|
| 326 |
sentiment_text = st.text_area("Enter text for sentiment analysis:")
|
| 327 |
if st.button("Analyze Sentiment"):
|
| 328 |
sentiment = sentiment_analysis(sentiment_text)
|
| 329 |
st.write(f"Sentiment: {sentiment}")
|
| 330 |
|
| 331 |
-
# Text Translation Tool (Code Translation)
|
| 332 |
st.subheader("Translate Code")
|
| 333 |
code_to_translate = st.text_area("Enter code to translate:")
|
| 334 |
-
source_language = st.selectbox("Source Language", ["en", "fr", "de", "es", "zh", "ja", "ko", "ru"])
|
| 335 |
-
target_language = st.selectbox("Target Language", ["en", "fr", "de", "es", "zh", "ja", "ko", "ru"])
|
| 336 |
if st.button("Translate Code"):
|
| 337 |
translated_code = translate_code(code_to_translate, source_language, target_language)
|
| 338 |
st.code(translated_code, language=target_language.lower())
|
| 339 |
|
| 340 |
-
# Code Generation
|
| 341 |
st.subheader("Code Generation")
|
| 342 |
code_idea = st.text_input("Enter your code idea:")
|
| 343 |
if st.button("Generate Code"):
|
| 344 |
generated_code = generate_code(code_idea)
|
| 345 |
st.code(generated_code, language="python")
|
| 346 |
|
| 347 |
-
# Display Preset Commands
|
| 348 |
st.subheader("Preset Commands")
|
| 349 |
preset_commands = {
|
| 350 |
"Create a new project": "create_project('project_name')",
|
|
@@ -359,17 +353,14 @@ elif app_mode == "Tool Box":
|
|
| 359 |
st.write(f"{command_name}: `{command}`")
|
| 360 |
|
| 361 |
elif app_mode == "Workspace Chat App":
|
| 362 |
-
# Workspace Chat App
|
| 363 |
st.header("Workspace Chat App")
|
| 364 |
|
| 365 |
-
# Project Workspace Creation
|
| 366 |
st.subheader("Create a New Project")
|
| 367 |
project_name = st.text_input("Enter project name:")
|
| 368 |
if st.button("Create Project"):
|
| 369 |
workspace_status = workspace_interface(project_name)
|
| 370 |
st.success(workspace_status)
|
| 371 |
|
| 372 |
-
# Add Code to Workspace
|
| 373 |
st.subheader("Add Code to Workspace")
|
| 374 |
code_to_add = st.text_area("Enter code to add to workspace:")
|
| 375 |
file_name = st.text_input("Enter file name (e.g. 'app.py'):")
|
|
@@ -377,14 +368,12 @@ elif app_mode == "Workspace Chat App":
|
|
| 377 |
add_code_status = add_code_to_workspace(project_name, code_to_add, file_name)
|
| 378 |
st.success(add_code_status)
|
| 379 |
|
| 380 |
-
# Terminal Interface with Project Context
|
| 381 |
st.subheader("Terminal (Workspace Context)")
|
| 382 |
terminal_input = st.text_input("Enter a command within the workspace:")
|
| 383 |
if st.button("Run Command"):
|
| 384 |
terminal_output = terminal_interface(terminal_input, project_name)
|
| 385 |
st.code(terminal_output, language="bash")
|
| 386 |
|
| 387 |
-
# Chat Interface for Guidance
|
| 388 |
st.subheader("Chat with CodeCraft for Guidance")
|
| 389 |
chat_input = st.text_area("Enter your message for guidance:")
|
| 390 |
if st.button("Get Guidance"):
|
|
@@ -392,26 +381,22 @@ elif app_mode == "Workspace Chat App":
|
|
| 392 |
st.session_state.chat_history.append((chat_input, chat_response))
|
| 393 |
st.write(f"CodeCraft: {chat_response}")
|
| 394 |
|
| 395 |
-
# Display Chat History
|
| 396 |
st.subheader("Chat History")
|
| 397 |
for user_input, response in st.session_state.chat_history:
|
| 398 |
st.write(f"User: {user_input}")
|
| 399 |
st.write(f"CodeCraft: {response}")
|
| 400 |
|
| 401 |
-
# Display Terminal History
|
| 402 |
st.subheader("Terminal History")
|
| 403 |
for command, output in st.session_state.terminal_history:
|
| 404 |
st.write(f"Command: {command}")
|
| 405 |
st.code(output, language="bash")
|
| 406 |
|
| 407 |
-
# Display Projects and Files
|
| 408 |
st.subheader("Workspace Projects")
|
| 409 |
for project, details in st.session_state.workspace_projects.items():
|
| 410 |
st.write(f"Project: {project}")
|
| 411 |
for file in details['files']:
|
| 412 |
st.write(f" - {file}")
|
| 413 |
|
| 414 |
-
# Chat with AI Agents
|
| 415 |
st.subheader("Chat with AI Agents")
|
| 416 |
selected_agent = st.selectbox("Select an AI agent", st.session_state.available_agents)
|
| 417 |
agent_chat_input = st.text_area("Enter your message for the agent:")
|
|
@@ -420,8 +405,14 @@ elif app_mode == "Workspace Chat App":
|
|
| 420 |
st.session_state.chat_history.append((agent_chat_input, agent_chat_response))
|
| 421 |
st.write(f"{selected_agent}: {agent_chat_response}")
|
| 422 |
|
| 423 |
-
# Automate Build Process
|
| 424 |
st.subheader("Automate Build Process")
|
| 425 |
if st.button("Automate"):
|
| 426 |
-
|
| 427 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import subprocess
|
| 2 |
import streamlit as st
|
| 3 |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
|
|
|
| 42 |
|
| 43 |
def autonomous_build(self, chat_history, workspace_projects):
|
| 44 |
"""
|
| 45 |
+
Autonomous build logic.
|
| 46 |
+
For now, it provides a simple summary and suggests the next step.
|
| 47 |
"""
|
| 48 |
summary = "Chat History:\n" + "\n".join([f"User: {u}\nAgent: {a}" for u, a in chat_history])
|
| 49 |
summary += "\n\nWorkspace Projects:\n" + "\n".join(
|
|
|
|
| 55 |
|
| 56 |
|
| 57 |
def save_agent_to_file(agent):
|
| 58 |
+
"""Saves the agent's information to files."""
|
| 59 |
if not os.path.exists(AGENT_DIRECTORY):
|
| 60 |
os.makedirs(AGENT_DIRECTORY)
|
| 61 |
file_path = os.path.join(AGENT_DIRECTORY, f"{agent.name}.txt")
|
|
|
|
| 66 |
file.write(f"Agent Name: {agent.name}\nDescription: {agent.description}")
|
| 67 |
st.session_state.available_agents.append(agent.name)
|
| 68 |
|
| 69 |
+
# (Optional) Commit and push if you have set up Hugging Face integration.
|
| 70 |
+
# commit_and_push_changes(f"Add agent {agent.name}")
|
| 71 |
|
| 72 |
|
| 73 |
def load_agent_prompt(agent_name):
|
|
|
|
| 82 |
|
| 83 |
|
| 84 |
def create_agent_from_text(name, text):
|
| 85 |
+
"""Creates an AI agent from the provided text input."""
|
| 86 |
skills = text.split('\n')
|
| 87 |
agent = AIAgent(name, "AI agent created from text input.", skills)
|
| 88 |
save_agent_to_file(agent)
|
|
|
|
| 95 |
if agent_prompt is None:
|
| 96 |
return f"Agent {agent_name} not found."
|
| 97 |
|
| 98 |
+
# Load the GPT-2 model
|
| 99 |
model_name = "gpt2"
|
| 100 |
try:
|
| 101 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
|
|
| 104 |
except EnvironmentError as e:
|
| 105 |
return f"Error loading model: {e}"
|
| 106 |
|
| 107 |
+
# Combine agent prompt and user input (truncate if necessary)
|
| 108 |
combined_input = f"{agent_prompt}\n\nUser: {input_text}\nAgent:"
|
| 109 |
+
max_input_length = 900
|
|
|
|
|
|
|
| 110 |
input_ids = tokenizer.encode(combined_input, return_tensors="pt")
|
| 111 |
if input_ids.shape[1] > max_input_length:
|
| 112 |
input_ids = input_ids[:, :max_input_length]
|
| 113 |
|
| 114 |
+
# Generate response
|
| 115 |
outputs = model.generate(
|
| 116 |
+
input_ids,
|
| 117 |
+
max_new_tokens=50,
|
| 118 |
+
num_return_sequences=1,
|
| 119 |
+
do_sample=True,
|
| 120 |
+
pad_token_id=tokenizer.eos_token_id
|
| 121 |
)
|
| 122 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 123 |
return response
|
|
|
|
| 125 |
|
| 126 |
# Basic chat interface (no agent)
|
| 127 |
def chat_interface(input_text):
|
| 128 |
+
# Load the GPT-2 model
|
| 129 |
model_name = "gpt2"
|
| 130 |
try:
|
| 131 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
|
|
| 134 |
except EnvironmentError as e:
|
| 135 |
return f"Error loading model: {e}"
|
| 136 |
|
| 137 |
+
# Generate response
|
| 138 |
outputs = generator(input_text, max_new_tokens=50, num_return_sequences=1, do_sample=True)
|
| 139 |
response = outputs[0]['generated_text']
|
| 140 |
return response
|
| 141 |
|
| 142 |
|
| 143 |
def workspace_interface(project_name):
|
| 144 |
+
"""Manages project creation."""
|
| 145 |
project_path = os.path.join(PROJECT_ROOT, project_name)
|
| 146 |
if not os.path.exists(PROJECT_ROOT):
|
| 147 |
os.makedirs(PROJECT_ROOT)
|
|
|
|
| 149 |
os.makedirs(project_path)
|
| 150 |
st.session_state.workspace_projects[project_name] = {"files": []}
|
| 151 |
st.session_state.current_state['workspace_chat']['project_name'] = project_name
|
| 152 |
+
# (Optional) Commit and push if you have set up Hugging Face integration.
|
| 153 |
+
# commit_and_push_changes(f"Create project {project_name}")
|
| 154 |
return f"Project {project_name} created successfully."
|
| 155 |
else:
|
| 156 |
return f"Project {project_name} already exists."
|
| 157 |
|
| 158 |
|
| 159 |
def add_code_to_workspace(project_name, code, file_name):
|
| 160 |
+
"""Adds code to a file in the specified project."""
|
| 161 |
project_path = os.path.join(PROJECT_ROOT, project_name)
|
| 162 |
if os.path.exists(project_path):
|
| 163 |
file_path = os.path.join(project_path, file_name)
|
|
|
|
| 165 |
file.write(code)
|
| 166 |
st.session_state.workspace_projects[project_name]["files"].append(file_name)
|
| 167 |
st.session_state.current_state['workspace_chat']['added_code'] = {"file_name": file_name, "code": code}
|
| 168 |
+
# (Optional) Commit and push if you have set up Hugging Face integration.
|
| 169 |
+
# commit_and_push_changes(f"Add code to {file_name} in project {project_name}")
|
| 170 |
return f"Code added to {file_name} in project {project_name} successfully."
|
| 171 |
else:
|
| 172 |
return f"Project {project_name} does not exist."
|
| 173 |
|
|
|
|
| 174 |
def terminal_interface(command, project_name=None):
|
| 175 |
+
"""Executes commands in the terminal, optionally within a project's directory."""
|
| 176 |
if project_name:
|
| 177 |
project_path = os.path.join(PROJECT_ROOT, project_name)
|
| 178 |
if not os.path.exists(project_path):
|
|
|
|
| 180 |
result = subprocess.run(command, cwd=project_path, shell=True, capture_output=True, text=True)
|
| 181 |
else:
|
| 182 |
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
| 183 |
+
|
| 184 |
if result.returncode == 0:
|
| 185 |
st.session_state.current_state['toolbox']['terminal_output'] = result.stdout
|
| 186 |
return result.stdout
|
|
|
|
| 190 |
|
| 191 |
|
| 192 |
def summarize_text(text):
|
| 193 |
+
"""Summarizes text using a Hugging Face pipeline."""
|
| 194 |
summarizer = pipeline("summarization")
|
| 195 |
summary = summarizer(text, max_length=100, min_length=25, do_sample=False)
|
| 196 |
st.session_state.current_state['toolbox']['summary'] = summary[0]['summary_text']
|
|
|
|
| 198 |
|
| 199 |
|
| 200 |
def sentiment_analysis(text):
|
| 201 |
+
"""Analyzes sentiment of text using a Hugging Face pipeline."""
|
| 202 |
analyzer = pipeline("sentiment-analysis")
|
| 203 |
sentiment = analyzer(text)
|
| 204 |
st.session_state.current_state['toolbox']['sentiment'] = sentiment[0]
|
|
|
|
| 206 |
|
| 207 |
|
| 208 |
def code_editor_interface(code):
|
| 209 |
+
"""Formats and lints Python code."""
|
| 210 |
try:
|
| 211 |
formatted_code = black.format_str(code, mode=black.FileMode())
|
| 212 |
lint_result = StringIO()
|
| 213 |
lint.Run([
|
| 214 |
+
'--disable=C0114,C0115,C0116',
|
| 215 |
'--output-format=text',
|
| 216 |
+
'--reports=n',
|
| 217 |
+
'-'
|
| 218 |
], exit=False, do_exit=False)
|
| 219 |
lint_message = lint_result.getvalue()
|
| 220 |
return formatted_code, lint_message
|
|
|
|
| 223 |
|
| 224 |
|
| 225 |
def translate_code(code, input_language, output_language):
|
| 226 |
+
"""Translates code between programming languages."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
try:
|
| 228 |
translator = pipeline("translation", model=f"{input_language}-to-{output_language}")
|
| 229 |
translated_code = translator(code, max_length=10000)[0]['translation_text']
|
|
|
|
| 234 |
|
| 235 |
|
| 236 |
def generate_code(code_idea):
|
| 237 |
+
"""Generates code from a user idea using a Hugging Face pipeline."""
|
| 238 |
try:
|
| 239 |
+
generator = pipeline('text-generation', model='gpt2')
|
| 240 |
generated_code = generator(f"```python\n{code_idea}\n```", max_length=1000, num_return_sequences=1)[0][
|
| 241 |
'generated_text']
|
| 242 |
|
| 243 |
+
# Extract code from the generated text
|
| 244 |
start_index = generated_code.find("```python") + len("```python")
|
| 245 |
end_index = generated_code.find("```", start_index)
|
| 246 |
if start_index != -1 and end_index != -1:
|
|
|
|
| 253 |
|
| 254 |
|
| 255 |
def commit_and_push_changes(commit_message):
|
| 256 |
+
"""(Optional) Commits and pushes changes.
|
| 257 |
+
Needs to be configured for your Hugging Face repository.
|
| 258 |
+
"""
|
| 259 |
commands = [
|
| 260 |
"git add .",
|
| 261 |
f"git commit -m '{commit_message}'",
|
|
|
|
| 268 |
break
|
| 269 |
|
| 270 |
|
| 271 |
+
# --- Streamlit App ---
|
| 272 |
st.title("AI Agent Creator")
|
| 273 |
|
| 274 |
# Sidebar navigation
|
|
|
|
| 276 |
app_mode = st.sidebar.selectbox("Choose the app mode", ["AI Agent Creator", "Tool Box", "Workspace Chat App"])
|
| 277 |
|
| 278 |
if app_mode == "AI Agent Creator":
|
|
|
|
| 279 |
st.header("Create an AI Agent from Text")
|
|
|
|
|
|
|
| 280 |
agent_name = st.text_input("Enter agent name:")
|
| 281 |
text_input = st.text_area("Enter skills (one per line):")
|
| 282 |
if st.button("Create Agent"):
|
|
|
|
| 285 |
st.session_state.available_agents.append(agent_name)
|
| 286 |
|
| 287 |
elif app_mode == "Tool Box":
|
|
|
|
| 288 |
st.header("AI-Powered Tools")
|
| 289 |
|
|
|
|
| 290 |
st.subheader("Chat with CodeCraft")
|
| 291 |
chat_input = st.text_area("Enter your message:")
|
| 292 |
if st.button("Send"):
|
| 293 |
if chat_input.startswith("@"):
|
| 294 |
+
agent_name = chat_input.split(" ")[0][1:]
|
| 295 |
+
chat_input = " ".join(chat_input.split(" ")[1:])
|
| 296 |
chat_response = chat_interface_with_agent(chat_input, agent_name)
|
| 297 |
else:
|
| 298 |
chat_response = chat_interface(chat_input)
|
| 299 |
st.session_state.chat_history.append((chat_input, chat_response))
|
| 300 |
st.write(f"CodeCraft: {chat_response}")
|
| 301 |
|
|
|
|
| 302 |
st.subheader("Terminal")
|
| 303 |
terminal_input = st.text_input("Enter a command:")
|
| 304 |
if st.button("Run"):
|
|
|
|
| 306 |
st.session_state.terminal_history.append((terminal_input, terminal_output))
|
| 307 |
st.code(terminal_output, language="bash")
|
| 308 |
|
|
|
|
| 309 |
st.subheader("Code Editor")
|
| 310 |
code_editor = st.text_area("Write your code:", height=300)
|
| 311 |
if st.button("Format & Lint"):
|
|
|
|
| 313 |
st.code(formatted_code, language="python")
|
| 314 |
st.info(lint_message)
|
| 315 |
|
|
|
|
| 316 |
st.subheader("Summarize Text")
|
| 317 |
text_to_summarize = st.text_area("Enter text to summarize:")
|
| 318 |
if st.button("Summarize"):
|
| 319 |
summary = summarize_text(text_to_summarize)
|
| 320 |
st.write(f"Summary: {summary}")
|
| 321 |
|
|
|
|
| 322 |
st.subheader("Sentiment Analysis")
|
| 323 |
sentiment_text = st.text_area("Enter text for sentiment analysis:")
|
| 324 |
if st.button("Analyze Sentiment"):
|
| 325 |
sentiment = sentiment_analysis(sentiment_text)
|
| 326 |
st.write(f"Sentiment: {sentiment}")
|
| 327 |
|
|
|
|
| 328 |
st.subheader("Translate Code")
|
| 329 |
code_to_translate = st.text_area("Enter code to translate:")
|
| 330 |
+
source_language = st.selectbox("Source Language", ["en", "fr", "de", "es", "zh", "ja", "ko", "ru"])
|
| 331 |
+
target_language = st.selectbox("Target Language", ["en", "fr", "de", "es", "zh", "ja", "ko", "ru"])
|
| 332 |
if st.button("Translate Code"):
|
| 333 |
translated_code = translate_code(code_to_translate, source_language, target_language)
|
| 334 |
st.code(translated_code, language=target_language.lower())
|
| 335 |
|
|
|
|
| 336 |
st.subheader("Code Generation")
|
| 337 |
code_idea = st.text_input("Enter your code idea:")
|
| 338 |
if st.button("Generate Code"):
|
| 339 |
generated_code = generate_code(code_idea)
|
| 340 |
st.code(generated_code, language="python")
|
| 341 |
|
|
|
|
| 342 |
st.subheader("Preset Commands")
|
| 343 |
preset_commands = {
|
| 344 |
"Create a new project": "create_project('project_name')",
|
|
|
|
| 353 |
st.write(f"{command_name}: `{command}`")
|
| 354 |
|
| 355 |
elif app_mode == "Workspace Chat App":
|
|
|
|
| 356 |
st.header("Workspace Chat App")
|
| 357 |
|
|
|
|
| 358 |
st.subheader("Create a New Project")
|
| 359 |
project_name = st.text_input("Enter project name:")
|
| 360 |
if st.button("Create Project"):
|
| 361 |
workspace_status = workspace_interface(project_name)
|
| 362 |
st.success(workspace_status)
|
| 363 |
|
|
|
|
| 364 |
st.subheader("Add Code to Workspace")
|
| 365 |
code_to_add = st.text_area("Enter code to add to workspace:")
|
| 366 |
file_name = st.text_input("Enter file name (e.g. 'app.py'):")
|
|
|
|
| 368 |
add_code_status = add_code_to_workspace(project_name, code_to_add, file_name)
|
| 369 |
st.success(add_code_status)
|
| 370 |
|
|
|
|
| 371 |
st.subheader("Terminal (Workspace Context)")
|
| 372 |
terminal_input = st.text_input("Enter a command within the workspace:")
|
| 373 |
if st.button("Run Command"):
|
| 374 |
terminal_output = terminal_interface(terminal_input, project_name)
|
| 375 |
st.code(terminal_output, language="bash")
|
| 376 |
|
|
|
|
| 377 |
st.subheader("Chat with CodeCraft for Guidance")
|
| 378 |
chat_input = st.text_area("Enter your message for guidance:")
|
| 379 |
if st.button("Get Guidance"):
|
|
|
|
| 381 |
st.session_state.chat_history.append((chat_input, chat_response))
|
| 382 |
st.write(f"CodeCraft: {chat_response}")
|
| 383 |
|
|
|
|
| 384 |
st.subheader("Chat History")
|
| 385 |
for user_input, response in st.session_state.chat_history:
|
| 386 |
st.write(f"User: {user_input}")
|
| 387 |
st.write(f"CodeCraft: {response}")
|
| 388 |
|
|
|
|
| 389 |
st.subheader("Terminal History")
|
| 390 |
for command, output in st.session_state.terminal_history:
|
| 391 |
st.write(f"Command: {command}")
|
| 392 |
st.code(output, language="bash")
|
| 393 |
|
|
|
|
| 394 |
st.subheader("Workspace Projects")
|
| 395 |
for project, details in st.session_state.workspace_projects.items():
|
| 396 |
st.write(f"Project: {project}")
|
| 397 |
for file in details['files']:
|
| 398 |
st.write(f" - {file}")
|
| 399 |
|
|
|
|
| 400 |
st.subheader("Chat with AI Agents")
|
| 401 |
selected_agent = st.selectbox("Select an AI agent", st.session_state.available_agents)
|
| 402 |
agent_chat_input = st.text_area("Enter your message for the agent:")
|
|
|
|
| 405 |
st.session_state.chat_history.append((agent_chat_input, agent_chat_response))
|
| 406 |
st.write(f"{selected_agent}: {agent_chat_response}")
|
| 407 |
|
|
|
|
| 408 |
st.subheader("Automate Build Process")
|
| 409 |
if st.button("Automate"):
|
| 410 |
+
if selected_agent:
|
| 411 |
+
agent = AIAgent(selected_agent, "", [])
|
| 412 |
+
summary, next_step = agent.autonomous_build(st.session_state.chat_history, st.session_state.workspace_projects)
|
| 413 |
+
st.write("Autonomous Build Summary:")
|
| 414 |
+
st.write(summary)
|
| 415 |
+
st.write("Next Step:")
|
| 416 |
+
st.write(next_step)
|
| 417 |
+
else:
|
| 418 |
+
st.warning("Please select an AI agent first.")
|