acecalisto3 commited on
Commit
23773dc
·
verified ·
1 Parent(s): a1d0b45

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -94
app.py CHANGED
@@ -1,26 +1,29 @@
1
  import os
2
- from huggingface_hub import InferenceClient
3
- import gradio as gr
4
- import random
5
- from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
6
- import subprocess
7
- import threading
8
- import time
9
  import json
 
 
 
 
10
  import streamlit as st
 
 
 
 
 
 
11
 
12
- # Initialize the session state
13
- if 'current_state' not in st.session_state:
14
- st.session_state.current_state = None
15
- # Initialize the InferenceClient for Mixtral-8x7B-Instruct-v0.1
16
- client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
 
17
 
18
- # Load the model and tokenizer from a different repository
19
- model_name = "bigscience/bloom-1b7"
20
- model = AutoModelForCausalLM.from_pretrained(model_name)
21
- tokenizer = AutoTokenizer.from_pretrained(model_name)
22
 
23
- # Define the agents
24
  agents = {
25
  "WEB_DEV": {
26
  "description": "Expert in web development technologies and frameworks.",
@@ -59,36 +62,19 @@ agents = {
59
  },
60
  }
61
 
62
- class AIAgent:
63
- def __init__(self, name, description, skills, system_prompt):
64
- self.name = name
65
- self.description = description
66
- self.skills = skills
67
- self.system_prompt = system_prompt
68
- self.active = False
69
-
70
- def activate(self):
71
- self.active = True
72
-
73
- def deactivate(self):
74
- self.active = False
75
-
76
- def create_agent_prompt(self):
77
- skills_str = '\n'.join([f"* {skill}" for skill in self.skills])
78
- agent_prompt = f"""
79
- As an elite expert developer, my name is {self.name}. I possess a comprehensive understanding of the following areas:
80
- {skills_str}
81
- 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.
82
- """
83
- return agent_prompt
84
-
85
- def autonomous_build(self, chat_history, workspace_projects):
86
- summary = "Chat History:\n" + "\n".join([f"User: {u}\nAgent: {a}" for u, a in chat_history])
87
- summary += "\n\nWorkspace Projects:\n" + "\n".join([f"{p}: {details}" for p, details in workspace_projects.items()])
88
- next_step = "Based on the current state, the next logical step is to implement the main application logic."
89
- return summary, next_step
90
-
91
- def format_prompt(message, history, agent_prompt):
92
  prompt = "<s>"
93
  for user_prompt, bot_response in history:
94
  prompt += f"[INST] {user_prompt} [/INST]"
@@ -96,41 +82,44 @@ def format_prompt(message, history, agent_prompt):
96
  prompt += f"[INST] {agent_prompt}, {message} [/INST]"
97
  return prompt
98
 
99
- def generate(prompt, history, agent_name, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
100
- seed = random.randint(1, 1111111111111111)
101
  agent = agents[agent_name]
102
  system_prompt = agent["system_prompt"]
103
-
104
  generate_kwargs = dict(
105
- temperature=float(temperature),
106
- max_new_tokens=max_new_tokens,
107
- top_p=top_p,
108
- repetition_penalty=repetition_penalty,
109
  do_sample=True,
110
- seed=seed,
111
  )
112
-
113
- formatted_prompt = format_prompt(prompt, history, system_prompt)
114
- input_ids = tokenizer.encode(formatted_prompt, return_tensors="pt")
115
  output = model.generate(input_ids, **generate_kwargs)
116
  response = tokenizer.decode(output[0], skip_special_tokens=True)
117
  return response
118
 
119
- def chat_interface(chat_input, agent_name):
120
- if agents[agent_name].active:
121
- response = generate(chat_input, st.session_state.chat_history, agent_name)
122
- return response
 
 
 
 
 
123
  else:
124
- return "Agent is not active. Please activate the agent."
125
 
126
- def terminal_interface(command, project_name):
 
127
  try:
128
  result = subprocess.run(command, shell=True, capture_output=True, text=True, cwd=project_name)
129
  return result.stdout if result.returncode == 0 else result.stderr
130
  except Exception as e:
131
  return str(e)
132
 
133
- def add_code_to_workspace(project_name, code, file_name):
 
134
  project_path = os.path.join(os.getcwd(), project_name)
135
  if not os.path.exists(project_path):
136
  os.makedirs(project_path)
@@ -142,10 +131,49 @@ def add_code_to_workspace(project_name, code, file_name):
142
  st.session_state.workspace_projects[project_name]['files'].append(file_name)
143
  return f"Added {file_name} to {project_name}"
144
 
145
- # Streamlit UI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  st.title("DevToolKit: AI-Powered Development Environment")
147
 
148
- # Project Management
149
  st.header("Project Management")
150
  project_name = st.text_input("Enter project name:")
151
  if st.button("Create Project"):
@@ -155,7 +183,7 @@ if st.button("Create Project"):
155
  else:
156
  st.warning(f"Project {project_name} already exists")
157
 
158
- # Code Addition
159
  st.subheader("Add Code to Workspace")
160
  code_to_add = st.text_area("Enter code to add to workspace:")
161
  file_name = st.text_input("Enter file name (e.g. 'app.py'):")
@@ -163,55 +191,58 @@ if st.button("Add Code"):
163
  add_code_status = add_code_to_workspace(project_name, code_to_add, file_name)
164
  st.success(add_code_status)
165
 
166
- # Terminal Interface
167
  st.subheader("Terminal (Workspace Context)")
168
  terminal_input = st.text_input("Enter a command within the workspace:")
169
  if st.button("Run Command"):
170
  terminal_output = terminal_interface(terminal_input, project_name)
171
  st.code(terminal_output, language="bash")
172
 
173
- # Chat Interface
174
  st.subheader("Chat with AI Agents")
175
- selected_agent = st.selectbox("Select an AI agent", list(agents.keys()))
176
- agent_chat_input = st.text_area("Enter your message for the agent:")
177
- if st.button("Send to Agent"):
178
- agent_chat_response = chat_interface(agent_chat_input, selected_agent)
179
- st.session_state.chat_history.append((agent_chat_input, agent_chat_response))
180
- st.write(f"{selected_agent}: {agent_chat_response}")
181
-
182
- # Agent Control
183
  st.subheader("Agent Control")
184
  for agent_name in agents:
185
  agent = agents[agent_name]
186
  with st.expander(f"{agent_name} ({agent['description']})"):
187
- if st.button(f"Activate {agent_name}"):
188
- agent.activate()
189
  st.success(f"{agent_name} activated.")
190
- if st.button(f"Deactivate {agent_name}"):
191
- agent.deactivate()
192
  st.success(f"{agent_name} deactivated.")
193
 
194
- # Automate Build Process
195
  st.subheader("Automate Build Process")
196
  if st.button("Automate"):
197
- # Select the appropriate agent based on the current context
198
- # ...
199
- # Implement the autonomous build process
200
- # ...
201
- pass
202
 
203
- # Display current state for debugging
204
  st.sidebar.subheader("Current State")
205
  st.sidebar.json(st.session_state.current_state)
 
 
 
 
206
 
207
- # Gradio Interface
208
  additional_inputs = [
209
  gr.Dropdown(label="Agents", choices=[s for s in agents.keys()], value=list(agents.keys())[0], interactive=True),
210
  gr.Textbox(label="System Prompt", max_lines=1, interactive=True),
211
- 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"),
212
- 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"),
213
- 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"),
214
- gr.Slider(label="Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05, interactive=True, info="Penalize repeated tokens"),
215
  ]
216
 
217
  examples = [
 
1
  import os
 
 
 
 
 
 
 
2
  import json
3
+ import time
4
+ from typing import Dict, List, Tuple
5
+
6
+ import gradio as gr
7
  import streamlit as st
8
+ from huggingface_hub import InferenceClient
9
+ from transformers import AutoModelForCausalLM, AutoTokenizer
10
+ from rich import print as rprint
11
+ from rich.panel import Panel
12
+ from rich.progress import track
13
+ from rich.table import Table
14
 
15
+ # --- Constants ---
16
+ MODEL_NAME = "bigscience/bloom-1b7" # Choose a suitable model
17
+ MAX_NEW_TOKENS = 1024
18
+ TEMPERATURE = 0.7
19
+ TOP_P = 0.95
20
+ REPETITION_PENALTY = 1.2
21
 
22
+ # --- Model & Tokenizer ---
23
+ model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
24
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
 
25
 
26
+ # --- Agents ---
27
  agents = {
28
  "WEB_DEV": {
29
  "description": "Expert in web development technologies and frameworks.",
 
62
  },
63
  }
64
 
65
+ # --- Session State ---
66
+ if "workspace_projects" not in st.session_state:
67
+ st.session_state.workspace_projects = {}
68
+ if "chat_history" not in st.session_state:
69
+ st.session_state.chat_history = []
70
+ if "active_agent" not in st.session_state:
71
+ st.session_state.active_agent = None
72
+ if "selected_agents" not in st.session_state:
73
+ st.session_state.selected_agents = []
74
+
75
+ # --- Functions ---
76
+ def format_prompt(message: str, history: List[Tuple[str, str]], agent_prompt: str) -> str:
77
+ """Formats the prompt for the language model."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  prompt = "<s>"
79
  for user_prompt, bot_response in history:
80
  prompt += f"[INST] {user_prompt} [/INST]"
 
82
  prompt += f"[INST] {agent_prompt}, {message} [/INST]"
83
  return prompt
84
 
85
+ def generate_response(prompt: str, agent_name: str) -> str:
86
+ """Generates a response from the language model."""
87
  agent = agents[agent_name]
88
  system_prompt = agent["system_prompt"]
 
89
  generate_kwargs = dict(
90
+ temperature=TEMPERATURE,
91
+ max_new_tokens=MAX_NEW_TOKENS,
92
+ top_p=TOP_P,
93
+ repetition_penalty=REPETITION_PENALTY,
94
  do_sample=True,
 
95
  )
96
+ input_ids = tokenizer.encode(prompt, return_tensors="pt")
 
 
97
  output = model.generate(input_ids, **generate_kwargs)
98
  response = tokenizer.decode(output[0], skip_special_tokens=True)
99
  return response
100
 
101
+ def chat_interface(chat_input: str, agent_names: List[str]) -> str:
102
+ """Handles chat interactions with the selected agents."""
103
+ if agent_names:
104
+ responses = []
105
+ for agent_name in agent_names:
106
+ prompt = format_prompt(chat_input, st.session_state.chat_history, agents[agent_name]["system_prompt"])
107
+ response = generate_response(prompt, agent_name)
108
+ responses.append(f"{agent_name}: {response}")
109
+ return "\n".join(responses)
110
  else:
111
+ return "Please select at least one agent."
112
 
113
+ def terminal_interface(command: str, project_name: str) -> str:
114
+ """Executes a command within the specified project directory."""
115
  try:
116
  result = subprocess.run(command, shell=True, capture_output=True, text=True, cwd=project_name)
117
  return result.stdout if result.returncode == 0 else result.stderr
118
  except Exception as e:
119
  return str(e)
120
 
121
+ def add_code_to_workspace(project_name: str, code: str, file_name: str) -> str:
122
+ """Adds code to a workspace project."""
123
  project_path = os.path.join(os.getcwd(), project_name)
124
  if not os.path.exists(project_path):
125
  os.makedirs(project_path)
 
131
  st.session_state.workspace_projects[project_name]['files'].append(file_name)
132
  return f"Added {file_name} to {project_name}"
133
 
134
+ def display_workspace_projects():
135
+ """Displays a table of workspace projects."""
136
+ table = Table(title="Workspace Projects")
137
+ table.add_column("Project Name", style="cyan", no_wrap=True)
138
+ table.add_column("Files", style="magenta")
139
+ for project_name, details in st.session_state.workspace_projects.items():
140
+ table.add_row(project_name, ", ".join(details['files']))
141
+ rprint(Panel(table, title="[bold blue]Workspace Projects[/bold blue]"))
142
+
143
+ def display_chat_history():
144
+ """Displays the chat history in a formatted way."""
145
+ table = Table(title="Chat History")
146
+ table.add_column("User", style="cyan", no_wrap=True)
147
+ table.add_column("Agent", style="magenta")
148
+ for user_prompt, bot_response in st.session_state.chat_history:
149
+ table.add_row(user_prompt, bot_response)
150
+ rprint(Panel(table, title="[bold blue]Chat History[/bold blue]"))
151
+
152
+ def display_agent_info(agent_name: str):
153
+ """Displays information about the selected agent."""
154
+ agent = agents[agent_name]
155
+ table = Table(title=f"{agent_name} - Agent Information")
156
+ table.add_column("Description", style="cyan", no_wrap=True)
157
+ table.add_column("Skills", style="magenta")
158
+ table.add_row(agent["description"], ", ".join(agent["skills"]))
159
+ rprint(Panel(table, title=f"[bold blue]{agent_name} - Agent Information[/bold blue]"))
160
+
161
+ def run_autonomous_build(agent_names: List[str], project_name: str):
162
+ """Runs the autonomous build process."""
163
+ for agent_name in agent_names:
164
+ agent = agents[agent_name]
165
+ chat_history = st.session_state.chat_history
166
+ workspace_projects = st.session_state.workspace_projects
167
+ summary, next_step = agent.autonomous_build(chat_history, workspace_projects)
168
+ rprint(Panel(summary, title="[bold blue]Current State[/bold blue]"))
169
+ rprint(Panel(next_step, title="[bold blue]Next Step[/bold blue]"))
170
+ # Implement logic for autonomous build based on the current state
171
+ # ...
172
+
173
+ # --- Streamlit UI ---
174
  st.title("DevToolKit: AI-Powered Development Environment")
175
 
176
+ # --- Project Management ---
177
  st.header("Project Management")
178
  project_name = st.text_input("Enter project name:")
179
  if st.button("Create Project"):
 
183
  else:
184
  st.warning(f"Project {project_name} already exists")
185
 
186
+ # --- Code Addition ---
187
  st.subheader("Add Code to Workspace")
188
  code_to_add = st.text_area("Enter code to add to workspace:")
189
  file_name = st.text_input("Enter file name (e.g. 'app.py'):")
 
191
  add_code_status = add_code_to_workspace(project_name, code_to_add, file_name)
192
  st.success(add_code_status)
193
 
194
+ # --- Terminal Interface ---
195
  st.subheader("Terminal (Workspace Context)")
196
  terminal_input = st.text_input("Enter a command within the workspace:")
197
  if st.button("Run Command"):
198
  terminal_output = terminal_interface(terminal_input, project_name)
199
  st.code(terminal_output, language="bash")
200
 
201
+ # --- Chat Interface ---
202
  st.subheader("Chat with AI Agents")
203
+ selected_agents = st.multiselect("Select AI agents", list(agents.keys()), key="agent_select")
204
+ st.session_state.selected_agents = selected_agents
205
+ agent_chat_input = st.text_area("Enter your message for the agents:", key="agent_input")
206
+ if st.button("Send to Agents", key="agent_send"):
207
+ agent_chat_response = chat_interface(agent_chat_input, selected_agents)
208
+ st.write(agent_chat_response)
209
+
210
+ # --- Agent Control ---
211
  st.subheader("Agent Control")
212
  for agent_name in agents:
213
  agent = agents[agent_name]
214
  with st.expander(f"{agent_name} ({agent['description']})"):
215
+ if st.button(f"Activate {agent_name}", key=f"activate_{agent_name}"):
216
+ st.session_state.active_agent = agent_name
217
  st.success(f"{agent_name} activated.")
218
+ if st.button(f"Deactivate {agent_name}", key=f"deactivate_{agent_name}"):
219
+ st.session_state.active_agent = None
220
  st.success(f"{agent_name} deactivated.")
221
 
222
+ # --- Automate Build Process ---
223
  st.subheader("Automate Build Process")
224
  if st.button("Automate"):
225
+ if st.session_state.selected_agents:
226
+ run_autonomous_build(st.session_state.selected_agents, project_name)
227
+ else:
228
+ st.warning("Please select at least one agent.")
 
229
 
230
+ # --- Display Information ---
231
  st.sidebar.subheader("Current State")
232
  st.sidebar.json(st.session_state.current_state)
233
+ if st.session_state.active_agent:
234
+ display_agent_info(st.session_state.active_agent)
235
+ display_workspace_projects()
236
+ display_chat_history()
237
 
238
+ # --- Gradio Interface ---
239
  additional_inputs = [
240
  gr.Dropdown(label="Agents", choices=[s for s in agents.keys()], value=list(agents.keys())[0], interactive=True),
241
  gr.Textbox(label="System Prompt", max_lines=1, interactive=True),
242
+ gr.Slider(label="Temperature", value=TEMPERATURE, minimum=0.0, maximum=1.0, step=0.05, interactive=True, info="Higher values produce more diverse outputs"),
243
+ gr.Slider(label="Max new tokens", value=MAX_NEW_TOKENS, minimum=0, maximum=1000*10, step=64, interactive=True, info="The maximum numbers of new tokens"),
244
+ gr.Slider(label="Top-p (nucleus sampling)", value=TOP_P, minimum=0.0, maximum=1, step=0.05, interactive=True, info="Higher values sample more low-probability tokens"),
245
+ gr.Slider(label="Repetition penalty", value=REPETITION_PENALTY, minimum=1.0, maximum=2.0, step=0.05, interactive=True, info="Penalize repeated tokens"),
246
  ]
247
 
248
  examples = [