Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,251 +1,354 @@
|
|
1 |
import os
|
|
|
2 |
import streamlit as st
|
3 |
-
from huggingface_hub import InferenceClient
|
4 |
-
import gradio as gr
|
5 |
-
import random
|
6 |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
7 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
# --- Agent Definitions ---
|
10 |
class AIAgent:
|
11 |
-
def __init__(self, name, description, skills
|
12 |
self.name = name
|
13 |
self.description = description
|
14 |
self.skills = skills
|
15 |
-
self.model_name = model_name
|
16 |
-
self.client = InferenceClient(self.model_name)
|
17 |
|
18 |
def create_agent_prompt(self):
|
19 |
skills_str = '\n'.join([f"* {skill}" for skill in self.skills])
|
20 |
agent_prompt = f"""
|
21 |
As an elite expert developer, my name is {self.name}. I possess a comprehensive understanding of the following areas:
|
22 |
{skills_str}
|
|
|
23 |
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.
|
24 |
"""
|
25 |
return agent_prompt
|
26 |
|
27 |
-
def generate_response(self, prompt, history, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
|
28 |
-
formatted_prompt = self.format_prompt(prompt, history)
|
29 |
-
stream = self.client.text_generation(formatted_prompt,
|
30 |
-
temperature=temperature,
|
31 |
-
max_new_tokens=max_new_tokens,
|
32 |
-
top_p=top_p,
|
33 |
-
repetition_penalty=repetition_penalty,
|
34 |
-
do_sample=True,
|
35 |
-
seed=random.randint(1, 1111111111111111),
|
36 |
-
stream=True,
|
37 |
-
details=True,
|
38 |
-
return_full_text=False)
|
39 |
-
output = ""
|
40 |
-
for response in stream:
|
41 |
-
output += response.token.text
|
42 |
-
yield output
|
43 |
-
return output
|
44 |
-
|
45 |
-
def format_prompt(self, message, history):
|
46 |
-
prompt = "<s>"
|
47 |
-
for user_prompt, bot_response in history:
|
48 |
-
prompt += f"[INST] {user_prompt} [/INST]"
|
49 |
-
prompt += f" {bot_response}</s> "
|
50 |
-
prompt += f"[INST] {message} [/INST]"
|
51 |
-
return prompt
|
52 |
-
|
53 |
def autonomous_build(self, chat_history, workspace_projects):
|
|
|
|
|
|
|
54 |
summary = "Chat History:\n" + "\n".join([f"User: {u}\nAgent: {a}" for u, a in chat_history])
|
55 |
summary += "\n\nWorkspace Projects:\n" + "\n".join([f"{p}: {details}" for p, details in workspace_projects.items()])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
next_step = "Based on the current state, the next logical step is to implement the main application logic."
|
|
|
57 |
return summary, next_step
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
project_name = st.text_input("Enter project name:")
|
76 |
-
if st.button("Create Project"):
|
77 |
-
if project_name not in st.session_state.workspace_projects:
|
78 |
-
st.session_state.workspace_projects[project_name] = {'files': []}
|
79 |
-
st.success(f"Created project: {project_name}")
|
80 |
else:
|
81 |
-
|
82 |
-
|
83 |
-
# --- Code Addition ---
|
84 |
-
st.subheader("Add Code to Workspace")
|
85 |
-
code_to_add = st.text_area("Enter code to add to workspace:")
|
86 |
-
file_name = st.text_input("Enter file name (e.g. 'app.py'):")
|
87 |
-
if st.button("Add Code"):
|
88 |
-
add_code_status = add_code_to_workspace(project_name, code_to_add, file_name)
|
89 |
-
st.success(add_code_status)
|
90 |
-
|
91 |
-
# --- Terminal Interface ---
|
92 |
-
st.subheader("Terminal (Workspace Context)")
|
93 |
-
terminal_input = st.text_input("Enter a command within the workspace:")
|
94 |
-
if st.button("Run Command"):
|
95 |
-
terminal_output = terminal_interface(terminal_input, project_name)
|
96 |
-
st.code(terminal_output, language="bash")
|
97 |
-
|
98 |
-
# --- Chat Interface ---
|
99 |
-
st.subheader("Chat with DevToolKit for Guidance")
|
100 |
-
chat_input = st.text_area("Enter your message for guidance:")
|
101 |
-
if st.button("Get Guidance"):
|
102 |
-
chat_response = chat_interface(chat_input)
|
103 |
-
st.session_state.chat_history.append((chat_input, chat_response))
|
104 |
-
st.write(f"DevToolKit: {chat_response}")
|
105 |
-
|
106 |
-
# --- Display Chat History ---
|
107 |
-
st.subheader("Chat History")
|
108 |
-
for user_input, response in st.session_state.chat_history:
|
109 |
-
st.write(f"User: {user_input}")
|
110 |
-
st.write(f"DevToolKit: {response}")
|
111 |
-
|
112 |
-
# --- Display Terminal History ---
|
113 |
-
st.subheader("Terminal History")
|
114 |
-
for command, output in st.session_state.terminal_history:
|
115 |
-
st.write(f"Command: {command}")
|
116 |
-
st.code(output, language="bash")
|
117 |
-
|
118 |
-
# --- Display Projects and Files ---
|
119 |
-
st.subheader("Workspace Projects")
|
120 |
-
for project, details in st.session_state.workspace_projects.items():
|
121 |
-
st.write(f"Project: {project}")
|
122 |
-
for file in details['files']:
|
123 |
-
st.write(f" - {file}")
|
124 |
-
|
125 |
-
# --- Chat with AI Agents ---
|
126 |
-
st.subheader("Chat with AI Agents")
|
127 |
-
selected_agent_name = st.selectbox("Select an AI agent", list(agents.keys()))
|
128 |
-
selected_agent = agents[selected_agent_name]
|
129 |
-
agent_chat_input = st.text_area("Enter your message for the agent:")
|
130 |
-
if st.button("Send to Agent"):
|
131 |
-
agent_chat_response = selected_agent.generate_response(agent_chat_input, st.session_state.chat_history)
|
132 |
-
st.session_state.chat_history.append((agent_chat_input, agent_chat_response))
|
133 |
-
st.write(f"{selected_agent.name}: {agent_chat_response}")
|
134 |
-
|
135 |
-
# --- Automate Build Process ---
|
136 |
-
st.subheader("Automate Build Process")
|
137 |
-
if st.button("Automate"):
|
138 |
-
summary, next_step = selected_agent.autonomous_build(st.session_state.chat_history, st.session_state.workspace_projects)
|
139 |
-
st.write("Autonomous Build Summary:")
|
140 |
-
st.write(summary)
|
141 |
-
st.write("Next Step:")
|
142 |
-
st.write(next_step)
|
143 |
-
|
144 |
-
# --- Display current state for debugging ---
|
145 |
-
st.sidebar.subheader("Current State")
|
146 |
-
st.sidebar.json(st.session_state.current_state)
|
147 |
-
|
148 |
-
# --- Gradio Interface ---
|
149 |
-
additional_inputs = [
|
150 |
-
gr.Dropdown(label="Agents", choices=list(agents.keys()), value=list(agents.keys())[0], interactive=True),
|
151 |
-
gr.Textbox(label="System Prompt", max_lines=1, interactive=True),
|
152 |
-
gr.Slider(label="Temperature", value=0.9, minimum=0.0, maximum=1.0, step=0.05, interactive=True, info="Higher values produce more diverse outputs"),
|
153 |
-
gr.Slider(label="Max new tokens", value=1048*10, minimum=0, maximum=1000*10, step=64, interactive=True, info="The maximum numbers of new tokens"),
|
154 |
-
gr.Slider(label="Top-p (nucleus sampling)", value=0.90, minimum=0.0, maximum=1, step=0.05, interactive=True, info="Higher values sample more low-probability tokens"),
|
155 |
-
gr.Slider(label="Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05, interactive=True, info="Penalize repeated tokens"),
|
156 |
-
]
|
157 |
-
|
158 |
-
examples = [
|
159 |
-
["Create a simple web application using Flask", "WEB_DEV", None, None, None, None, ],
|
160 |
-
["Generate a Python script to perform a linear regression analysis", "PYTHON_CODE_DEV", None, None, None, None, ],
|
161 |
-
["Create a Dockerfile for a Node.js application", "AI_SYSTEM_PROMPT", None, None, None, None, ],
|
162 |
-
# Add more examples as needed
|
163 |
-
]
|
164 |
-
|
165 |
-
gr.ChatInterface(
|
166 |
-
fn=generate,
|
167 |
-
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
|
168 |
-
additional_inputs=additional_inputs,
|
169 |
-
title="DevToolKit AI Assistant",
|
170 |
-
examples=examples,
|
171 |
-
concurrency_limit=20,
|
172 |
-
).launch(show_api=True)
|
173 |
-
|
174 |
-
# --- Helper Functions (Moved to separate file) ---
|
175 |
-
def generate(prompt, history, agent_name, sys_prompt="", temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
|
176 |
-
# ... (Implementation in utils.py)
|
177 |
-
|
178 |
-
def chat_interface(chat_input):
|
179 |
-
# ... (Implementation in utils.py)
|
180 |
-
|
181 |
-
def chat_interface_with_agent(chat_input, agent_name):
|
182 |
-
# ... (Implementation in utils.py)
|
183 |
-
|
184 |
-
def terminal_interface(command, project_name):
|
185 |
-
# ... (Implementation in utils.py)
|
186 |
|
187 |
-
def
|
188 |
-
|
189 |
-
|
|
|
|
|
190 |
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
subprocess
|
196 |
-
3. utils.py (Helper Functions)
|
197 |
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
|
|
|
|
214 |
)
|
215 |
-
|
216 |
-
formatted_prompt = agent.format_prompt(f"{system_prompt}, {prompt}", history)
|
217 |
-
stream = agent.client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
218 |
-
output = ""
|
219 |
-
|
220 |
-
for response in stream:
|
221 |
-
output += response.token.text
|
222 |
-
yield output
|
223 |
-
return output
|
224 |
-
|
225 |
-
def chat_interface(chat_input):
|
226 |
-
response = generate(chat_input, st.session_state.chat_history)
|
227 |
return response
|
228 |
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
|
234 |
-
|
|
|
235 |
try:
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
240 |
|
241 |
-
|
242 |
-
|
|
|
243 |
if not os.path.exists(project_path):
|
244 |
os.makedirs(project_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
245 |
file_path = os.path.join(project_path, file_name)
|
246 |
-
with open(file_path,
|
247 |
file.write(code)
|
248 |
-
if project_name not in st.session_state.workspace_projects:
|
249 |
-
st.session_state.workspace_projects[project_name] = {'files': []}
|
250 |
st.session_state.workspace_projects[project_name]['files'].append(file_name)
|
251 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import subprocess
|
3 |
import streamlit as st
|
|
|
|
|
|
|
4 |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
5 |
+
import black
|
6 |
+
from pylint import lint
|
7 |
+
from io import StringIO
|
8 |
+
import openai
|
9 |
+
import sys
|
10 |
+
|
11 |
+
# Set your OpenAI API key here
|
12 |
+
openai.api_key = "YOUR_OPENAI_API_KEY"
|
13 |
+
|
14 |
+
HUGGING_FACE_REPO_URL = "https://huggingface.co/spaces/acecalisto3/DevToolKit"
|
15 |
+
PROJECT_ROOT = "projects"
|
16 |
+
AGENT_DIRECTORY = "agents"
|
17 |
+
|
18 |
+
# Global state to manage communication between Tool Box and Workspace Chat App
|
19 |
+
if 'chat_history' not in st.session_state:
|
20 |
+
st.session_state.chat_history = []
|
21 |
+
if 'terminal_history' not in st.session_state:
|
22 |
+
st.session_state.terminal_history = []
|
23 |
+
if 'workspace_projects' not in st.session_state:
|
24 |
+
st.session_state.workspace_projects = {}
|
25 |
+
if 'available_agents' not in st.session_state:
|
26 |
+
st.session_state.available_agents = []
|
27 |
+
if 'current_state' not in st.session_state:
|
28 |
+
st.session_state.current_state = {
|
29 |
+
'toolbox': {},
|
30 |
+
'workspace_chat': {}
|
31 |
+
}
|
32 |
|
|
|
33 |
class AIAgent:
|
34 |
+
def __init__(self, name, description, skills):
|
35 |
self.name = name
|
36 |
self.description = description
|
37 |
self.skills = skills
|
|
|
|
|
38 |
|
39 |
def create_agent_prompt(self):
|
40 |
skills_str = '\n'.join([f"* {skill}" for skill in self.skills])
|
41 |
agent_prompt = f"""
|
42 |
As an elite expert developer, my name is {self.name}. I possess a comprehensive understanding of the following areas:
|
43 |
{skills_str}
|
44 |
+
|
45 |
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.
|
46 |
"""
|
47 |
return agent_prompt
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
def autonomous_build(self, chat_history, workspace_projects):
|
50 |
+
"""
|
51 |
+
Autonomous build logic that continues based on the state of chat history and workspace projects.
|
52 |
+
"""
|
53 |
summary = "Chat History:\n" + "\n".join([f"User: {u}\nAgent: {a}" for u, a in chat_history])
|
54 |
summary += "\n\nWorkspace Projects:\n" + "\n".join([f"{p}: {details}" for p, details in workspace_projects.items()])
|
55 |
+
|
56 |
+
# Analyze chat history and workspace projects to suggest actions
|
57 |
+
# Example:
|
58 |
+
# - Check if the user has requested to create a new file
|
59 |
+
# - Check if the user has requested to install a package
|
60 |
+
# - Check if the user has requested to run a command
|
61 |
+
# - Check if the user has requested to generate code
|
62 |
+
# - Check if the user has requested to translate code
|
63 |
+
# - Check if the user has requested to summarize text
|
64 |
+
# - Check if the user has requested to analyze sentiment
|
65 |
+
|
66 |
+
# Generate a response based on the analysis
|
67 |
next_step = "Based on the current state, the next logical step is to implement the main application logic."
|
68 |
+
|
69 |
return summary, next_step
|
70 |
|
71 |
+
def save_agent_to_file(agent):
|
72 |
+
"""Saves the agent's prompt to a file."""
|
73 |
+
if not os.path.exists(AGENT_DIRECTORY):
|
74 |
+
os.makedirs(AGENT_DIRECTORY)
|
75 |
+
file_path = os.path.join(AGENT_DIRECTORY, f"{agent.name}.txt")
|
76 |
+
with open(file_path, "w") as file:
|
77 |
+
file.write(agent.create_agent_prompt())
|
78 |
+
st.session_state.available_agents.append(agent.name)
|
79 |
+
|
80 |
+
def load_agent_prompt(agent_name):
|
81 |
+
"""Loads an agent prompt from a file."""
|
82 |
+
file_path = os.path.join(AGENT_DIRECTORY, f"{agent_name}.txt")
|
83 |
+
if os.path.exists(file_path):
|
84 |
+
with open(file_path, "r") as file:
|
85 |
+
agent_prompt = file.read()
|
86 |
+
return agent_prompt
|
|
|
|
|
|
|
|
|
|
|
87 |
else:
|
88 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
+
def create_agent_from_text(name, text):
|
91 |
+
skills = text.split('\n')
|
92 |
+
agent = AIAgent(name, "AI agent created from text input.", skills)
|
93 |
+
save_agent_to_file(agent)
|
94 |
+
return agent.create_agent_prompt()
|
95 |
|
96 |
+
def chat_interface_with_agent(input_text, agent_name):
|
97 |
+
agent_prompt = load_agent_prompt(agent_name)
|
98 |
+
if agent_prompt is None:
|
99 |
+
return f"Agent {agent_name} not found."
|
|
|
|
|
100 |
|
101 |
+
model_name = "Bin12345/AutoCoder_S_6.7B"
|
102 |
+
try:
|
103 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
104 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
105 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
106 |
+
except EnvironmentError as e:
|
107 |
+
return f"Error loading model: {e}"
|
108 |
+
|
109 |
+
combined_input = f"{agent_prompt}\n\nUser: {input_text}\nAgent:"
|
110 |
+
|
111 |
+
input_ids = tokenizer.encode(combined_input, return_tensors="pt")
|
112 |
+
max_input_length = 900
|
113 |
+
if input_ids.shape[1] > max_input_length:
|
114 |
+
input_ids = input_ids[:, :max_input_length]
|
115 |
+
|
116 |
+
outputs = model.generate(
|
117 |
+
input_ids, max_new_tokens=50, num_return_sequences=1, do_sample=True,
|
118 |
+
pad_token_id=tokenizer.eos_token_id # Set pad_token_id to eos_token_id
|
119 |
)
|
120 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
return response
|
122 |
|
123 |
+
# Terminal interface
|
124 |
+
def terminal_interface(command, project_name=None):
|
125 |
+
if project_name:
|
126 |
+
project_path = os.path.join(PROJECT_ROOT, project_name)
|
127 |
+
if not os.path.exists(project_path):
|
128 |
+
return f"Project {project_name} does not exist."
|
129 |
+
result = subprocess.run(command, shell=True, capture_output=True, text=True, cwd=project_path)
|
130 |
+
else:
|
131 |
+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
132 |
+
return result.stdout
|
133 |
|
134 |
+
# Code editor interface
|
135 |
+
def code_editor_interface(code):
|
136 |
try:
|
137 |
+
formatted_code = black.format_str(code, mode=black.FileMode())
|
138 |
+
except black.NothingChanged:
|
139 |
+
formatted_code = code
|
140 |
+
|
141 |
+
result = StringIO()
|
142 |
+
sys.stdout = result
|
143 |
+
sys.stderr = result
|
144 |
+
|
145 |
+
(pylint_stdout, pylint_stderr) = lint.py_run(code, return_std=True)
|
146 |
+
sys.stdout = sys.__stdout__
|
147 |
+
sys.stderr = sys.__stderr__
|
148 |
+
|
149 |
+
lint_message = pylint_stdout.getvalue() + pylint_stderr.getvalue()
|
150 |
+
|
151 |
+
return formatted_code, lint_message
|
152 |
+
|
153 |
+
# Text summarization tool
|
154 |
+
def summarize_text(text):
|
155 |
+
summarizer = pipeline("summarization")
|
156 |
+
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
|
157 |
+
return summary[0]['summary_text']
|
158 |
+
|
159 |
+
# Sentiment analysis tool
|
160 |
+
def sentiment_analysis(text):
|
161 |
+
analyzer = pipeline("sentiment-analysis")
|
162 |
+
result = analyzer(text)
|
163 |
+
return result[0]['label']
|
164 |
+
|
165 |
+
# Text translation tool (code translation)
|
166 |
+
def translate_code(code, source_language, target_language):
|
167 |
+
# Use a Hugging Face translation model instead of OpenAI
|
168 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-es") # Example: English to Spanish
|
169 |
+
translated_code = translator(code, target_lang=target_language)[0]['translation_text']
|
170 |
+
return translated_code
|
171 |
+
|
172 |
+
def generate_code(code_idea):
|
173 |
+
# Use a Hugging Face code generation model instead of OpenAI
|
174 |
+
generator = pipeline('text-generation', model='bigcode/starcoder')
|
175 |
+
generated_code = generator(code_idea, max_length=1000, num_return_sequences=1)[0]['generated_text']
|
176 |
+
return generated_code
|
177 |
+
|
178 |
+
def chat_interface(input_text):
|
179 |
+
"""Handles general chat interactions with the user."""
|
180 |
+
# Use a Hugging Face chatbot model or your own logic
|
181 |
+
chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
|
182 |
+
response = chatbot(input_text, max_length=50, num_return_sequences=1)[0]['generated_text']
|
183 |
+
return response
|
184 |
|
185 |
+
# Workspace interface
|
186 |
+
def workspace_interface(project_name):
|
187 |
+
project_path = os.path.join(PROJECT_ROOT, project_name)
|
188 |
if not os.path.exists(project_path):
|
189 |
os.makedirs(project_path)
|
190 |
+
st.session_state.workspace_projects[project_name] = {'files': []}
|
191 |
+
return f"Project '{project_name}' created successfully."
|
192 |
+
else:
|
193 |
+
return f"Project '{project_name}' already exists."
|
194 |
+
|
195 |
+
# Add code to workspace
|
196 |
+
def add_code_to_workspace(project_name, code, file_name):
|
197 |
+
project_path = os.path.join(PROJECT_ROOT, project_name)
|
198 |
+
if not os.path.exists(project_path):
|
199 |
+
return f"Project '{project_name}' does not exist."
|
200 |
+
|
201 |
file_path = os.path.join(project_path, file_name)
|
202 |
+
with open(file_path, "w") as file:
|
203 |
file.write(code)
|
|
|
|
|
204 |
st.session_state.workspace_projects[project_name]['files'].append(file_name)
|
205 |
+
return f"Code added to '{file_name}' in project '{project_name}'."
|
206 |
+
|
207 |
+
# Streamlit App
|
208 |
+
st.title("AI Agent Creator")
|
209 |
+
|
210 |
+
# Sidebar navigation
|
211 |
+
st.sidebar.title("Navigation")
|
212 |
+
app_mode = st.sidebar.selectbox("Choose the app mode", ["AI Agent Creator", "Tool Box", "Workspace Chat App"])
|
213 |
+
|
214 |
+
if app_mode == "AI Agent Creator":
|
215 |
+
# AI Agent Creator
|
216 |
+
st.header("Create an AI Agent from Text")
|
217 |
+
|
218 |
+
st.subheader("From Text")
|
219 |
+
agent_name = st.text_input("Enter agent name:")
|
220 |
+
text_input = st.text_area("Enter skills (one per line):")
|
221 |
+
if st.button("Create Agent"):
|
222 |
+
agent_prompt = create_agent_from_text(agent_name, text_input)
|
223 |
+
st.success(f"Agent '{agent_name}' created and saved successfully.")
|
224 |
+
st.session_state.available_agents.append(agent_name)
|
225 |
+
|
226 |
+
elif app_mode == "Tool Box":
|
227 |
+
# Tool Box
|
228 |
+
st.header("AI-Powered Tools")
|
229 |
+
|
230 |
+
# Chat Interface
|
231 |
+
st.subheader("Chat with CodeCraft")
|
232 |
+
chat_input = st.text_area("Enter your message:")
|
233 |
+
if st.button("Send"):
|
234 |
+
chat_response = chat_interface(chat_input)
|
235 |
+
st.session_state.chat_history.append((chat_input, chat_response))
|
236 |
+
st.write(f"CodeCraft: {chat_response}")
|
237 |
+
|
238 |
+
# Terminal Interface
|
239 |
+
st.subheader("Terminal")
|
240 |
+
terminal_input = st.text_input("Enter a command:")
|
241 |
+
if st.button("Run"):
|
242 |
+
terminal_output = terminal_interface(terminal_input)
|
243 |
+
st.session_state.terminal_history.append((terminal_input, terminal_output))
|
244 |
+
st.code(terminal_output, language="bash")
|
245 |
+
|
246 |
+
# Code Editor Interface
|
247 |
+
st.subheader("Code Editor")
|
248 |
+
code_editor = st.text_area("Write your code:", height=300)
|
249 |
+
if st.button("Format & Lint"):
|
250 |
+
formatted_code, lint_message = code_editor_interface(code_editor)
|
251 |
+
st.code(formatted_code, language="python")
|
252 |
+
st.info(lint_message)
|
253 |
+
|
254 |
+
# Text Summarization Tool
|
255 |
+
st.subheader("Summarize Text")
|
256 |
+
text_to_summarize = st.text_area("Enter text to summarize:")
|
257 |
+
if st.button("Summarize"):
|
258 |
+
summary = summarize_text(text_to_summarize)
|
259 |
+
st.write(f"Summary: {summary}")
|
260 |
+
|
261 |
+
# Sentiment Analysis Tool
|
262 |
+
st.subheader("Sentiment Analysis")
|
263 |
+
sentiment_text = st.text_area("Enter text for sentiment analysis:")
|
264 |
+
if st.button("Analyze Sentiment"):
|
265 |
+
sentiment = sentiment_analysis(sentiment_text)
|
266 |
+
st.write(f"Sentiment: {sentiment}")
|
267 |
+
|
268 |
+
# Text Translation Tool (Code Translation)
|
269 |
+
st.subheader("Translate Code")
|
270 |
+
code_to_translate = st.text_area("Enter code to translate:")
|
271 |
+
source_language = st.text_input("Enter source language (e.g., 'Python'):")
|
272 |
+
target_language = st.text_input("Enter target language (e.g., 'JavaScript'):")
|
273 |
+
if st.button("Translate Code"):
|
274 |
+
translated_code = translate_code(code_to_translate, source_language, target_language)
|
275 |
+
st.code(translated_code, language=target_language.lower())
|
276 |
+
|
277 |
+
# Code Generation
|
278 |
+
st.subheader("Code Generation")
|
279 |
+
code_idea = st.text_input("Enter your code idea:")
|
280 |
+
if st.button("Generate Code"):
|
281 |
+
generated_code = generate_code(code_idea)
|
282 |
+
st.code(generated_code, language="python")
|
283 |
+
|
284 |
+
elif app_mode == "Workspace Chat App":
|
285 |
+
# Workspace Chat App
|
286 |
+
st.header("Workspace Chat App")
|
287 |
+
|
288 |
+
# Project Workspace Creation
|
289 |
+
st.subheader("Create a New Project")
|
290 |
+
project_name = st.text_input("Enter project name:")
|
291 |
+
if st.button("Create Project"):
|
292 |
+
workspace_status = workspace_interface(project_name)
|
293 |
+
st.success(workspace_status)
|
294 |
+
|
295 |
+
# Add Code to Workspace
|
296 |
+
st.subheader("Add Code to Workspace")
|
297 |
+
code_to_add = st.text_area("Enter code to add to workspace:")
|
298 |
+
file_name = st.text_input("Enter file name (e.g., 'app.py'):")
|
299 |
+
if st.button("Add Code"):
|
300 |
+
add_code_status = add_code_to_workspace(project_name, code_to_add, file_name)
|
301 |
+
st.success(add_code_status)
|
302 |
+
|
303 |
+
# Terminal Interface with Project Context
|
304 |
+
st.subheader("Terminal (Workspace Context)")
|
305 |
+
terminal_input = st.text_input("Enter a command within the workspace:")
|
306 |
+
if st.button("Run Command"):
|
307 |
+
terminal_output = terminal_interface(terminal_input, project_name)
|
308 |
+
st.code(terminal_output, language="bash")
|
309 |
+
|
310 |
+
# Chat Interface for Guidance
|
311 |
+
st.subheader("Chat with CodeCraft for Guidance")
|
312 |
+
chat_input = st.text_area("Enter your message for guidance:")
|
313 |
+
if st.button("Get Guidance"):
|
314 |
+
chat_response = chat_interface(chat_input)
|
315 |
+
st.session_state.chat_history.append((chat_input, chat_response))
|
316 |
+
st.write(f"CodeCraft: {chat_response}")
|
317 |
+
|
318 |
+
# Display Chat History
|
319 |
+
st.subheader("Chat History")
|
320 |
+
for user_input, response in st.session_state.chat_history:
|
321 |
+
st.write(f"User: {user_input}")
|
322 |
+
st.write(f"CodeCraft: {response}")
|
323 |
+
|
324 |
+
# Display Terminal History
|
325 |
+
st.subheader("Terminal History")
|
326 |
+
for command, output in st.session_state.terminal_history:
|
327 |
+
st.write(f"Command: {command}")
|
328 |
+
st.code(output, language="bash")
|
329 |
+
|
330 |
+
# Display Projects and Files
|
331 |
+
st.subheader("Workspace Projects")
|
332 |
+
for project, details in st.session_state.workspace_projects.items():
|
333 |
+
st.write(f"Project: {project}")
|
334 |
+
for file in details['files']:
|
335 |
+
st.write(f" - {file}")
|
336 |
+
|
337 |
+
# Chat with AI Agents
|
338 |
+
st.subheader("Chat with AI Agents")
|
339 |
+
selected_agent = st.selectbox("Select an AI agent", st.session_state.available_agents)
|
340 |
+
agent_chat_input = st.text_area("Enter your message for the agent:")
|
341 |
+
if st.button("Send to Agent"):
|
342 |
+
agent_chat_response = chat_interface_with_agent(agent_chat_input, selected_agent)
|
343 |
+
st.session_state.chat_history.append((agent_chat_input, agent_chat_response))
|
344 |
+
st.write(f"{selected_agent}: {agent_chat_response}")
|
345 |
+
|
346 |
+
# Automate Build Process
|
347 |
+
st.subheader("Automate Build Process")
|
348 |
+
if st.button("Automate"):
|
349 |
+
agent = AIAgent(selected_agent, "", []) # Load the agent without skills for now
|
350 |
+
summary, next_step = agent.autonomous_build(st.session_state.chat_history, st.session_state.workspace_projects)
|
351 |
+
st.write("Autonomous Build Summary:")
|
352 |
+
st.write(summary)
|
353 |
+
st.write("Next Step:")
|
354 |
+
st.write(next_step)
|