Spaces:
Sleeping
Sleeping
acecalisto3
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,15 @@ from io import StringIO
|
|
7 |
import os
|
8 |
import json
|
9 |
from streamlit_ace import st_ace
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
# Set Hugging Face repository URL and project root path
|
12 |
HUGGING_FACE_REPO_URL = "https://huggingface.co/spaces/acecalisto3/Mistri"
|
@@ -28,6 +37,16 @@ if 'current_state' not in st.session_state:
|
|
28 |
'workspace_chat': {}
|
29 |
}
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
# Define AIAgent class
|
32 |
class AIAgent:
|
33 |
def __init__(self, name, description, skills):
|
@@ -47,6 +66,13 @@ I am confident that I can leverage my expertise to assist you in developing and
|
|
47 |
def autonomous_build(self, chat_history, workspace_projects):
|
48 |
"""
|
49 |
Autonomous build logic based on chat history and workspace projects.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
"""
|
51 |
summary = "Chat History:\n" + '\n'.join([f"User: {u}\nAgent: {a}" for u, a in chat_history])
|
52 |
summary += "\n\nWorkspace Projects:\n" + '\n'.join([f"{p}: {', '.join(ws_projects.keys())}" for p, ws_projects in workspace_projects.items()])
|
@@ -97,34 +123,48 @@ def create_agent_from_text(name, text):
|
|
97 |
|
98 |
# Chat interface using a selected agent
|
99 |
def chat_interface_with_agent(input_text, agent_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
agent_prompt = load_agent_prompt(agent_name)
|
101 |
if agent_prompt is None:
|
102 |
return f"Agent {agent_name} not found."
|
103 |
|
104 |
-
# Load GPT-2 model
|
105 |
-
model_name = "gpt2"
|
106 |
-
try:
|
107 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
108 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
109 |
-
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
110 |
-
except EnvironmentError as e:
|
111 |
-
return f"Error loading model: {e}"
|
112 |
-
|
113 |
# Combine agent prompt with user input
|
114 |
combined_input = f"{agent_prompt}\n\nUser: {input_text}\nAgent:"
|
115 |
|
116 |
-
# Truncate input text for model length limit
|
117 |
-
max_input_length = 900
|
118 |
-
input_ids = tokenizer.encode(combined_input, return_tensors="pt")
|
119 |
-
if input_ids.shape[1] > max_input_length:
|
120 |
-
input_ids = input_ids[:, :max_input_length]
|
121 |
-
|
122 |
# Generate chatbot response
|
123 |
-
chatbot_response =
|
124 |
return chatbot_response
|
125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
# Workspace interface for creating projects
|
127 |
def workspace_interface(project_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
project_path = os.path.join(PROJECT_ROOT, project_name)
|
129 |
if not os.path.exists(PROJECT_ROOT):
|
130 |
os.makedirs(PROJECT_ROOT)
|
@@ -138,6 +178,15 @@ def workspace_interface(project_name):
|
|
138 |
|
139 |
# Function to add code to the workspace
|
140 |
def add_code_to_workspace(project_name, code, file_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
project_path = os.path.join(PROJECT_ROOT, project_name)
|
142 |
if os.path.exists(project_path):
|
143 |
file_path = os.path.join(project_path, file_name)
|
@@ -152,6 +201,14 @@ def add_code_to_workspace(project_name, code, file_name):
|
|
152 |
|
153 |
# Terminal interface with optional project context
|
154 |
def terminal_interface(command, project_name=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
if project_name:
|
156 |
project_path = os.path.join(PROJECT_ROOT, project_name)
|
157 |
if not os.path.exists(project_path):
|
@@ -165,9 +222,16 @@ def terminal_interface(command, project_name=None):
|
|
165 |
else:
|
166 |
st.session_state.current_state['toolbox']['terminal_output'] = result.stderr
|
167 |
return result.stderr
|
168 |
-
|
169 |
# Code editor interface for formatting and linting
|
170 |
def code_editor_interface(code):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
try:
|
172 |
formatted_code = black.format_str(code, mode=black.FileMode())
|
173 |
except black.NothingChanged:
|
@@ -185,6 +249,13 @@ def code_editor_interface(code):
|
|
185 |
|
186 |
# Function to summarize text using a summarization pipeline
|
187 |
def summarize_text(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
summarizer = pipeline("summarization")
|
189 |
summary = summarizer(text, max_length=50, min_length=25, do_sample=False)
|
190 |
st.session_state.current_state['toolbox']['summary'] = summary[0]['summary_text']
|
@@ -192,13 +263,29 @@ def summarize_text(text):
|
|
192 |
|
193 |
# Function to perform sentiment analysis using a sentiment analysis pipeline
|
194 |
def sentiment_analysis(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
analyzer = pipeline("sentiment-analysis")
|
196 |
sentiment = analyzer(text)
|
197 |
st.session_state.current_state['toolbox']['sentiment'] = sentiment[0]
|
198 |
return sentiment[0]
|
199 |
|
200 |
-
# Function to translate code using the
|
201 |
def translate_code(code, input_language, output_language):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
# Define a dictionary to map programming languages to their corresponding file extensions
|
203 |
language_extensions = {
|
204 |
"Python": ".py",
|
@@ -218,31 +305,25 @@ def translate_code(code, input_language, output_language):
|
|
218 |
input_extension = language_extensions[input_language]
|
219 |
output_extension = language_extensions[output_language]
|
220 |
|
221 |
-
# Translate the code using the
|
222 |
-
|
223 |
-
response = openai.ChatCompletion.create(
|
224 |
-
model="gpt-4",
|
225 |
-
messages=[
|
226 |
-
{"role": "system", "content": "You are an expert software developer."},
|
227 |
-
{"role": "user", "content": prompt}
|
228 |
-
]
|
229 |
-
)
|
230 |
-
translated_code = response.choices[0].message['content'].strip()
|
231 |
|
232 |
# Return the translated code
|
233 |
st.session_state.current_state['toolbox']['translated_code'] = translated_code
|
234 |
return translated_code
|
235 |
|
236 |
-
# Function to generate code based on a code idea using the
|
237 |
def generate_code(code_idea):
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
|
|
|
|
246 |
st.session_state.current_state['toolbox']['generated_code'] = generated_code
|
247 |
return generated_code
|
248 |
|
@@ -355,7 +436,7 @@ elif app_mode == "Tool Box":
|
|
355 |
}
|
356 |
for command_name, command in preset_commands.items():
|
357 |
st.write(f"{command_name}: `{command}`")
|
358 |
-
|
359 |
# Workspace Chat App
|
360 |
elif app_mode == "Workspace Chat App":
|
361 |
st.header("Workspace Chat App")
|
|
|
7 |
import os
|
8 |
import json
|
9 |
from streamlit_ace import st_ace
|
10 |
+
from agent import (
|
11 |
+
AppType,
|
12 |
+
createLlamaPrompt,
|
13 |
+
createSpace,
|
14 |
+
isPythonOrGradioAppPrompt,
|
15 |
+
isReactAppPrompt,
|
16 |
+
isStreamlitAppPrompt,
|
17 |
+
generateFiles,
|
18 |
+
)
|
19 |
|
20 |
# Set Hugging Face repository URL and project root path
|
21 |
HUGGING_FACE_REPO_URL = "https://huggingface.co/spaces/acecalisto3/Mistri"
|
|
|
37 |
'workspace_chat': {}
|
38 |
}
|
39 |
|
40 |
+
# Load Hugging Face models for code generation, translation, and conversation
|
41 |
+
try:
|
42 |
+
code_generator = pipeline("text-generation", model="Salesforce/codegen-350M-mono")
|
43 |
+
translator = pipeline("translation_xx_to_yy", model="Helsinki-NLP/opus-mt-en-fr") # Replace with appropriate language pair
|
44 |
+
conversational_model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
45 |
+
conversational_tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
46 |
+
conversational_generator = pipeline("text-generation", model=conversational_model, tokenizer=conversational_tokenizer)
|
47 |
+
except EnvironmentError as e:
|
48 |
+
st.error(f"Error loading Hugging Face models: {e}")
|
49 |
+
|
50 |
# Define AIAgent class
|
51 |
class AIAgent:
|
52 |
def __init__(self, name, description, skills):
|
|
|
66 |
def autonomous_build(self, chat_history, workspace_projects):
|
67 |
"""
|
68 |
Autonomous build logic based on chat history and workspace projects.
|
69 |
+
This function analyzes the chat history and workspace projects to determine the next steps in the development process.
|
70 |
+
It uses sentiment analysis to gauge the user's satisfaction and summarization to extract key information.
|
71 |
+
Args:
|
72 |
+
chat_history (list): A list of tuples containing user input and agent responses.
|
73 |
+
workspace_projects (dict): A dictionary of projects and their associated files.
|
74 |
+
Returns:
|
75 |
+
tuple: A tuple containing a summary of the current state and the suggested next step.
|
76 |
"""
|
77 |
summary = "Chat History:\n" + '\n'.join([f"User: {u}\nAgent: {a}" for u, a in chat_history])
|
78 |
summary += "\n\nWorkspace Projects:\n" + '\n'.join([f"{p}: {', '.join(ws_projects.keys())}" for p, ws_projects in workspace_projects.items()])
|
|
|
123 |
|
124 |
# Chat interface using a selected agent
|
125 |
def chat_interface_with_agent(input_text, agent_name):
|
126 |
+
"""
|
127 |
+
Provides a chat interface using a selected AI agent.
|
128 |
+
Loads the agent's prompt and uses a conversational model to generate responses.
|
129 |
+
Args:
|
130 |
+
input_text (str): The user's input text.
|
131 |
+
agent_name (str): The name of the selected AI agent.
|
132 |
+
Returns:
|
133 |
+
str: The AI agent's response.
|
134 |
+
"""
|
135 |
agent_prompt = load_agent_prompt(agent_name)
|
136 |
if agent_prompt is None:
|
137 |
return f"Agent {agent_name} not found."
|
138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
# Combine agent prompt with user input
|
140 |
combined_input = f"{agent_prompt}\n\nUser: {input_text}\nAgent:"
|
141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
# Generate chatbot response
|
143 |
+
chatbot_response = conversational_generator(combined_input, max_length=150, min_length=30, do_sample=True)[0]['generated_text']
|
144 |
return chatbot_response
|
145 |
|
146 |
+
# Chat interface (default)
|
147 |
+
def chat_interface(input_text):
|
148 |
+
"""
|
149 |
+
Provides a general chat interface using a conversational model.
|
150 |
+
Args:
|
151 |
+
input_text (str): The user's input text.
|
152 |
+
Returns:
|
153 |
+
str: The chatbot's response.
|
154 |
+
"""
|
155 |
+
# Generate response
|
156 |
+
response = conversational_generator(input_text, max_length=150, min_length=30, do_sample=True)[0]['generated_text']
|
157 |
+
return response
|
158 |
+
|
159 |
# Workspace interface for creating projects
|
160 |
def workspace_interface(project_name):
|
161 |
+
"""
|
162 |
+
Creates a new project workspace.
|
163 |
+
Args:
|
164 |
+
project_name (str): The name of the project.
|
165 |
+
Returns:
|
166 |
+
str: A message indicating the status of the project creation.
|
167 |
+
"""
|
168 |
project_path = os.path.join(PROJECT_ROOT, project_name)
|
169 |
if not os.path.exists(PROJECT_ROOT):
|
170 |
os.makedirs(PROJECT_ROOT)
|
|
|
178 |
|
179 |
# Function to add code to the workspace
|
180 |
def add_code_to_workspace(project_name, code, file_name):
|
181 |
+
"""
|
182 |
+
Adds code to a specified file in a project workspace.
|
183 |
+
Args:
|
184 |
+
project_name (str): The name of the project.
|
185 |
+
code (str): The code to be added.
|
186 |
+
file_name (str): The name of the file.
|
187 |
+
Returns:
|
188 |
+
str: A message indicating the status of the code addition.
|
189 |
+
"""
|
190 |
project_path = os.path.join(PROJECT_ROOT, project_name)
|
191 |
if os.path.exists(project_path):
|
192 |
file_path = os.path.join(project_path, file_name)
|
|
|
201 |
|
202 |
# Terminal interface with optional project context
|
203 |
def terminal_interface(command, project_name=None):
|
204 |
+
"""
|
205 |
+
Executes a terminal command with optional project context.
|
206 |
+
Args:
|
207 |
+
command (str): The terminal command to execute.
|
208 |
+
project_name (str, optional): The name of the project to execute the command in. Defaults to None.
|
209 |
+
Returns:
|
210 |
+
str: The output of the terminal command.
|
211 |
+
"""
|
212 |
if project_name:
|
213 |
project_path = os.path.join(PROJECT_ROOT, project_name)
|
214 |
if not os.path.exists(project_path):
|
|
|
222 |
else:
|
223 |
st.session_state.current_state['toolbox']['terminal_output'] = result.stderr
|
224 |
return result.stderr
|
225 |
+
|
226 |
# Code editor interface for formatting and linting
|
227 |
def code_editor_interface(code):
|
228 |
+
"""
|
229 |
+
Provides a code editor interface with formatting and linting capabilities.
|
230 |
+
Args:
|
231 |
+
code (str): The code to be edited.
|
232 |
+
Returns:
|
233 |
+
tuple: A tuple containing the formatted code and any linting messages.
|
234 |
+
"""
|
235 |
try:
|
236 |
formatted_code = black.format_str(code, mode=black.FileMode())
|
237 |
except black.NothingChanged:
|
|
|
249 |
|
250 |
# Function to summarize text using a summarization pipeline
|
251 |
def summarize_text(text):
|
252 |
+
"""
|
253 |
+
Summarizes a given text using a Hugging Face summarization pipeline.
|
254 |
+
Args:
|
255 |
+
text (str): The text to be summarized.
|
256 |
+
Returns:
|
257 |
+
str: The summarized text.
|
258 |
+
"""
|
259 |
summarizer = pipeline("summarization")
|
260 |
summary = summarizer(text, max_length=50, min_length=25, do_sample=False)
|
261 |
st.session_state.current_state['toolbox']['summary'] = summary[0]['summary_text']
|
|
|
263 |
|
264 |
# Function to perform sentiment analysis using a sentiment analysis pipeline
|
265 |
def sentiment_analysis(text):
|
266 |
+
"""
|
267 |
+
Performs sentiment analysis on a given text using a Hugging Face sentiment analysis pipeline.
|
268 |
+
Args:
|
269 |
+
text (str): The text to be analyzed.
|
270 |
+
Returns:
|
271 |
+
dict: The sentiment analysis result.
|
272 |
+
"""
|
273 |
analyzer = pipeline("sentiment-analysis")
|
274 |
sentiment = analyzer(text)
|
275 |
st.session_state.current_state['toolbox']['sentiment'] = sentiment[0]
|
276 |
return sentiment[0]
|
277 |
|
278 |
+
# Function to translate code using the Hugging Face API
|
279 |
def translate_code(code, input_language, output_language):
|
280 |
+
"""
|
281 |
+
Translates code from one programming language to another using a Hugging Face translation pipeline.
|
282 |
+
Args:
|
283 |
+
code (str): The code to be translated.
|
284 |
+
input_language (str): The source programming language.
|
285 |
+
output_language (str): The target programming language.
|
286 |
+
Returns:
|
287 |
+
str: The translated code.
|
288 |
+
"""
|
289 |
# Define a dictionary to map programming languages to their corresponding file extensions
|
290 |
language_extensions = {
|
291 |
"Python": ".py",
|
|
|
305 |
input_extension = language_extensions[input_language]
|
306 |
output_extension = language_extensions[output_language]
|
307 |
|
308 |
+
# Translate the code using the Hugging Face API
|
309 |
+
translated_code = translator(code, max_length=1024)[0]['translation_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
310 |
|
311 |
# Return the translated code
|
312 |
st.session_state.current_state['toolbox']['translated_code'] = translated_code
|
313 |
return translated_code
|
314 |
|
315 |
+
# Function to generate code based on a code idea using the Hugging Face API
|
316 |
def generate_code(code_idea):
|
317 |
+
"""
|
318 |
+
Generates code based on a given code idea using a Hugging Face code generation pipeline.
|
319 |
+
Args:
|
320 |
+
code_idea (str): The code idea or description.
|
321 |
+
Returns:
|
322 |
+
str: The generated code.
|
323 |
+
"""
|
324 |
+
# Generate code using the Hugging Face API
|
325 |
+
generated_code = code_generator(f"```python\n{code_idea}\n```", max_length=512)[0]['generated_text']
|
326 |
+
|
327 |
st.session_state.current_state['toolbox']['generated_code'] = generated_code
|
328 |
return generated_code
|
329 |
|
|
|
436 |
}
|
437 |
for command_name, command in preset_commands.items():
|
438 |
st.write(f"{command_name}: `{command}`")
|
439 |
+
|
440 |
# Workspace Chat App
|
441 |
elif app_mode == "Workspace Chat App":
|
442 |
st.header("Workspace Chat App")
|