Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,13 +2,11 @@ import os
|
|
2 |
import json
|
3 |
import subprocess
|
4 |
import re
|
5 |
-
import ast
|
6 |
import requests
|
7 |
from datetime import datetime
|
8 |
|
9 |
import gradio as gr
|
10 |
-
from transformers import
|
11 |
-
TrainingArguments, Trainer, AutoModel, RagRetriever, AutoModelForSeq2SeqLM)
|
12 |
import torch
|
13 |
import tree_sitter
|
14 |
from tree_sitter import Language, Parser
|
@@ -18,556 +16,235 @@ from io import StringIO
|
|
18 |
import sys
|
19 |
from huggingface_hub import Repository, hf_hub_url, HfApi, snapshot_download
|
20 |
import tempfile
|
21 |
-
|
|
|
|
|
22 |
|
23 |
# Constants
|
24 |
MODEL_NAME = "bigscience/bloom"
|
25 |
PROJECT_ROOT = "projects"
|
26 |
AGENT_DIRECTORY = "agents"
|
27 |
AVAILABLE_CODE_GENERATIVE_MODELS = [
|
28 |
-
"bigcode/starcoder",
|
29 |
-
"Salesforce/codegen-350M-mono",
|
30 |
-
"microsoft/CodeGPT-small",
|
31 |
-
"
|
32 |
-
"facebook/
|
33 |
]
|
34 |
|
35 |
-
|
36 |
# Load Models and Resources
|
37 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
38 |
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float16)
|
39 |
pipe = TextGenerationPipeline(model=model, tokenizer=tokenizer)
|
40 |
|
41 |
# Build Tree-sitter parser libraries (if not already built)
|
42 |
-
Language.build_library(
|
43 |
-
PYTHON_LANGUAGE = Language(
|
44 |
-
JAVASCRIPT_LANGUAGE = Language(
|
45 |
parser = Parser()
|
46 |
|
47 |
# Session State Initialization
|
48 |
-
if 'chat_history' not in
|
49 |
-
|
50 |
-
if 'terminal_history' not in
|
51 |
-
|
52 |
-
if 'workspace_projects' not in
|
53 |
-
|
54 |
-
if 'available_agents' not in
|
55 |
-
|
56 |
-
if 'current_state' not in
|
57 |
-
|
58 |
'toolbox': {},
|
59 |
'workspace_chat': {}
|
60 |
}
|
61 |
|
|
|
|
|
|
|
62 |
|
63 |
-
#
|
64 |
-
|
65 |
-
"bigcode/starcoder", # Popular and powerful
|
66 |
-
"Salesforce/codegen-350M-mono", # Smaller, good for quick tasks
|
67 |
-
"microsoft/CodeGPT-small", # Smaller, good for quick tasks
|
68 |
-
"google/flan-t5-xl", # Powerful, good for complex tasks
|
69 |
-
"facebook/bart-large-cnn", # Good for text-to-code tasks
|
70 |
-
]
|
71 |
|
72 |
-
#
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
# Load tokenizer
|
79 |
-
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
80 |
-
|
81 |
-
def process_input(user_input):
|
82 |
-
# Input pipeline: Tokenize and preprocess user input
|
83 |
-
input_ids = tokenizer(user_input, return_tensors="pt").input_ids
|
84 |
-
attention_mask = tokenizer(user_input, return_tensors="pt").attention_mask
|
85 |
-
|
86 |
-
# RAG model: Generate response
|
87 |
-
with torch.no_grad():
|
88 |
-
output = rag_retriever(input_ids, attention_mask=attention_mask)
|
89 |
-
response = output.generator_outputs[0].sequences[0]
|
90 |
-
|
91 |
-
# Chat model: Refine response
|
92 |
-
chat_input = tokenizer(response, return_tensors="pt")
|
93 |
-
chat_input["input_ids"] = chat_input["input_ids"].unsqueeze(0)
|
94 |
-
chat_input["attention_mask"] = chat_input["attention_mask"].unsqueeze(0)
|
95 |
-
with torch.no_grad():
|
96 |
-
chat_output = chat_model(**chat_input)
|
97 |
-
refined_response = chat_output.sequences[0]
|
98 |
-
|
99 |
-
# Output pipeline: Return final response
|
100 |
-
return refined_response
|
101 |
-
|
102 |
-
class AIAgent:
|
103 |
-
def __init__(self, name, description, skills):
|
104 |
-
self.name = name
|
105 |
-
self.description = description
|
106 |
-
self.skills = skills
|
107 |
-
|
108 |
-
def create_agent_prompt(self):
|
109 |
-
skills_str = '\n'.join([f"* {skill}" for skill in self.skills])
|
110 |
-
agent_prompt = f"""
|
111 |
-
As an elite expert developer, my name is {self.name}. I possess a comprehensive understanding of the following areas:
|
112 |
-
{skills_str}
|
113 |
-
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.
|
114 |
-
"""
|
115 |
-
return agent_prompt
|
116 |
-
|
117 |
-
def autonomous_build(self, chat_history, workspace_projects, project_name, selected_model):
|
118 |
-
"""
|
119 |
-
Autonomous build logic that continues based on the state of chat history and workspace projects.
|
120 |
-
"""
|
121 |
-
summary = "Chat History:\n" + "\n".join([f"User: {u}\nAgent: {a}" for u, a in chat_history])
|
122 |
-
summary += "\n\nWorkspace Projects:\n" + "\n".join([f"{p}: {details}" for p, details in workspace_projects.items()])
|
123 |
-
|
124 |
-
# Analyze chat history and workspace projects to suggest actions
|
125 |
-
# Example:
|
126 |
-
# - Check if the user has requested to create a new file
|
127 |
-
# - Check if the user has requested to install a package
|
128 |
-
# - Check if the user has requested to run a command
|
129 |
-
# - Check if the user has requested to generate code
|
130 |
-
# - Check if the user has requested to translate code
|
131 |
-
# - Check if the user has requested to summarize text
|
132 |
-
# - Check if the user has requested to analyze sentiment
|
133 |
-
|
134 |
-
# Generate a response based on the analysis
|
135 |
-
next_step = "Based on the current state, the next logical step is to implement the main application logic."
|
136 |
-
|
137 |
-
# Ensure project folder exists
|
138 |
-
project_path = os.path.join(PROJECT_ROOT, project_name)
|
139 |
-
if not os.path.exists(project_path):
|
140 |
-
os.makedirs(project_path)
|
141 |
-
|
142 |
-
# Create requirements.txt if it doesn't exist
|
143 |
-
requirements_file = os.path.join(project_path, "requirements.txt")
|
144 |
-
if not os.path.exists(requirements_file):
|
145 |
-
with open(requirements_file, "w") as f:
|
146 |
-
f.write("# Add your project's dependencies here\n")
|
147 |
-
|
148 |
-
# Create app.py if it doesn't exist
|
149 |
-
app_file = os.path.join(project_path, "app.py")
|
150 |
-
if not os.path.exists(app_file):
|
151 |
-
with open(app_file, "w") as f:
|
152 |
-
f.write("# Your project's main application logic goes here\n")
|
153 |
-
|
154 |
-
# Generate GUI code for app.py if requested
|
155 |
-
if "create a gui" in summary.lower():
|
156 |
-
gui_code = generate_code("Create a simple GUI for this application", selected_model)
|
157 |
-
with open(app_file, "a") as f:
|
158 |
-
f.write(gui_code)
|
159 |
-
|
160 |
-
# Run the default build process
|
161 |
-
build_command = "pip install -r requirements.txt && python app.py"
|
162 |
-
try:
|
163 |
-
result = subprocess.run(build_command, shell=True, capture_output=True, text=True, cwd=project_path)
|
164 |
-
st.write(f"Build Output:\n{result.stdout}")
|
165 |
-
if result.stderr:
|
166 |
-
st.error(f"Build Errors:\n{result.stderr}")
|
167 |
-
except Exception as e:
|
168 |
-
st.error(f"Build Error: {e}")
|
169 |
-
|
170 |
-
return summary, next_step
|
171 |
-
|
172 |
-
def save_agent_to_file(agent):
|
173 |
-
"""Saves the agent's prompt to a file."""
|
174 |
-
if not os.path.exists(AGENT_DIRECTORY):
|
175 |
-
os.makedirs(AGENT_DIRECTORY)
|
176 |
-
file_path = os.path.join(AGENT_DIRECTORY, f"{agent.name}.txt")
|
177 |
-
with open(file_path, "w") as file:
|
178 |
-
file.write(agent.create_agent_prompt())
|
179 |
-
st.session_state.available_agents.append(agent.name)
|
180 |
-
|
181 |
-
def load_agent_prompt(agent_name):
|
182 |
-
"""Loads an agent prompt from a file."""
|
183 |
-
file_path = os.path.join(AGENT_DIRECTORY, f"{agent_name}.txt")
|
184 |
-
if os.path.exists(file_path):
|
185 |
-
with open(file_path, "r") as file:
|
186 |
-
agent_prompt = file.read()
|
187 |
-
return agent_prompt
|
188 |
-
else:
|
189 |
return None
|
190 |
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
save_agent_to_file(agent)
|
195 |
-
return agent.create_agent_prompt()
|
196 |
|
197 |
-
|
198 |
-
|
199 |
-
if agent_prompt is None:
|
200 |
-
return f"Agent {agent_name} not found."
|
201 |
-
|
202 |
-
model_name ="MaziyarPanahi/Codestral-22B-v0.1-GGUF"
|
203 |
-
try:
|
204 |
-
from transformers import AutoModel, AutoTokenizer # Import AutoModel here
|
205 |
-
model = AutoModel.from_pretrained("MaziyarPanahi/Codestral-22B-v0.1-GGUF")
|
206 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
207 |
-
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
208 |
-
except EnvironmentError as e:
|
209 |
-
return f"Error loading model: {e}"
|
210 |
-
|
211 |
-
combined_input = f"{agent_prompt}\n\nUser: {input_text}\nAgent:"
|
212 |
-
|
213 |
-
input_ids = tokenizer.encode(combined_input, return_tensors="pt")
|
214 |
-
max_input_length = 900
|
215 |
-
if input_ids.shape[1] > max_input_length:
|
216 |
-
input_ids = input_ids[:, :max_input_length]
|
217 |
-
|
218 |
-
outputs = model.generate(
|
219 |
-
input_ids, max_new_tokens=50, num_return_sequences=1, do_sample=True,
|
220 |
-
pad_token_id=tokenizer.eos_token_id # Set pad_token_id to eos_token_id
|
221 |
-
)
|
222 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
223 |
-
return response
|
224 |
-
|
225 |
-
# Terminal interface
|
226 |
-
def terminal_interface(command, project_name=None):
|
227 |
-
if project_name:
|
228 |
-
project_path = os.path.join(PROJECT_ROOT, project_name)
|
229 |
-
if not os.path.exists(project_path):
|
230 |
-
return f"Project {project_name} does not exist."
|
231 |
-
result = subprocess.run(command, shell=True, capture_output=True, text=True, cwd=project_path)
|
232 |
-
else:
|
233 |
-
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
234 |
-
return result.stdout
|
235 |
-
|
236 |
-
# Code editor interface
|
237 |
-
def code_editor_interface(code):
|
238 |
-
try:
|
239 |
-
formatted_code = black.format_str(code, mode=black.FileMode())
|
240 |
-
except black.NothingChanged:
|
241 |
-
formatted_code = code
|
242 |
-
|
243 |
-
result = StringIO()
|
244 |
-
sys.stdout = result
|
245 |
-
sys.stderr = result
|
246 |
-
|
247 |
-
(pylint_stdout, pylint_stderr) = lint.py_run(code, return_std=True)
|
248 |
-
sys.stdout = sys.__stdout__
|
249 |
-
sys.stderr = sys.__stderr__
|
250 |
-
|
251 |
-
lint_message = pylint_stdout.getvalue() + pylint_stderr.getvalue()
|
252 |
-
|
253 |
-
return formatted_code, lint_message
|
254 |
-
|
255 |
-
# Text summarization tool
|
256 |
-
def summarize_text(text):
|
257 |
-
summarizer = pipeline("summarization")
|
258 |
-
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
|
259 |
-
return summary[0]['summary_text']
|
260 |
-
|
261 |
-
# Sentiment analysis tool
|
262 |
-
def sentiment_analysis(text):
|
263 |
-
analyzer = pipeline("sentiment-analysis")
|
264 |
-
result = analyzer(text)
|
265 |
-
return result[0]['label']
|
266 |
-
|
267 |
-
# Text translation tool (code translation)
|
268 |
-
def translate_code(code, source_language, target_language):
|
269 |
-
# Use a Hugging Face translation model instead of OpenAI
|
270 |
-
translator = pipeline("translation", model="bartowski/Codestral-22B-v0.1-GGUF") # Example: English to Spanish
|
271 |
-
translated_code = translator(code, target_lang=target_language)[0]['translation_text']
|
272 |
-
return translated_code
|
273 |
-
|
274 |
-
def generate_code(code_idea, model_name):
|
275 |
-
"""Generates code using the selected model."""
|
276 |
try:
|
277 |
-
|
278 |
-
|
279 |
-
return generated_code
|
280 |
except Exception as e:
|
281 |
-
|
|
|
282 |
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
response = chatbot(input_text, max_length=50, num_return_sequences=1)[0]['generated_text']
|
288 |
-
return response
|
289 |
-
|
290 |
-
# Workspace interface
|
291 |
-
def workspace_interface(project_name):
|
292 |
-
project_path = os.path.join(PROJECT_ROOT, project_name)
|
293 |
-
if not os.path.exists(project_path):
|
294 |
-
os.makedirs(project_path)
|
295 |
-
st.session_state.workspace_projects[project_name] = {'files': []}
|
296 |
-
return f"Project '{project_name}' created successfully."
|
297 |
-
else:
|
298 |
-
return f"Project '{project_name}' already exists."
|
299 |
-
|
300 |
-
# Add code to workspace
|
301 |
-
def add_code_to_workspace(project_name, code, file_name):
|
302 |
-
project_path = os.path.join(PROJECT_ROOT, project_name)
|
303 |
-
if not os.path.exists(project_path):
|
304 |
-
return f"Project '{project_name}' does not exist."
|
305 |
-
|
306 |
-
file_path = os.path.join(project_path, file_name)
|
307 |
-
with open(file_path, "w") as file:
|
308 |
-
file.write(code)
|
309 |
-
st.session_state.workspace_projects[project_name]['files'].append(file_name)
|
310 |
-
return f"Code added to '{file_name}' in project '{project_name}'."
|
311 |
-
|
312 |
-
# Streamlit App
|
313 |
-
st.title("AI Agent Creator")
|
314 |
-
|
315 |
-
# Sidebar navigation
|
316 |
-
st.sidebar.title("Navigation")
|
317 |
-
app_mode = st.sidebar.selectbox("Choose the app mode", ["AI Agent Creator", "Tool Box", "Workspace Chat App"])
|
318 |
-
|
319 |
-
# Get Hugging Face token from secrets.toml
|
320 |
-
hf_token = st.secrets["huggingface"]["hf_token"]
|
321 |
-
|
322 |
-
if app_mode == "AI Agent Creator":
|
323 |
-
# AI Agent Creator
|
324 |
-
st.header("Create an AI Agent from Text")
|
325 |
-
|
326 |
-
st.subheader("From Text")
|
327 |
-
agent_name = st.text_input("Enter agent name:")
|
328 |
-
text_input = st.text_area("Enter skills (one per line):")
|
329 |
-
if st.button("Create Agent"):
|
330 |
-
agent_prompt = create_agent_from_text(agent_name, text_input)
|
331 |
-
st.success(f"Agent '{agent_name}' created and saved successfully.")
|
332 |
-
st.session_state.available_agents.append(agent_name)
|
333 |
-
|
334 |
-
elif app_mode == "Tool Box":
|
335 |
-
# Tool Box
|
336 |
-
st.header("AI-Powered Tools")
|
337 |
-
|
338 |
-
# Chat Interface
|
339 |
-
st.subheader("Chat with CodeCraft")
|
340 |
-
chat_input = st.text_area("Enter your message:")
|
341 |
-
if st.button("Send"):
|
342 |
-
chat_response = chat_interface(chat_input)
|
343 |
-
st.session_state.chat_history.append((chat_input, chat_response))
|
344 |
-
st.write(f"CodeCraft: {chat_response}")
|
345 |
-
|
346 |
-
# Terminal Interface
|
347 |
-
st.subheader("Terminal")
|
348 |
-
terminal_input = st.text_input("Enter a command:")
|
349 |
-
if st.button("Run"):
|
350 |
-
terminal_output = terminal_interface(terminal_input)
|
351 |
-
st.session_state.terminal_history.append((terminal_input, terminal_output))
|
352 |
-
st.code(terminal_output, language="bash")
|
353 |
-
|
354 |
-
# Code Editor Interface
|
355 |
-
st.subheader("Code Editor")
|
356 |
-
code_editor = st.text_area("Write your code:", height=300)
|
357 |
-
if st.button("Format & Lint"):
|
358 |
-
formatted_code, lint_message = code_editor_interface(code_editor)
|
359 |
-
st.code(formatted_code, language="python")
|
360 |
-
st.info(lint_message)
|
361 |
-
|
362 |
-
# Text Summarization Tool
|
363 |
-
st.subheader("Summarize Text")
|
364 |
-
text_to_summarize = st.text_area("Enter text to summarize:")
|
365 |
-
if st.button("Summarize"):
|
366 |
-
summary = summarize_text(text_to_summarize)
|
367 |
-
st.write(f"Summary: {summary}")
|
368 |
-
|
369 |
-
# Sentiment Analysis Tool
|
370 |
-
st.subheader("Sentiment Analysis")
|
371 |
-
sentiment_text = st.text_area("Enter text for sentiment analysis:")
|
372 |
-
if st.button("Analyze Sentiment"):
|
373 |
-
sentiment = sentiment_analysis(sentiment_text)
|
374 |
-
st.write(f"Sentiment: {sentiment}")
|
375 |
-
|
376 |
-
# Text Translation Tool (Code Translation)
|
377 |
-
st.subheader("Translate Code")
|
378 |
-
code_to_translate = st.text_area("Enter code to translate:")
|
379 |
-
source_language = st.text_input("Enter source language (e.g., 'Python'):")
|
380 |
-
target_language = st.text_input("Enter target language (e.g., 'JavaScript'):")
|
381 |
-
if st.button("Translate Code"):
|
382 |
-
translated_code = translate_code(code_to_translate, source_language, target_language)
|
383 |
-
st.code(translated_code, language=target_language.lower())
|
384 |
-
|
385 |
-
# Code Generation
|
386 |
-
st.subheader("Code Generation")
|
387 |
-
code_idea = st.text_input("Enter your code idea:")
|
388 |
-
if st.button("Generate Code"):
|
389 |
-
generated_code = generate_code(code_idea)
|
390 |
-
st.code(generated_code, language="python")
|
391 |
-
|
392 |
-
elif app_mode == "Workspace Chat App":
|
393 |
-
# Workspace Chat App
|
394 |
-
st.header("Workspace Chat App")
|
395 |
-
|
396 |
-
# Project Workspace Creation
|
397 |
-
st.subheader("Create a New Project")
|
398 |
-
project_name = st.text_input("Enter project name:")
|
399 |
-
if st.button("Create Project"):
|
400 |
-
workspace_status = workspace_interface(project_name)
|
401 |
-
st.success(workspace_status)
|
402 |
-
|
403 |
-
# Automatically create requirements.txt and app.py
|
404 |
-
project_path = os.path.join(PROJECT_ROOT, project_name)
|
405 |
-
requirements_file = os.path.join(project_path, "requirements.txt")
|
406 |
-
if not os.path.exists(requirements_file):
|
407 |
-
with open(requirements_file, "w") as f:
|
408 |
-
f.write("# Add your project's dependencies here\n")
|
409 |
-
|
410 |
-
app_file = os.path.join(project_path, "app.py")
|
411 |
-
if not os.path.exists(app_file):
|
412 |
-
with open(app_file, "w") as f:
|
413 |
-
f.write("# Your project's main application logic goes here\n")
|
414 |
-
|
415 |
-
# Add Code to Workspace
|
416 |
-
st.subheader("Add Code to Workspace")
|
417 |
-
code_to_add = st.text_area("Enter code to add to workspace:")
|
418 |
-
file_name = st.text_input("Enter file name (e.g., 'app.py'):")
|
419 |
-
if st.button("Add Code"):
|
420 |
-
add_code_status = add_code_to_workspace(project_name, code_to_add, file_name)
|
421 |
-
st.session_state.terminal_history.append((f"Add Code: {code_to_add}", add_code_status))
|
422 |
-
st.success(add_code_status)
|
423 |
-
|
424 |
-
# Terminal Interface with Project Context
|
425 |
-
st.subheader("Terminal (Workspace Context)")
|
426 |
-
terminal_input = st.text_input("Enter a command within the workspace:")
|
427 |
-
if st.button("Run Command"):
|
428 |
-
terminal_output = terminal_interface(terminal_input, project_name)
|
429 |
-
st.session_state.terminal_history.append((terminal_input, terminal_output))
|
430 |
-
st.code(terminal_output, language="bash")
|
431 |
-
|
432 |
-
# Chat Interface for Guidance
|
433 |
-
st.subheader("Chat with CodeCraft for Guidance")
|
434 |
-
chat_input = st.text_area("Enter your message for guidance:")
|
435 |
-
if st.button("Get Guidance"):
|
436 |
-
chat_response = chat_interface(chat_input)
|
437 |
-
st.session_state.chat_history.append((chat_input, chat_response))
|
438 |
-
st.write(f"CodeCraft: {chat_response}")
|
439 |
-
|
440 |
-
# Display Chat History
|
441 |
-
st.subheader("Chat History")
|
442 |
-
for user_input, response in st.session_state.chat_history:
|
443 |
-
st.write(f"User: {user_input}")
|
444 |
-
st.write(f"CodeCraft: {response}")
|
445 |
-
|
446 |
-
# Display Terminal History
|
447 |
-
st.subheader("Terminal History")
|
448 |
-
for command, output in st.session_state.terminal_history:
|
449 |
-
st.write(f"Command: {command}")
|
450 |
-
st.code(output, language="bash")
|
451 |
-
|
452 |
-
# Display Projects and Files
|
453 |
-
st.subheader("Workspace Projects")
|
454 |
-
for project, details in st.session_state.workspace_projects.items():
|
455 |
-
st.write(f"Project: {project}")
|
456 |
-
for file in details['files']:
|
457 |
-
st.write(f" - {file}")
|
458 |
-
|
459 |
-
# Chat with AI Agents
|
460 |
-
st.subheader("Chat with AI Agents")
|
461 |
-
selected_agent = st.selectbox("Select an AI agent", st.session_state.available_agents)
|
462 |
-
agent_chat_input = st.text_area("Enter your message for the agent:")
|
463 |
-
if st.button("Send to Agent"):
|
464 |
-
agent_chat_response = chat_interface_with_agent(agent_chat_input, selected_agent)
|
465 |
-
st.session_state.chat_history.append((agent_chat_input, agent_chat_response))
|
466 |
-
st.write(f"{selected_agent}: {agent_chat_response}")
|
467 |
-
|
468 |
-
# Code Generation
|
469 |
-
st.subheader("Code Generation")
|
470 |
-
code_idea = st.text_input("Enter your code idea:")
|
471 |
-
|
472 |
-
# Model Selection Menu
|
473 |
-
selected_model = st.selectbox("Select a code-generative model", AVAILABLE_CODE_GENERATIVE_MODELS)
|
474 |
-
|
475 |
-
if st.button("Generate Code"):
|
476 |
-
generated_code = generate_code(code_idea, selected_model)
|
477 |
-
st.code(generated_code, language="python")
|
478 |
-
|
479 |
-
# Automate Build Process
|
480 |
-
st.subheader("Automate Build Process")
|
481 |
-
if st.button("Automate"):
|
482 |
-
agent = AIAgent(selected_agent, "", []) # Load the agent without skills for now
|
483 |
-
summary, next_step = agent.autonomous_build(st.session_state.chat_history, st.session_state.workspace_projects, project_name, selected_model)
|
484 |
-
st.write("Autonomous Build Summary:")
|
485 |
-
st.write(summary)
|
486 |
-
st.write("Next Step:")
|
487 |
-
st.write(next_step)
|
488 |
-
|
489 |
-
# Use the hf_token to interact with the Hugging Face API
|
490 |
-
api = HfApi(token=hf_token)
|
491 |
-
# Function to create a Space on Hugging Face
|
492 |
-
def create_space(api, name, description, public, files, entrypoint="launch.py"):
|
493 |
-
url = f"{hf_hub_url()}spaces/{name}/prepare-repo"
|
494 |
-
headers = {"Authorization": f"Bearer {api.access_token}"}
|
495 |
-
|
496 |
-
# Set your Hugging Face API key here
|
497 |
-
hf_token = "YOUR_HUGGING_FACE_API_KEY" # Replace with your actual token
|
498 |
-
|
499 |
-
# Other code remains unchanged
|
500 |
-
|
501 |
-
class AIAgent:
|
502 |
-
def __init__(self, name, description, skills, hf_api=None):
|
503 |
-
self.name = name
|
504 |
-
self.description = description
|
505 |
-
self.skills = skills
|
506 |
-
self._hf_api = hf_api
|
507 |
-
|
508 |
-
@property
|
509 |
-
def hf_api(self):
|
510 |
-
if not self._hf_api and self.has_valid_hf_token():
|
511 |
-
self._hf_api = HfApi(token=self._hf_token)
|
512 |
-
return self._hf_api
|
513 |
-
|
514 |
-
def has_valid_hf_token(self):
|
515 |
-
return bool(self._hf_token)
|
516 |
-
|
517 |
-
async def autonomous_build(self, chat_history, workspace_projects, project_name, selected_model, hf_token):
|
518 |
-
self._hf_token = hf_token
|
519 |
-
# Continuation of previous methods
|
520 |
-
|
521 |
-
def deploy_built_space_to_hf(self):
|
522 |
-
if not self._hf_api or not self._hf_token:
|
523 |
-
raise ValueError("Cannot deploy the Space since no valid Hugoging Face API connection was established.")
|
524 |
-
|
525 |
-
repository_name = f"my-awesome-space_{datetime.now().timestamp()}"
|
526 |
-
files = get_built_space_files()
|
527 |
-
|
528 |
-
commit_response = self.hf_api.commit_repo(
|
529 |
-
repo_id=repository_name,
|
530 |
-
branch="main",
|
531 |
-
commits=[{"message": "Built Space Commit", "tree": tree_payload}]
|
532 |
-
)
|
533 |
|
534 |
-
|
535 |
-
|
536 |
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
|
542 |
-
|
|
|
543 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
544 |
|
545 |
-
|
|
|
|
|
546 |
|
547 |
-
|
548 |
-
|
549 |
-
|
|
|
|
|
550 |
|
551 |
-
|
552 |
-
"public": public,
|
553 |
-
"gitignore_template": "web",
|
554 |
-
"default_branch": "main",
|
555 |
-
"archived": False,
|
556 |
-
"files": []
|
557 |
-
}
|
558 |
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
568 |
-
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import json
|
3 |
import subprocess
|
4 |
import re
|
|
|
5 |
import requests
|
6 |
from datetime import datetime
|
7 |
|
8 |
import gradio as gr
|
9 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer, TextGenerationPipeline, AutoModel, RagRetriever, AutoModelForSeq2SeqLM
|
|
|
10 |
import torch
|
11 |
import tree_sitter
|
12 |
from tree_sitter import Language, Parser
|
|
|
16 |
import sys
|
17 |
from huggingface_hub import Repository, hf_hub_url, HfApi, snapshot_download
|
18 |
import tempfile
|
19 |
+
import logging
|
20 |
+
from loguru import logger
|
21 |
+
logger.add("app.log", format="{time} {level} {message}", level="INFO")
|
22 |
|
23 |
# Constants
|
24 |
MODEL_NAME = "bigscience/bloom"
|
25 |
PROJECT_ROOT = "projects"
|
26 |
AGENT_DIRECTORY = "agents"
|
27 |
AVAILABLE_CODE_GENERATIVE_MODELS = [
|
28 |
+
"bigcode/starcoder",
|
29 |
+
"Salesforce/codegen-350M-mono",
|
30 |
+
"microsoft/CodeGPT-small-py",
|
31 |
+
"NinedayWang/PolyCoder-2.7B",
|
32 |
+
"facebook/incoder-1B",
|
33 |
]
|
34 |
|
|
|
35 |
# Load Models and Resources
|
36 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
37 |
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float16)
|
38 |
pipe = TextGenerationPipeline(model=model, tokenizer=tokenizer)
|
39 |
|
40 |
# Build Tree-sitter parser libraries (if not already built)
|
41 |
+
Language.build_library("build/my-languages.so", ["tree-sitter-python", "tree-sitter-javascript"])
|
42 |
+
PYTHON_LANGUAGE = Language("build/my-languages.so", "python")
|
43 |
+
JAVASCRIPT_LANGUAGE = Language("build/my-languages.so", "javascript")
|
44 |
parser = Parser()
|
45 |
|
46 |
# Session State Initialization
|
47 |
+
if 'chat_history' not in gr.State.session_state:
|
48 |
+
gr.State.chat_history = []
|
49 |
+
if 'terminal_history' not in gr.State.session_state:
|
50 |
+
gr.State.terminal_history = []
|
51 |
+
if 'workspace_projects' not in gr.State.session_state:
|
52 |
+
gr.State.workspace_projects = {}
|
53 |
+
if 'available_agents' not in gr.State.session_state:
|
54 |
+
gr.State.available_agents = []
|
55 |
+
if 'current_state' not in gr.State.session_state:
|
56 |
+
gr.State.current_state = {
|
57 |
'toolbox': {},
|
58 |
'workspace_chat': {}
|
59 |
}
|
60 |
|
61 |
+
# Define is_code function
|
62 |
+
def is_code(message):
|
63 |
+
return message.lstrip().startswith("```") or message.lstrip().startswith("code:")
|
64 |
|
65 |
+
# Define agents variable
|
66 |
+
agents = ["python", "javascript", "java"]
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
# Define load_agent_from_file function
|
69 |
+
def load_agent_from_file(agent_name):
|
70 |
+
try:
|
71 |
+
with open(os.path.join(AGENT_DIRECTORY, agent_name + ".json"), "r") as f:
|
72 |
+
return json.load(f)
|
73 |
+
except FileNotFoundError:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
return None
|
75 |
|
76 |
+
# Define load_pipeline function
|
77 |
+
def load_pipeline(model_category, model_name):
|
78 |
+
return available_models[model_category][model_name]
|
|
|
|
|
79 |
|
80 |
+
# Define execute_translation function
|
81 |
+
def execute_translation(code, target_language, pipe):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
try:
|
83 |
+
output = pipe(code, max_length=1000)[0]["generated_text"]
|
84 |
+
return output
|
|
|
85 |
except Exception as e:
|
86 |
+
logger.error(f"Error in execute_translation function: {e}")
|
87 |
+
return "Error: Unable to translate code."
|
88 |
|
89 |
+
# Refactor using CodeT5+
|
90 |
+
def execute_refactoring_codet5(code: str) -> str:
|
91 |
+
"""
|
92 |
+
Refactors the provided code using the CodeT5+ model.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
+
Args:
|
95 |
+
code (str): The code to refactor.
|
96 |
|
97 |
+
Returns:
|
98 |
+
str: The refactored code.
|
99 |
+
"""
|
100 |
+
try:
|
101 |
+
refactor_pipe = pipeline(
|
102 |
+
"text2text-generation",
|
103 |
+
model="Salesforce/codet5p-220m-finetune-Refactor"
|
104 |
)
|
105 |
+
prompt = f"Refactor this Python code:\n{code}"
|
106 |
+
output = refactor_pipe(prompt, max_length=1000)[0]["generated_text"]
|
107 |
+
return output
|
108 |
+
except Exception as e:
|
109 |
+
logger.error(f"Error in execute_refactoring_codet5 function: {e}")
|
110 |
+
return "Error: Unable to refactor code."
|
111 |
+
|
112 |
+
# Chat interface with agent
|
113 |
+
def chat_interface_with_agent(input_text, agent_name, selected_model):
|
114 |
+
"""
|
115 |
+
Handles interaction with the selected AI agent.
|
116 |
+
"""
|
117 |
+
agent = load_agent_from_file(agent_name)
|
118 |
+
if not agent:
|
119 |
+
return f"Agent {agent_name} not found."
|
120 |
|
121 |
+
agent.pipeline = available_models[selected_model]
|
122 |
+
agent_prompt = agent.create_agent_prompt()
|
123 |
+
full_prompt = f"{agent_prompt}\n\nUser: {input_text}\nAgent:"
|
124 |
|
125 |
+
try:
|
126 |
+
response = agent.generate_response(full_prompt)
|
127 |
+
except Exception as e:
|
128 |
+
logger.error(f"Error generating agent response: {e}")
|
129 |
+
response = "Error: Unable to process your request."
|
130 |
|
131 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
+
# Available models
|
134 |
+
available_models = {
|
135 |
+
"Code Generation & Completion": {
|
136 |
+
"Salesforce CodeGen-350M (Mono)": pipeline("text-generation", model="Salesforce/codegen-350M-mono"),
|
137 |
+
"BigCode StarCoder": pipeline("text-generation", model="bigcode/starcoder"),
|
138 |
+
"Mixtral-8x7B-v0.1": pipeline("text-generation", model="mistralai/Mixtral-8x7B-v0.1"),
|
139 |
+
"CodeGPT-small-py": pipeline("text-generation", model="microsoft/CodeGPT-small-py"),
|
140 |
+
"PolyCoder-2.7B": pipeline("text-generation", model="NinedayWang/PolyCoder-2.7B"),
|
141 |
+
"InCoder-1B": pipeline("text-generation", model="facebook/incoder-1B"),
|
142 |
+
#... more code generation models
|
143 |
+
},
|
144 |
+
"Code Translation": {
|
145 |
+
"Python to JavaScript": (lambda code, pipe=pipeline("translation", model="transformersbook/codeparrot-translation-en-java"): execute_translation(code, "javascript", pipe), []), # Pipeline for Python to JavaScript
|
146 |
+
"Python to C++": (lambda code, pipe=pipeline("text-generation", model="konodyuk/codeparrot-small-trans-py-cpp"): execute_translation(code, "cpp", pipe), []), # Pipeline for Python to C++
|
147 |
+
#... more language pairs
|
148 |
+
},
|
149 |
+
#... other categories
|
150 |
+
}
|
151 |
+
|
152 |
+
# Gradio interface with tabs
|
153 |
+
with gr.Blocks(title="AI Power Tools for Developers") as demo:
|
154 |
+
# --- State ---
|
155 |
+
code = gr.State("") # Use gr.State to store code across tabs
|
156 |
+
task_dropdown = gr.State(list(available_models.keys())[0]) # Initialize task dropdown
|
157 |
+
model_dropdown = gr.State(
|
158 |
+
list(available_models[task_dropdown.value].keys())[0]
|
159 |
+
) # Initialize model dropdown
|
160 |
+
|
161 |
+
def update_model_dropdown(selected_task):
|
162 |
+
models_for_task = list(available_models[selected_task].keys())
|
163 |
+
return gr.Dropdown.update(choices=models_for_task)
|
164 |
+
|
165 |
+
with gr.Tab("Chat & Code"):
|
166 |
+
chatbot = gr.Chatbot(elem_id="chatbot")
|
167 |
+
msg = gr.Textbox(label="Enter your message", placeholder="Type your message here...")
|
168 |
+
clear = gr.ClearButton([msg, chatbot])
|
169 |
+
|
170 |
+
def user(message, history):
|
171 |
+
if is_code(message):
|
172 |
+
response = "" # Initialize response
|
173 |
+
task = message.split()[0].lower() # Extract task keyword
|
174 |
+
|
175 |
+
# Use the selected model or a default one
|
176 |
+
model_category = task_dropdown.value
|
177 |
+
model_name = model_dropdown.value
|
178 |
+
pipeline = load_pipeline(model_category, model_name)
|
179 |
+
|
180 |
+
if task in agents:
|
181 |
+
agent = load_agent_from_file(task)
|
182 |
+
try:
|
183 |
+
response = agent.generate_response(message)
|
184 |
+
except Exception as e:
|
185 |
+
logger.error(f"Error executing agent {task}: {e}")
|
186 |
+
response = f"Error executing agent {task}: {e}"
|
187 |
+
else:
|
188 |
+
response = "Invalid command or task not found."
|
189 |
+
else:
|
190 |
+
# Process as natural language request
|
191 |
+
response = pipe(message, max_length=1000)[0]["generated_text"]
|
192 |
+
|
193 |
+
return response, history + [(message, response)]
|
194 |
+
|
195 |
+
msg.change(user, inputs=[msg, chatbot], outputs=[chatbot, chatbot])
|
196 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
197 |
+
|
198 |
+
# Model Selection Tab
|
199 |
+
with gr.Tab("Model Selection"):
|
200 |
+
task_dropdown.render()
|
201 |
+
model_dropdown.render()
|
202 |
+
task_dropdown.change(update_model_dropdown, task_dropdown, model_dropdown)
|
203 |
+
|
204 |
+
# Workspace Tab
|
205 |
+
with gr.Tab("Workspace"):
|
206 |
+
with gr.Row():
|
207 |
+
with gr.Column():
|
208 |
+
code.render()
|
209 |
+
file_output = gr.File(label="Save File As...", interactive=False)
|
210 |
+
with gr.Column():
|
211 |
+
output = gr.Textbox(label="Output")
|
212 |
+
|
213 |
+
run_btn = gr.Button(value="Run Code")
|
214 |
+
upload_btn = gr.UploadButton("Upload Python File", file_types=[".py"])
|
215 |
+
save_button = gr.Button(value="Save Code")
|
216 |
+
|
217 |
+
def run_code(code_str):
|
218 |
+
try:
|
219 |
+
# Save code to a temporary file
|
220 |
+
with open("temp_code.py", "w") as f:
|
221 |
+
f.write(code_str)
|
222 |
+
|
223 |
+
# Execute the code using subprocess
|
224 |
+
process = subprocess.Popen(["python", "temp_code.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
225 |
+
output, error = process.communicate()
|
226 |
+
|
227 |
+
# Return the output and error messages
|
228 |
+
if error:
|
229 |
+
return "Error: " + error.decode("utf-8")
|
230 |
+
else:
|
231 |
+
return output.decode("utf-8")
|
232 |
+
|
233 |
+
except Exception as e:
|
234 |
+
logger.error(f"Error running code: {e}")
|
235 |
+
return f"Error running code: {e}"
|
236 |
+
|
237 |
+
def upload_file(file):
|
238 |
+
with open("uploaded_code.py", "wb") as f:
|
239 |
+
f.write(file.file.getvalue())
|
240 |
+
return "File uploaded successfully!"
|
241 |
+
|
242 |
+
def save_code(code_str):
|
243 |
+
file_output.value = code_str
|
244 |
+
return file_output
|
245 |
+
|
246 |
+
run_btn.click(run_code, inputs=[code], outputs=[output])
|
247 |
+
upload_btn.click(upload_file, inputs=[upload_btn], outputs=[output])
|
248 |
+
save_button.click(save_code, inputs=[code], outputs=[file_output])
|
249 |
+
|
250 |
+
demo.launch()
|