0shotTest / app.py
acecalisto3's picture
Update app.py
80dbb6b verified
raw
history blame
8.85 kB
# config.py
import os
from huggingface_hub import InferenceClient
from transformers import pipeline
# Initialize clients and models
MIXTRAL_CLIENT = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
LLAMA_PIPELINE = pipeline("text-generation", model="bartowski/Llama-3-8B-Instruct-Coder-GGUF")
AGENTS = [
"WEB_DEV",
"AI_SYSTEM_PROMPT",
"PYTHON_CODE_DEV",
"CODE_REVIEW_ASSISTANT",
"CONTENT_WRITER_EDITOR",
"QUESTION_GENERATOR",
"HUGGINGFACE_FILE_DEV",
]
# ai_agent.py
import random
from typing import List, Dict, Tuple
class AIAgent:
def __init__(self, name: str, description: str, skills: List[str]):
self.name = name
self.description = description
self.skills = skills
def create_agent_prompt(self) -> str:
skills_str = '\n'.join([f"* {skill}" for skill in self.skills])
return f"""
As an elite expert developer, my name is {self.name}.
I possess a comprehensive understanding of the following areas:
{skills_str}
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.
"""
def autonomous_build(self, chat_history: List[Tuple[str, str]], workspace_projects: Dict[str, Dict]) -> Tuple[str, str]:
summary = "Chat History:\n" + "\n".join([f"User: {u}\nAgent: {a}" for u, a in chat_history])
summary += "\n\nWorkspace Projects:\n" + "\n".join([f"{p}: {details}" for p, details in workspace_projects.items()])
next_step = "Based on the current state, the next logical step is to implement the main application logic."
return summary, next_step
# utils.py
import os
import subprocess
from typing import List, Tuple
def format_prompt(message: str, history: List[Tuple[str, str]]) -> str:
prompt = "<s>"
for user_prompt, bot_response in history:
prompt += f"[INST] {user_prompt} [/INST] {bot_response}</s> "
prompt += f"[INST] {message} [/INST]"
return prompt
def generate(prompt: str, history: List[Tuple[str, str]], agent_name: str = AGENTS[0], sys_prompt: str = "",
temperature: float = 0.9, max_new_tokens: int = 256, top_p: float = 0.95, repetition_penalty: float = 1.0):
seed = random.randint(1, 1111111111111111)
generate_kwargs = dict(
temperature=temperature,
max_new_tokens=max_new_tokens,
top_p=top_p,
repetition_penalty=repetition_penalty,
do_sample=True,
seed=seed,
)
formatted_prompt = format_prompt(f"{sys_prompt}, {prompt}", history)
stream = MIXTRAL_CLIENT.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
output = ""
for response in stream:
output += response.token.text
yield output
return output
def terminal_interface(command: str, project_name: str) -> str:
try:
result = subprocess.run(command, shell=True, capture_output=True, text=True, cwd=project_name)
return result.stdout if result.returncode == 0 else result.stderr
except Exception as e:
return str(e)
def add_code_to_workspace(project_name: str, code: str, file_name: str) -> str:
project_path = os.path.join(os.getcwd(), project_name)
os.makedirs(project_path, exist_ok=True)
file_path = os.path.join(project_path, file_name)
with open(file_path, 'w') as file:
file.write(code)
return f"Added {file_name} to {project_name}"
# main.py
import streamlit as st
import gradio as gr
from config import AGENTS
from ai_agent import AIAgent
from utils import generate, terminal_interface, add_code_to_workspace
# Streamlit UI
def main():
st.title("DevToolKit: AI-Powered Development Environment")
# Project Management
st.header("Project Management")
project_name = st.text_input("Enter project name:")
if st.button("Create Project"):
if project_name not in st.session_state.workspace_projects:
st.session_state.workspace_projects[project_name] = {'files': []}
st.success(f"Created project: {project_name}")
else:
st.warning(f"Project {project_name} already exists")
# Code Addition
st.subheader("Add Code to Workspace")
code_to_add = st.text_area("Enter code to add to workspace:")
file_name = st.text_input("Enter file name (e.g. 'app.py'):")
if st.button("Add Code"):
add_code_status = add_code_to_workspace(project_name, code_to_add, file_name)
st.success(add_code_status)
# Terminal Interface
st.subheader("Terminal (Workspace Context)")
terminal_input = st.text_input("Enter a command within the workspace:")
if st.button("Run Command"):
terminal_output = terminal_interface(terminal_input, project_name)
st.code(terminal_output, language="bash")
# Chat Interface
st.subheader("Chat with DevToolKit for Guidance")
chat_input = st.text_area("Enter your message for guidance:")
if st.button("Get Guidance"):
chat_response = next(generate(chat_input, st.session_state.chat_history))
st.session_state.chat_history.append((chat_input, chat_response))
st.write(f"DevToolKit: {chat_response}")
# Display Chat History
st.subheader("Chat History")
for user_input, response in st.session_state.chat_history:
st.write(f"User: {user_input}")
st.write(f"DevToolKit: {response}")
# Display Terminal History
st.subheader("Terminal History")
for command, output in st.session_state.terminal_history:
st.write(f"Command: {command}")
st.code(output, language="bash")
# Display Projects and Files
st.subheader("Workspace Projects")
for project, details in st.session_state.workspace_projects.items():
st.write(f"Project: {project}")
for file in details['files']:
st.write(f" - {file}")
# Chat with AI Agents
st.subheader("Chat with AI Agents")
selected_agent = st.selectbox("Select an AI agent", AGENTS)
agent_chat_input = st.text_area("Enter your message for the agent:")
if st.button("Send to Agent"):
agent_chat_response = next(generate(agent_chat_input, st.session_state.chat_history, agent_name=selected_agent))
st.session_state.chat_history.append((agent_chat_input, agent_chat_response))
st.write(f"{selected_agent}: {agent_chat_response}")
# Automate Build Process
st.subheader("Automate Build Process")
if st.button("Automate"):
agent = AIAgent(selected_agent, "", []) # Load the agent without skills for now
summary, next_step = agent.autonomous_build(st.session_state.chat_history, st.session_state.workspace_projects)
st.write("Autonomous Build Summary:")
st.write(summary)
st.write("Next Step:")
st.write(next_step)
# Display current state for debugging
st.sidebar.subheader("Current State")
st.sidebar.json(st.session_state.current_state)
if __name__ == "__main__":
main()
# gradio_interface.py
import gradio as gr
from config import AGENTS
from utils import generate
def create_gradio_interface():
additional_inputs = [
gr.Dropdown(label="Agents", choices=[s for s in AGENTS], value=AGENTS[0], interactive=True),
gr.Textbox(label="System Prompt", max_lines=1, interactive=True),
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"),
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"),
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"),
gr.Slider(label="Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05, interactive=True, info="Penalize repeated tokens"),
]
examples = [
["Create a simple web application using Flask", AGENTS[0], None, None, None, None],
["Generate a Python script to perform a linear regression analysis", AGENTS[2], None, None, None, None],
["Create a Dockerfile for a Node.js application", AGENTS[1], None, None, None, None],
]
return gr.ChatInterface(
fn=generate,
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
additional_inputs=additional_inputs,
title="DevToolKit AI Assistant",
examples=examples,
concurrency_limit=20,
)
if __name__ == "__main__":
interface = create_gradio_interface()
interface.launch(show_api=True)