Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,31 +6,32 @@ import black
|
|
| 6 |
import streamlit as st
|
| 7 |
from pylint import lint
|
| 8 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
|
|
|
| 11 |
if 'chat_history' not in st.session_state:
|
| 12 |
st.session_state['chat_history'] = []
|
| 13 |
-
|
| 14 |
-
# Access and update chat_history
|
| 15 |
-
chat_history = st.session_state['chat_history']
|
| 16 |
-
chat_history.append("New message")
|
| 17 |
-
|
| 18 |
-
# Display chat history
|
| 19 |
-
st.write("Chat History:")
|
| 20 |
-
for message in chat_history:
|
| 21 |
-
st.write(message)
|
| 22 |
-
|
| 23 |
-
# Global state to manage communication between Tool Box and Workspace Chat App
|
| 24 |
if 'workspace_projects' not in st.session_state:
|
| 25 |
st.session_state.workspace_projects = {}
|
| 26 |
if 'available_agents' not in st.session_state:
|
| 27 |
st.session_state.available_agents = []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
|
|
|
| 29 |
class AIAgent:
|
| 30 |
-
def __init__(self, name, description, skills):
|
| 31 |
self.name = name
|
| 32 |
self.description = description
|
| 33 |
self.skills = skills
|
|
|
|
| 34 |
|
| 35 |
def create_agent_prompt(self):
|
| 36 |
skills_str = '\n'.join([f"* {skill}" for skill in self.skills])
|
|
@@ -40,6 +41,8 @@ My expertise lies in the following areas:
|
|
| 40 |
|
| 41 |
{skills_str}
|
| 42 |
|
|
|
|
|
|
|
| 43 |
I am here to help you build, deploy, and improve your applications.
|
| 44 |
Feel free to ask me any questions or present me with any challenges you encounter.
|
| 45 |
I will do my best to provide helpful and insightful responses.
|
|
@@ -59,6 +62,7 @@ I will do my best to provide helpful and insightful responses.
|
|
| 59 |
|
| 60 |
return summary, next_step
|
| 61 |
|
|
|
|
| 62 |
def save_agent_to_file(agent):
|
| 63 |
"""Saves the agent's prompt to a file."""
|
| 64 |
if not os.path.exists("agents"):
|
|
@@ -78,13 +82,33 @@ def load_agent_prompt(agent_name):
|
|
| 78 |
else:
|
| 79 |
return None
|
| 80 |
|
| 81 |
-
def create_agent_from_text(name, text):
|
| 82 |
skills = text.split('\n')
|
| 83 |
-
agent = AIAgent(name, "AI agent created from text input.", skills)
|
| 84 |
save_agent_to_file(agent)
|
| 85 |
return agent.create_agent_prompt()
|
| 86 |
|
| 87 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
def chat_interface_with_agent(input_text, agent_name):
|
| 89 |
agent_prompt = load_agent_prompt(agent_name)
|
| 90 |
if agent_prompt is None:
|
|
@@ -102,7 +126,7 @@ def chat_interface_with_agent(input_text, agent_name):
|
|
| 102 |
combined_input = f"{agent_prompt}\n\nUser: {input_text}\nAgent:"
|
| 103 |
|
| 104 |
# Truncate input text to avoid exceeding the model's maximum length
|
| 105 |
-
max_input_length =
|
| 106 |
input_ids = tokenizer.encode(combined_input, return_tensors="pt")
|
| 107 |
if input_ids.shape[1] > max_input_length:
|
| 108 |
input_ids = input_ids[:, :max_input_length]
|
|
@@ -111,21 +135,11 @@ def chat_interface_with_agent(input_text, agent_name):
|
|
| 111 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 112 |
return response
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
"""Handles user input in the chat interface.
|
| 119 |
-
|
| 120 |
-
Args:
|
| 121 |
-
input_text: User's input text.
|
| 122 |
-
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
Returns:
|
| 127 |
-
The chatbot's response.
|
| 128 |
-
"""
|
| 129 |
# Load the GPT-2 model which is compatible with AutoModelForCausalLM
|
| 130 |
model_name = "gpt2"
|
| 131 |
try:
|
|
@@ -134,57 +148,25 @@ def chat_interface(input_text):
|
|
| 134 |
except EnvironmentError as e:
|
| 135 |
return f"Error loading model: {e}"
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
# Truncate input text to avoid exceeding the model's maximum length
|
| 141 |
-
max_input_length =
|
| 142 |
-
input_ids = tokenizer.encode(
|
| 143 |
if input_ids.shape[1] > max_input_length:
|
| 144 |
input_ids = input_ids[:, :max_input_length]
|
| 145 |
|
| 146 |
-
outputs = model.generate(input_ids, max_length=
|
| 147 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 148 |
return response
|
| 149 |
|
| 150 |
-
|
| 151 |
-
# 2. Terminal
|
| 152 |
-
def terminal_interface(command, project_name=None):
|
| 153 |
-
"""Executes commands in the terminal.
|
| 154 |
-
|
| 155 |
-
Args:
|
| 156 |
-
command: User's command.
|
| 157 |
-
project_name: Name of the project workspace to add installed packages.
|
| 158 |
-
|
| 159 |
-
Returns:
|
| 160 |
-
The terminal output.
|
| 161 |
-
"""
|
| 162 |
-
# Execute command
|
| 163 |
-
try:
|
| 164 |
-
process = subprocess.run(command.split(), capture_output=True, text=True)
|
| 165 |
-
output = process.stdout
|
| 166 |
-
|
| 167 |
-
# If the command is to install a package, update the workspace
|
| 168 |
-
if "install" in command and project_name:
|
| 169 |
-
requirements_path = os.path.join("projects", project_name, "requirements.txt")
|
| 170 |
-
with open(requirements_path, "a") as req_file:
|
| 171 |
-
package_name = command.split()[-1]
|
| 172 |
-
req_file.write(f"{package_name}\n")
|
| 173 |
-
except Exception as e:
|
| 174 |
-
output = f"Error: {e}"
|
| 175 |
-
return output
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
# 3. Code Editor
|
| 179 |
def code_editor_interface(code):
|
| 180 |
-
"""Provides code completion, formatting, and linting in the code editor.
|
| 181 |
-
|
| 182 |
-
Args:
|
| 183 |
-
code: User's code.
|
| 184 |
-
|
| 185 |
-
Returns:
|
| 186 |
-
Formatted and linted code.
|
| 187 |
-
"""
|
| 188 |
# Format code using black
|
| 189 |
try:
|
| 190 |
formatted_code = black.format_str(code, mode=black.FileMode())
|
|
@@ -205,17 +187,9 @@ def code_editor_interface(code):
|
|
| 205 |
|
| 206 |
return formatted_code, lint_message
|
| 207 |
|
| 208 |
-
|
| 209 |
-
# 4. Workspace
|
| 210 |
def workspace_interface(project_name):
|
| 211 |
-
"""Manages projects, files, and resources in the workspace.
|
| 212 |
-
|
| 213 |
-
Args:
|
| 214 |
-
project_name: Name of the new project.
|
| 215 |
-
|
| 216 |
-
Returns:
|
| 217 |
-
Project creation status.
|
| 218 |
-
"""
|
| 219 |
project_path = os.path.join("projects", project_name)
|
| 220 |
# Create project directory
|
| 221 |
try:
|
|
@@ -230,16 +204,7 @@ def workspace_interface(project_name):
|
|
| 230 |
return status
|
| 231 |
|
| 232 |
def add_code_to_workspace(project_name, code, file_name):
|
| 233 |
-
"""Adds selected code files to the workspace.
|
| 234 |
-
|
| 235 |
-
Args:
|
| 236 |
-
project_name: Name of the project.
|
| 237 |
-
code: Code to be added.
|
| 238 |
-
file_name: Name of the file to be created.
|
| 239 |
-
|
| 240 |
-
Returns:
|
| 241 |
-
File creation status.
|
| 242 |
-
"""
|
| 243 |
project_path = os.path.join("projects", project_name)
|
| 244 |
file_path = os.path.join(project_path, file_name)
|
| 245 |
|
|
@@ -252,22 +217,9 @@ def add_code_to_workspace(project_name, code, file_name):
|
|
| 252 |
status = f"Error: {e}"
|
| 253 |
return status
|
| 254 |
|
| 255 |
-
|
| 256 |
-
# 5. AI-Infused Tools
|
| 257 |
-
|
| 258 |
-
# Define custom AI-powered tools using Hugging Face models
|
| 259 |
-
|
| 260 |
-
# Example: Text summarization tool
|
| 261 |
def summarize_text(text):
|
| 262 |
-
"""Summarizes a given text using a Hugging Face model.
|
| 263 |
-
|
| 264 |
-
Args:
|
| 265 |
-
text: Text to be summarized.
|
| 266 |
-
|
| 267 |
-
Returns:
|
| 268 |
-
Summarized text.
|
| 269 |
-
"""
|
| 270 |
-
# Load the summarization model
|
| 271 |
model_name = "facebook/bart-large-cnn"
|
| 272 |
try:
|
| 273 |
summarizer = pipeline("summarization", model=model_name)
|
|
@@ -275,7 +227,7 @@ def summarize_text(text):
|
|
| 275 |
return f"Error loading model: {e}"
|
| 276 |
|
| 277 |
# Truncate input text to avoid exceeding the model's maximum length
|
| 278 |
-
max_input_length =
|
| 279 |
inputs = text
|
| 280 |
if len(text) > max_input_length:
|
| 281 |
inputs = text[:max_input_length]
|
|
@@ -286,17 +238,8 @@ def summarize_text(text):
|
|
| 286 |
]
|
| 287 |
return summary
|
| 288 |
|
| 289 |
-
# Example: Sentiment analysis tool
|
| 290 |
def sentiment_analysis(text):
|
| 291 |
-
"""Performs sentiment analysis on a given text using a Hugging Face model.
|
| 292 |
-
|
| 293 |
-
Args:
|
| 294 |
-
text: Text to be analyzed.
|
| 295 |
-
|
| 296 |
-
Returns:
|
| 297 |
-
Sentiment analysis result.
|
| 298 |
-
"""
|
| 299 |
-
# Load the sentiment analysis model
|
| 300 |
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
|
| 301 |
try:
|
| 302 |
analyzer = pipeline("sentiment-analysis", model=model_name)
|
|
@@ -307,18 +250,8 @@ def sentiment_analysis(text):
|
|
| 307 |
result = analyzer(text)[0]
|
| 308 |
return result
|
| 309 |
|
| 310 |
-
# Example: Text translation tool (code translation)
|
| 311 |
def translate_code(code, source_language, target_language):
|
| 312 |
-
"""Translates code from one programming language to another using OpenAI Codex.
|
| 313 |
-
|
| 314 |
-
Args:
|
| 315 |
-
code: Code to be translated.
|
| 316 |
-
source_language: The source programming language.
|
| 317 |
-
target_language: The target programming language.
|
| 318 |
-
|
| 319 |
-
Returns:
|
| 320 |
-
Translated code.
|
| 321 |
-
"""
|
| 322 |
# You might want to replace this with a Hugging Face translation model
|
| 323 |
# for example, "Helsinki-NLP/opus-mt-en-fr"
|
| 324 |
# Refer to Hugging Face documentation for model usage.
|
|
@@ -331,17 +264,8 @@ def translate_code(code, source_language, target_language):
|
|
| 331 |
translated_code = f"Error: {e}"
|
| 332 |
return translated_code
|
| 333 |
|
| 334 |
-
|
| 335 |
-
# 6. Code Generation
|
| 336 |
def generate_code(idea):
|
| 337 |
-
"""Generates code based on a given idea using the EleutherAI/gpt-neo-2.7B model.
|
| 338 |
-
Args:
|
| 339 |
-
idea: The idea for the code to be generated.
|
| 340 |
-
Returns:
|
| 341 |
-
The generated code as a string.
|
| 342 |
-
"""
|
| 343 |
-
|
| 344 |
-
# Load the code generation model
|
| 345 |
model_name = "EleutherAI/gpt-neo-2.7B"
|
| 346 |
try:
|
| 347 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
@@ -357,7 +281,7 @@ def generate_code(idea):
|
|
| 357 |
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
| 358 |
output_sequences = model.generate(
|
| 359 |
input_ids=input_ids,
|
| 360 |
-
max_length=max_length,
|
| 361 |
num_return_sequences=1,
|
| 362 |
no_repeat_ngram_size=2,
|
| 363 |
early_stopping=True,
|
|
@@ -375,17 +299,9 @@ def generate_code(idea):
|
|
| 375 |
|
| 376 |
return generated_code
|
| 377 |
|
| 378 |
-
|
| 379 |
-
# 7. AI Personas Creator
|
| 380 |
def create_persona_from_text(text):
|
| 381 |
-
"""Creates an AI persona from the given text.
|
| 382 |
-
|
| 383 |
-
Args:
|
| 384 |
-
text: Text to be used for creating the persona.
|
| 385 |
-
|
| 386 |
-
Returns:
|
| 387 |
-
Persona prompt.
|
| 388 |
-
"""
|
| 389 |
persona_prompt = f"""
|
| 390 |
As an elite expert developer with the highest level of proficiency in Streamlit, Gradio, and Hugging Face, I possess a comprehensive understanding of these technologies and their applications in web development and deployment. My expertise encompasses the following areas:
|
| 391 |
|
|
@@ -445,48 +361,267 @@ st.write(demo)
|
|
| 445 |
"""
|
| 446 |
return persona_prompt
|
| 447 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 448 |
|
| 449 |
-
# Streamlit App
|
| 450 |
st.title("AI Agent Creator")
|
| 451 |
|
| 452 |
-
# Sidebar
|
| 453 |
st.sidebar.title("Navigation")
|
| 454 |
app_mode = st.sidebar.selectbox("Choose the app mode", ["AI Agent Creator", "Tool Box", "Workspace Chat App"])
|
| 455 |
|
|
|
|
| 456 |
if app_mode == "AI Agent Creator":
|
| 457 |
-
# AI Agent Creator
|
| 458 |
st.header("Create an AI Agent from Text")
|
| 459 |
|
| 460 |
st.subheader("From Text")
|
| 461 |
agent_name = st.text_input("Enter agent name:")
|
| 462 |
text_input = st.text_area("Enter skills (one per line):")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 463 |
if st.button("Create Agent"):
|
| 464 |
-
agent_prompt = create_agent_from_text(agent_name, text_input)
|
| 465 |
st.success(f"Agent '{agent_name}' created and saved successfully.")
|
| 466 |
st.session_state.available_agents.append(agent_name)
|
| 467 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 468 |
elif app_mode == "Tool Box":
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 473 |
st.write(f" - {file}")
|
| 474 |
|
| 475 |
-
# Chat with AI Agents
|
| 476 |
st.subheader("Chat with AI Agents")
|
| 477 |
-
|
| 478 |
-
agent_chat_input = st.text_area("Enter your message
|
| 479 |
-
if st.button("Send
|
| 480 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 481 |
st.session_state.chat_history.append((agent_chat_input, agent_chat_response))
|
| 482 |
-
st.write(f"{
|
| 483 |
|
| 484 |
-
# Automate Build Process
|
| 485 |
st.subheader("Automate Build Process")
|
| 486 |
if st.button("Automate"):
|
| 487 |
-
agent = AIAgent(
|
| 488 |
summary, next_step = agent.autonomous_build(st.session_state.chat_history, st.session_state.workspace_projects)
|
| 489 |
st.write("Autonomous Build Summary:")
|
| 490 |
st.write(summary)
|
| 491 |
st.write("Next Step:")
|
| 492 |
-
st.write(next_step)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import streamlit as st
|
| 7 |
from pylint import lint
|
| 8 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 9 |
+
from transformers import pipeline as transformers_pipeline
|
| 10 |
+
from huggingface_hub import hf_hub_url, cached_download
|
| 11 |
+
import json
|
| 12 |
+
import time
|
| 13 |
+
import shutil
|
| 14 |
+
import gradio as gr
|
| 15 |
|
| 16 |
+
# --- Global State ---
|
| 17 |
if 'chat_history' not in st.session_state:
|
| 18 |
st.session_state['chat_history'] = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
if 'workspace_projects' not in st.session_state:
|
| 20 |
st.session_state.workspace_projects = {}
|
| 21 |
if 'available_agents' not in st.session_state:
|
| 22 |
st.session_state.available_agents = []
|
| 23 |
+
if 'available_clusters' not in st.session_state:
|
| 24 |
+
st.session_state.available_clusters = []
|
| 25 |
+
if 'current_project' not in st.session_state:
|
| 26 |
+
st.session_state.current_project = None
|
| 27 |
|
| 28 |
+
# --- Agent Class ---
|
| 29 |
class AIAgent:
|
| 30 |
+
def __init__(self, name, description, skills, persona_prompt=None):
|
| 31 |
self.name = name
|
| 32 |
self.description = description
|
| 33 |
self.skills = skills
|
| 34 |
+
self.persona_prompt = persona_prompt
|
| 35 |
|
| 36 |
def create_agent_prompt(self):
|
| 37 |
skills_str = '\n'.join([f"* {skill}" for skill in self.skills])
|
|
|
|
| 41 |
|
| 42 |
{skills_str}
|
| 43 |
|
| 44 |
+
{self.persona_prompt if self.persona_prompt else ''}
|
| 45 |
+
|
| 46 |
I am here to help you build, deploy, and improve your applications.
|
| 47 |
Feel free to ask me any questions or present me with any challenges you encounter.
|
| 48 |
I will do my best to provide helpful and insightful responses.
|
|
|
|
| 62 |
|
| 63 |
return summary, next_step
|
| 64 |
|
| 65 |
+
# --- Agent Management ---
|
| 66 |
def save_agent_to_file(agent):
|
| 67 |
"""Saves the agent's prompt to a file."""
|
| 68 |
if not os.path.exists("agents"):
|
|
|
|
| 82 |
else:
|
| 83 |
return None
|
| 84 |
|
| 85 |
+
def create_agent_from_text(name, text, persona_prompt=None):
|
| 86 |
skills = text.split('\n')
|
| 87 |
+
agent = AIAgent(name, "AI agent created from text input.", skills, persona_prompt)
|
| 88 |
save_agent_to_file(agent)
|
| 89 |
return agent.create_agent_prompt()
|
| 90 |
|
| 91 |
+
# --- Cluster Management ---
|
| 92 |
+
def create_agent_cluster(cluster_name, agent_names):
|
| 93 |
+
"""Creates a cluster of agents."""
|
| 94 |
+
if not os.path.exists("clusters"):
|
| 95 |
+
os.makedirs("clusters")
|
| 96 |
+
cluster_path = os.path.join("clusters", f"{cluster_name}.json")
|
| 97 |
+
with open(cluster_path, "w") as file:
|
| 98 |
+
json.dump({"agents": agent_names}, file)
|
| 99 |
+
st.session_state.available_clusters.append(cluster_name)
|
| 100 |
+
|
| 101 |
+
def load_agent_cluster(cluster_name):
|
| 102 |
+
"""Loads an agent cluster from a file."""
|
| 103 |
+
cluster_path = os.path.join("clusters", f"{cluster_name}.json")
|
| 104 |
+
if os.path.exists(cluster_path):
|
| 105 |
+
with open(cluster_path, "r") as file:
|
| 106 |
+
cluster_data = json.load(file)
|
| 107 |
+
return cluster_data["agents"]
|
| 108 |
+
else:
|
| 109 |
+
return None
|
| 110 |
+
|
| 111 |
+
# --- Chat Interface ---
|
| 112 |
def chat_interface_with_agent(input_text, agent_name):
|
| 113 |
agent_prompt = load_agent_prompt(agent_name)
|
| 114 |
if agent_prompt is None:
|
|
|
|
| 126 |
combined_input = f"{agent_prompt}\n\nUser: {input_text}\nAgent:"
|
| 127 |
|
| 128 |
# Truncate input text to avoid exceeding the model's maximum length
|
| 129 |
+
max_input_length = model.config.max_length
|
| 130 |
input_ids = tokenizer.encode(combined_input, return_tensors="pt")
|
| 131 |
if input_ids.shape[1] > max_input_length:
|
| 132 |
input_ids = input_ids[:, :max_input_length]
|
|
|
|
| 135 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 136 |
return response
|
| 137 |
|
| 138 |
+
def chat_interface_with_cluster(input_text, cluster_name):
|
| 139 |
+
agent_names = load_agent_cluster(cluster_name)
|
| 140 |
+
if agent_names is None:
|
| 141 |
+
return f"Cluster {cluster_name} not found."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
# Load the GPT-2 model which is compatible with AutoModelForCausalLM
|
| 144 |
model_name = "gpt2"
|
| 145 |
try:
|
|
|
|
| 148 |
except EnvironmentError as e:
|
| 149 |
return f"Error loading model: {e}"
|
| 150 |
|
| 151 |
+
# Combine the agent prompt with user input
|
| 152 |
+
combined_input = f"User: {input_text}\n"
|
| 153 |
+
for agent_name in agent_names:
|
| 154 |
+
agent_prompt = load_agent_prompt(agent_name)
|
| 155 |
+
combined_input += f"\n{agent_name}:\n{agent_prompt}\n"
|
| 156 |
|
| 157 |
# Truncate input text to avoid exceeding the model's maximum length
|
| 158 |
+
max_input_length = model.config.max_length
|
| 159 |
+
input_ids = tokenizer.encode(combined_input, return_tensors="pt")
|
| 160 |
if input_ids.shape[1] > max_input_length:
|
| 161 |
input_ids = input_ids[:, :max_input_length]
|
| 162 |
|
| 163 |
+
outputs = model.generate(input_ids, max_length=max_input_length, do_sample=True)
|
| 164 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 165 |
return response
|
| 166 |
|
| 167 |
+
# --- Code Editor ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
def code_editor_interface(code):
|
| 169 |
+
"""Provides code completion, formatting, and linting in the code editor."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
# Format code using black
|
| 171 |
try:
|
| 172 |
formatted_code = black.format_str(code, mode=black.FileMode())
|
|
|
|
| 187 |
|
| 188 |
return formatted_code, lint_message
|
| 189 |
|
| 190 |
+
# --- Workspace Management ---
|
|
|
|
| 191 |
def workspace_interface(project_name):
|
| 192 |
+
"""Manages projects, files, and resources in the workspace."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
project_path = os.path.join("projects", project_name)
|
| 194 |
# Create project directory
|
| 195 |
try:
|
|
|
|
| 204 |
return status
|
| 205 |
|
| 206 |
def add_code_to_workspace(project_name, code, file_name):
|
| 207 |
+
"""Adds selected code files to the workspace."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
project_path = os.path.join("projects", project_name)
|
| 209 |
file_path = os.path.join(project_path, file_name)
|
| 210 |
|
|
|
|
| 217 |
status = f"Error: {e}"
|
| 218 |
return status
|
| 219 |
|
| 220 |
+
# --- AI Tools ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
def summarize_text(text):
|
| 222 |
+
"""Summarizes a given text using a Hugging Face model."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
model_name = "facebook/bart-large-cnn"
|
| 224 |
try:
|
| 225 |
summarizer = pipeline("summarization", model=model_name)
|
|
|
|
| 227 |
return f"Error loading model: {e}"
|
| 228 |
|
| 229 |
# Truncate input text to avoid exceeding the model's maximum length
|
| 230 |
+
max_input_length = model.config.max_length
|
| 231 |
inputs = text
|
| 232 |
if len(text) > max_input_length:
|
| 233 |
inputs = text[:max_input_length]
|
|
|
|
| 238 |
]
|
| 239 |
return summary
|
| 240 |
|
|
|
|
| 241 |
def sentiment_analysis(text):
|
| 242 |
+
"""Performs sentiment analysis on a given text using a Hugging Face model."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
|
| 244 |
try:
|
| 245 |
analyzer = pipeline("sentiment-analysis", model=model_name)
|
|
|
|
| 250 |
result = analyzer(text)[0]
|
| 251 |
return result
|
| 252 |
|
|
|
|
| 253 |
def translate_code(code, source_language, target_language):
|
| 254 |
+
"""Translates code from one programming language to another using OpenAI Codex."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
# You might want to replace this with a Hugging Face translation model
|
| 256 |
# for example, "Helsinki-NLP/opus-mt-en-fr"
|
| 257 |
# Refer to Hugging Face documentation for model usage.
|
|
|
|
| 264 |
translated_code = f"Error: {e}"
|
| 265 |
return translated_code
|
| 266 |
|
|
|
|
|
|
|
| 267 |
def generate_code(idea):
|
| 268 |
+
"""Generates code based on a given idea using the EleutherAI/gpt-neo-2.7B model."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
model_name = "EleutherAI/gpt-neo-2.7B"
|
| 270 |
try:
|
| 271 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
|
|
| 281 |
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
| 282 |
output_sequences = model.generate(
|
| 283 |
input_ids=input_ids,
|
| 284 |
+
max_length=model.config.max_length,
|
| 285 |
num_return_sequences=1,
|
| 286 |
no_repeat_ngram_size=2,
|
| 287 |
early_stopping=True,
|
|
|
|
| 299 |
|
| 300 |
return generated_code
|
| 301 |
|
| 302 |
+
# --- AI Personas Creator ---
|
|
|
|
| 303 |
def create_persona_from_text(text):
|
| 304 |
+
"""Creates an AI persona from the given text."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
persona_prompt = f"""
|
| 306 |
As an elite expert developer with the highest level of proficiency in Streamlit, Gradio, and Hugging Face, I possess a comprehensive understanding of these technologies and their applications in web development and deployment. My expertise encompasses the following areas:
|
| 307 |
|
|
|
|
| 361 |
"""
|
| 362 |
return persona_prompt
|
| 363 |
|
| 364 |
+
# --- Terminal Interface ---
|
| 365 |
+
def terminal_interface(command, project_name=None):
|
| 366 |
+
"""Executes commands in the terminal."""
|
| 367 |
+
# Execute command
|
| 368 |
+
try:
|
| 369 |
+
process = subprocess.run(command.split(), capture_output=True, text=True)
|
| 370 |
+
output = process.stdout
|
| 371 |
+
|
| 372 |
+
# If the command is to install a package, update the workspace
|
| 373 |
+
if "install" in command and project_name:
|
| 374 |
+
requirements_path = os.path.join("projects", project_name, "requirements.txt")
|
| 375 |
+
with open(requirements_path, "a") as req_file:
|
| 376 |
+
package_name = command.split()[-1]
|
| 377 |
+
req_file.write(f"{package_name}\n")
|
| 378 |
+
except Exception as e:
|
| 379 |
+
output = f"Error: {e}"
|
| 380 |
+
return output
|
| 381 |
+
|
| 382 |
+
# --- Build and Deploy ---
|
| 383 |
+
def build_project(project_name):
|
| 384 |
+
"""Builds a project based on the workspace files."""
|
| 385 |
+
project_path = os.path.join("projects", project_name)
|
| 386 |
+
requirements_path = os.path.join(project_path, "requirements.txt")
|
| 387 |
+
|
| 388 |
+
# Install dependencies
|
| 389 |
+
os.chdir(project_path)
|
| 390 |
+
terminal_interface(f"pip install -r {requirements_path}")
|
| 391 |
+
os.chdir("..")
|
| 392 |
+
|
| 393 |
+
# Create a temporary directory for the built project
|
| 394 |
+
build_dir = os.path.join("build", project_name)
|
| 395 |
+
os.makedirs(build_dir, exist_ok=True)
|
| 396 |
+
|
| 397 |
+
# Copy project files to the build directory
|
| 398 |
+
for filename in os.listdir(project_path):
|
| 399 |
+
if filename == "requirements.txt":
|
| 400 |
+
continue
|
| 401 |
+
shutil.copy(os.path.join(project_path, filename), build_dir)
|
| 402 |
+
|
| 403 |
+
# Create a `main.py` file if it doesn't exist
|
| 404 |
+
main_file = os.path.join(build_dir, "main.py")
|
| 405 |
+
if not os.path.exists(main_file):
|
| 406 |
+
with open(main_file, "w") as f:
|
| 407 |
+
f.write("# Your Streamlit app code goes here\n")
|
| 408 |
+
|
| 409 |
+
# Return the path to the built project
|
| 410 |
+
return build_dir
|
| 411 |
+
|
| 412 |
+
def deploy_to_huggingface(build_dir, hf_token, repo_name):
|
| 413 |
+
"""Deploys the built project to Hugging Face Spaces."""
|
| 414 |
+
# Authenticate with Hugging Face
|
| 415 |
+
os.environ["HF_TOKEN"] = hf_token
|
| 416 |
+
|
| 417 |
+
# Create a new Hugging Face Space repository
|
| 418 |
+
try:
|
| 419 |
+
subprocess.run(f"huggingface-cli repo create {repo_name}", shell=True, check=True)
|
| 420 |
+
except subprocess.CalledProcessError as e:
|
| 421 |
+
st.error(f"Error creating Hugging Face Space repository: {e}")
|
| 422 |
+
return
|
| 423 |
+
|
| 424 |
+
# Upload the built project to the repository
|
| 425 |
+
try:
|
| 426 |
+
subprocess.run(f"huggingface-cli upload {repo_name} {build_dir}", shell=True, check=True)
|
| 427 |
+
except subprocess.CalledProcessError as e:
|
| 428 |
+
st.error(f"Error uploading project to Hugging Face Space repository: {e}")
|
| 429 |
+
return
|
| 430 |
+
|
| 431 |
+
# Deploy the project to Hugging Face Spaces
|
| 432 |
+
try:
|
| 433 |
+
subprocess.run(f"huggingface-cli space deploy {repo_name}", shell=True, check=True)
|
| 434 |
+
except subprocess.CalledProcessError as e:
|
| 435 |
+
st.error(f"Error deploying project to Hugging Face Spaces: {e}")
|
| 436 |
+
return
|
| 437 |
+
|
| 438 |
+
# Display the deployment URL
|
| 439 |
+
st.success(f"Project deployed successfully to Hugging Face Spaces: https://huggingface.co/spaces/{repo_name}")
|
| 440 |
+
|
| 441 |
+
def deploy_locally(build_dir):
|
| 442 |
+
"""Deploys the built project locally."""
|
| 443 |
+
# Run the project locally
|
| 444 |
+
os.chdir(build_dir)
|
| 445 |
+
subprocess.run("streamlit run main.py", shell=True, check=True)
|
| 446 |
+
os.chdir("..")
|
| 447 |
+
|
| 448 |
+
# Display a success message
|
| 449 |
+
st.success(f"Project deployed locally!")
|
| 450 |
|
| 451 |
+
# --- Streamlit App ---
|
| 452 |
st.title("AI Agent Creator")
|
| 453 |
|
| 454 |
+
# --- Sidebar Navigation ---
|
| 455 |
st.sidebar.title("Navigation")
|
| 456 |
app_mode = st.sidebar.selectbox("Choose the app mode", ["AI Agent Creator", "Tool Box", "Workspace Chat App"])
|
| 457 |
|
| 458 |
+
# --- AI Agent Creator ---
|
| 459 |
if app_mode == "AI Agent Creator":
|
|
|
|
| 460 |
st.header("Create an AI Agent from Text")
|
| 461 |
|
| 462 |
st.subheader("From Text")
|
| 463 |
agent_name = st.text_input("Enter agent name:")
|
| 464 |
text_input = st.text_area("Enter skills (one per line):")
|
| 465 |
+
persona_prompt_option = st.selectbox("Choose a persona prompt", ["None", "Expert Developer"])
|
| 466 |
+
persona_prompt = None
|
| 467 |
+
if persona_prompt_option == "Expert Developer":
|
| 468 |
+
persona_prompt = create_persona_from_text("Expert Developer")
|
| 469 |
if st.button("Create Agent"):
|
| 470 |
+
agent_prompt = create_agent_from_text(agent_name, text_input, persona_prompt)
|
| 471 |
st.success(f"Agent '{agent_name}' created and saved successfully.")
|
| 472 |
st.session_state.available_agents.append(agent_name)
|
| 473 |
|
| 474 |
+
st.subheader("Create an Agent Cluster")
|
| 475 |
+
cluster_name = st.text_input("Enter cluster name:")
|
| 476 |
+
agent_names = st.multiselect("Select agents for the cluster", st.session_state.available_agents)
|
| 477 |
+
if st.button("Create Cluster"):
|
| 478 |
+
create_agent_cluster(cluster_name, agent_names)
|
| 479 |
+
st.success(f"Cluster '{cluster_name}' created successfully.")
|
| 480 |
+
st.session_state.available_clusters.append(cluster_name)
|
| 481 |
+
|
| 482 |
+
# --- Tool Box ---
|
| 483 |
elif app_mode == "Tool Box":
|
| 484 |
+
st.header("Tool Box")
|
| 485 |
+
|
| 486 |
+
# --- Workspace ---
|
| 487 |
+
st.subheader("Workspace")
|
| 488 |
+
project_name = st.selectbox("Select a project", list(st.session_state.workspace_projects.keys()), key="project_select")
|
| 489 |
+
if project_name:
|
| 490 |
+
st.session_state.current_project = project_name
|
| 491 |
+
for file in st.session_state.workspace_projects[project_name]['files']:
|
| 492 |
st.write(f" - {file}")
|
| 493 |
|
| 494 |
+
# --- Chat with AI Agents ---
|
| 495 |
st.subheader("Chat with AI Agents")
|
| 496 |
+
selected_agent_or_cluster = st.selectbox("Select an AI agent or cluster", st.session_state.available_agents + st.session_state.available_clusters)
|
| 497 |
+
agent_chat_input = st.text_area("Enter your message:")
|
| 498 |
+
if st.button("Send"):
|
| 499 |
+
if selected_agent_or_cluster in st.session_state.available_agents:
|
| 500 |
+
agent_chat_response = chat_interface_with_agent(agent_chat_input, selected_agent_or_cluster)
|
| 501 |
+
elif selected_agent_or_cluster in st.session_state.available_clusters:
|
| 502 |
+
agent_chat_response = chat_interface_with_cluster(agent_chat_input, selected_agent_or_cluster)
|
| 503 |
+
else:
|
| 504 |
+
agent_chat_response = "Invalid selection."
|
| 505 |
st.session_state.chat_history.append((agent_chat_input, agent_chat_response))
|
| 506 |
+
st.write(f"{selected_agent_or_cluster}: {agent_chat_response}")
|
| 507 |
|
| 508 |
+
# --- Automate Build Process ---
|
| 509 |
st.subheader("Automate Build Process")
|
| 510 |
if st.button("Automate"):
|
| 511 |
+
agent = AIAgent(selected_agent_or_cluster, "", []) # Load the agent without skills for now
|
| 512 |
summary, next_step = agent.autonomous_build(st.session_state.chat_history, st.session_state.workspace_projects)
|
| 513 |
st.write("Autonomous Build Summary:")
|
| 514 |
st.write(summary)
|
| 515 |
st.write("Next Step:")
|
| 516 |
+
st.write(next_step)
|
| 517 |
+
|
| 518 |
+
# --- Workspace Chat App ---
|
| 519 |
+
elif app_mode == "Workspace Chat App":
|
| 520 |
+
st.header("Workspace Chat App")
|
| 521 |
+
|
| 522 |
+
# --- Project Selection ---
|
| 523 |
+
project_name = st.selectbox("Select a project", list(st.session_state.workspace_projects.keys()), key="project_select")
|
| 524 |
+
if project_name:
|
| 525 |
+
st.session_state.current_project = project_name
|
| 526 |
+
|
| 527 |
+
# --- Chat with AI Agents ---
|
| 528 |
+
st.subheader("Chat with AI Agents")
|
| 529 |
+
selected_agent_or_cluster = st.selectbox("Select an AI agent or cluster", st.session_state.available_agents + st.session_state.available_clusters)
|
| 530 |
+
agent_chat_input = st.text_area("Enter your message:")
|
| 531 |
+
if st.button("Send"):
|
| 532 |
+
if selected_agent_or_cluster in st.session_state.available_agents:
|
| 533 |
+
agent_chat_response = chat_interface_with_agent(agent_chat_input, selected_agent_or_cluster)
|
| 534 |
+
elif selected_agent_or_cluster in st.session_state.available_clusters:
|
| 535 |
+
agent_chat_response = chat_interface_with_cluster(agent_chat_input, selected_agent_or_cluster)
|
| 536 |
+
else:
|
| 537 |
+
agent_chat_response = "Invalid selection."
|
| 538 |
+
st.session_state.chat_history.append((agent_chat_input, agent_chat_response))
|
| 539 |
+
st.write(f"{selected_agent_or_cluster}: {agent_chat_response}")
|
| 540 |
+
|
| 541 |
+
# --- Code Editor ---
|
| 542 |
+
st.subheader("Code Editor")
|
| 543 |
+
code = st.text_area("Enter your code:")
|
| 544 |
+
if st.button("Format & Lint"):
|
| 545 |
+
formatted_code, lint_message = code_editor_interface(code)
|
| 546 |
+
st.code(formatted_code, language="python")
|
| 547 |
+
st.write("Linting Report:")
|
| 548 |
+
st.write(lint_message)
|
| 549 |
+
|
| 550 |
+
# --- Add Code to Workspace ---
|
| 551 |
+
st.subheader("Add Code to Workspace")
|
| 552 |
+
file_name = st.text_input("Enter file name:")
|
| 553 |
+
if st.button("Add Code"):
|
| 554 |
+
if st.session_state.current_project:
|
| 555 |
+
status = add_code_to_workspace(st.session_state.current_project, code, file_name)
|
| 556 |
+
st.write(status)
|
| 557 |
+
else:
|
| 558 |
+
st.warning("Please select a project first.")
|
| 559 |
+
|
| 560 |
+
# --- Terminal ---
|
| 561 |
+
st.subheader("Terminal")
|
| 562 |
+
command = st.text_input("Enter a command:")
|
| 563 |
+
if st.button("Execute"):
|
| 564 |
+
if st.session_state.current_project:
|
| 565 |
+
output = terminal_interface(command, st.session_state.current_project)
|
| 566 |
+
st.write(output)
|
| 567 |
+
else:
|
| 568 |
+
st.warning("Please select a project first.")
|
| 569 |
+
|
| 570 |
+
# --- AI Tools ---
|
| 571 |
+
st.subheader("AI Tools")
|
| 572 |
+
st.write("Summarize Text:")
|
| 573 |
+
text_to_summarize = st.text_area("Enter text to summarize:")
|
| 574 |
+
if st.button("Summarize"):
|
| 575 |
+
summary = summarize_text(text_to_summarize)
|
| 576 |
+
st.write(summary)
|
| 577 |
+
|
| 578 |
+
st.write("Sentiment Analysis:")
|
| 579 |
+
text_to_analyze = st.text_area("Enter text to analyze:")
|
| 580 |
+
if st.button("Analyze"):
|
| 581 |
+
result = sentiment_analysis(text_to_analyze)
|
| 582 |
+
st.write(result)
|
| 583 |
+
|
| 584 |
+
st.write("Code Translation:")
|
| 585 |
+
code_to_translate = st.text_area("Enter code to translate:")
|
| 586 |
+
source_language = st.selectbox("Source Language", ["Python", "JavaScript", "C++"])
|
| 587 |
+
target_language = st.selectbox("Target Language", ["Python", "JavaScript", "C++"])
|
| 588 |
+
if st.button("Translate"):
|
| 589 |
+
translated_code = translate_code(code_to_translate, source_language, target_language)
|
| 590 |
+
st.write(translated_code)
|
| 591 |
+
|
| 592 |
+
st.write("Code Generation:")
|
| 593 |
+
code_idea = st.text_input("Enter your code idea:")
|
| 594 |
+
if st.button("Generate"):
|
| 595 |
+
generated_code = generate_code(code_idea)
|
| 596 |
+
st.code(generated_code, language="python")
|
| 597 |
+
|
| 598 |
+
# --- Build and Deploy ---
|
| 599 |
+
st.subheader("Build and Deploy")
|
| 600 |
+
if st.session_state.current_project:
|
| 601 |
+
st.write(f"Current project: {st.session_state.current_project}")
|
| 602 |
+
if st.button("Build"):
|
| 603 |
+
# Implement build logic here
|
| 604 |
+
build_dir = build_project(st.session_state.current_project)
|
| 605 |
+
st.write(f"Project built successfully! Build directory: {build_dir}")
|
| 606 |
+
|
| 607 |
+
st.write("Select a deployment target:")
|
| 608 |
+
deployment_target = st.selectbox("Deployment Target", ["Local", "Hugging Face Spaces"])
|
| 609 |
+
if deployment_target == "Hugging Face Spaces":
|
| 610 |
+
hf_token = st.text_input("Enter your Hugging Face token:")
|
| 611 |
+
repo_name = st.text_input("Enter your Hugging Face Space repository name:")
|
| 612 |
+
if st.button("Deploy to Hugging Face Spaces"):
|
| 613 |
+
# Implement Hugging Face Spaces deployment logic here
|
| 614 |
+
deploy_to_huggingface(build_dir, hf_token, repo_name)
|
| 615 |
+
elif deployment_target == "Local":
|
| 616 |
+
if st.button("Deploy Locally"):
|
| 617 |
+
# Implement local deployment logic here
|
| 618 |
+
deploy_locally(build_dir)
|
| 619 |
+
else:
|
| 620 |
+
st.warning("Please select a project first.")
|
| 621 |
+
|
| 622 |
+
# --- Run the Streamlit App ---
|
| 623 |
+
if __name__ == "__main__":
|
| 624 |
+
st.set_page_config(page_title="AI Agent Creator", page_icon="🤖")
|
| 625 |
+
st.write("This is the AI Agent Creator application.")
|
| 626 |
+
st.write("You can create AI agents and agent clusters, and use them to chat, generate code, and more.")
|
| 627 |
+
st.write("You can also manage your project workspace, build and deploy your projects, and use AI tools.")
|