Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -86,6 +86,9 @@ class AIAgent:
|
|
| 86 |
"READ_PROMPT": self.read_prompt,
|
| 87 |
"TASK_PROMPT": self.task_prompt,
|
| 88 |
"UNDERSTAND_TEST_RESULTS_PROMPT": self.understand_test_results_prompt,
|
|
|
|
|
|
|
|
|
|
| 89 |
}
|
| 90 |
self.task_history: List[Dict[str, str]] = []
|
| 91 |
self.current_task: Optional[str] = None
|
|
@@ -99,6 +102,7 @@ class AIAgent:
|
|
| 99 |
"bigscience/T0_3B",
|
| 100 |
] # Add more as needed
|
| 101 |
self.selected_model = "gpt2" # Default model
|
|
|
|
| 102 |
|
| 103 |
# --- Search Functionality ---
|
| 104 |
def search(self, query: str) -> List[str]:
|
|
@@ -251,6 +255,12 @@ class AIAgent:
|
|
| 251 |
return "Enter the new task to start."
|
| 252 |
elif action == "UNDERSTAND_TEST_RESULTS_PROMPT":
|
| 253 |
return "Enter your question about the test results."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
else:
|
| 255 |
raise InvalidActionError("Please provide a valid action.")
|
| 256 |
except InvalidActionError as e:
|
|
@@ -314,6 +324,39 @@ class AIAgent:
|
|
| 314 |
"""Provides a prompt to understand the test results."""
|
| 315 |
return "What do you want to know about the test results?"
|
| 316 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
# --- Input Handling Functionality ---
|
| 318 |
def handle_input(self, input_str: str):
|
| 319 |
"""Handles user input and executes the corresponding action."""
|
|
@@ -401,64 +444,111 @@ if __name__ == "__main__":
|
|
| 401 |
layout="wide",
|
| 402 |
initial_sidebar_state="expanded",
|
| 403 |
)
|
| 404 |
-
st.title("AI Agent")
|
| 405 |
-
st.sidebar.title("Agent Settings")
|
| 406 |
-
|
| 407 |
-
# --- Command Dropdown ---
|
| 408 |
-
command_options = [
|
| 409 |
-
"SEARCH",
|
| 410 |
-
"CODEGEN",
|
| 411 |
-
"REFINE-CODE",
|
| 412 |
-
"TEST-CODE",
|
| 413 |
-
"INTEGRATE-CODE",
|
| 414 |
-
"TEST-APP",
|
| 415 |
-
"GENERATE-REPORT",
|
| 416 |
-
"WORKSPACE-EXPLORER",
|
| 417 |
-
"ADD_PROMPT",
|
| 418 |
-
"ACTION_PROMPT",
|
| 419 |
-
"COMPRESS_HISTORY_PROMPT",
|
| 420 |
-
"LOG_PROMPT",
|
| 421 |
-
"LOG_RESPONSE",
|
| 422 |
-
"MODIFY_PROMPT",
|
| 423 |
-
"PREFIX",
|
| 424 |
-
"SEARCH_QUERY",
|
| 425 |
-
"READ_PROMPT",
|
| 426 |
-
"TASK_PROMPT",
|
| 427 |
-
"UNDERSTAND_TEST_RESULTS_PROMPT",
|
| 428 |
-
]
|
| 429 |
-
selected_command = st.sidebar.selectbox("Command", command_options)
|
| 430 |
-
|
| 431 |
-
# --- Model Dropdown ---
|
| 432 |
-
selected_model = st.sidebar.selectbox(
|
| 433 |
-
"Model", agent.available_models, index=agent.available_models.index(agent.selected_model)
|
| 434 |
-
)
|
| 435 |
-
agent.selected_model = selected_model
|
| 436 |
-
|
| 437 |
-
# --- Input Field ---
|
| 438 |
-
input_str = st.text_input(f"Enter input for {selected_command}:")
|
| 439 |
-
|
| 440 |
-
# --- Execute Command ---
|
| 441 |
-
if st.button("Execute"):
|
| 442 |
-
if input_str:
|
| 443 |
-
agent.handle_input(f"{selected_command} {input_str}")
|
| 444 |
-
st.write(f"Output: {agent.task_history[-1]['output']}")
|
| 445 |
-
|
| 446 |
-
# --- Task History ---
|
| 447 |
-
st.subheader("Task History")
|
| 448 |
-
for task in agent.task_history:
|
| 449 |
-
st.write(f"**Action:** {task['action']}")
|
| 450 |
-
st.write(f"**Input:** {task['input']}")
|
| 451 |
-
st.write(f"**Output:** {task['output']}")
|
| 452 |
-
|
| 453 |
-
# --- Workspace Explorer ---
|
| 454 |
-
st.subheader("Workspace Explorer")
|
| 455 |
-
with st.expander("Explore Workspace"):
|
| 456 |
-
try:
|
| 457 |
-
workspace_output = agent.workspace_explorer()
|
| 458 |
-
st.write(workspace_output)
|
| 459 |
-
except WorkspaceExplorerError as e:
|
| 460 |
-
st.error(f"Error exploring workspace: {e}")
|
| 461 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 462 |
|
| 463 |
# --- Gradio Integration ---
|
| 464 |
def gradio_interface(input_text):
|
|
|
|
| 86 |
"READ_PROMPT": self.read_prompt,
|
| 87 |
"TASK_PROMPT": self.task_prompt,
|
| 88 |
"UNDERSTAND_TEST_RESULTS_PROMPT": self.understand_test_results_prompt,
|
| 89 |
+
"EXECUTE_COMMAND": self.execute_command, # Add command execution
|
| 90 |
+
"PYTHON_INTERPRET": self.python_interpret, # Add Python interpretation
|
| 91 |
+
"NLP": self.nlp, # Add NLP capabilities
|
| 92 |
}
|
| 93 |
self.task_history: List[Dict[str, str]] = []
|
| 94 |
self.current_task: Optional[str] = None
|
|
|
|
| 102 |
"bigscience/T0_3B",
|
| 103 |
] # Add more as needed
|
| 104 |
self.selected_model = "gpt2" # Default model
|
| 105 |
+
self.nlp_pipeline = None # Initialize NLP pipeline later
|
| 106 |
|
| 107 |
# --- Search Functionality ---
|
| 108 |
def search(self, query: str) -> List[str]:
|
|
|
|
| 255 |
return "Enter the new task to start."
|
| 256 |
elif action == "UNDERSTAND_TEST_RESULTS_PROMPT":
|
| 257 |
return "Enter your question about the test results."
|
| 258 |
+
elif action == "EXECUTE_COMMAND":
|
| 259 |
+
return "Enter the command to execute."
|
| 260 |
+
elif action == "PYTHON_INTERPRET":
|
| 261 |
+
return "Enter the Python code to interpret."
|
| 262 |
+
elif action == "NLP":
|
| 263 |
+
return "Enter the text for NLP analysis."
|
| 264 |
else:
|
| 265 |
raise InvalidActionError("Please provide a valid action.")
|
| 266 |
except InvalidActionError as e:
|
|
|
|
| 324 |
"""Provides a prompt to understand the test results."""
|
| 325 |
return "What do you want to know about the test results?"
|
| 326 |
|
| 327 |
+
# --- Command Execution Functionality ---
|
| 328 |
+
def execute_command(self, command: str) -> str:
|
| 329 |
+
"""Executes the provided command in the terminal."""
|
| 330 |
+
try:
|
| 331 |
+
process = subprocess.run(
|
| 332 |
+
command.split(), capture_output=True, text=True
|
| 333 |
+
)
|
| 334 |
+
return f"Command output:\n{process.stdout}"
|
| 335 |
+
except subprocess.CalledProcessError as e:
|
| 336 |
+
return f"Error executing command: {e}"
|
| 337 |
+
|
| 338 |
+
# --- Python Interpretation Functionality ---
|
| 339 |
+
def python_interpret(self, code: str) -> str:
|
| 340 |
+
"""Interprets the provided Python code."""
|
| 341 |
+
try:
|
| 342 |
+
exec(code)
|
| 343 |
+
return "Python code executed successfully."
|
| 344 |
+
except Exception as e:
|
| 345 |
+
return f"Error interpreting Python code: {e}"
|
| 346 |
+
|
| 347 |
+
# --- NLP Functionality ---
|
| 348 |
+
def nlp(self, text: str) -> str:
|
| 349 |
+
"""Performs NLP analysis on the provided text."""
|
| 350 |
+
try:
|
| 351 |
+
if not self.nlp_pipeline:
|
| 352 |
+
self.nlp_pipeline = pipeline(
|
| 353 |
+
"sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-5-lit"
|
| 354 |
+
) # Example NLP pipeline
|
| 355 |
+
analysis = self.nlp_pipeline(text)
|
| 356 |
+
return f"NLP Analysis: {analysis}"
|
| 357 |
+
except Exception as e:
|
| 358 |
+
return f"Error performing NLP analysis: {e}"
|
| 359 |
+
|
| 360 |
# --- Input Handling Functionality ---
|
| 361 |
def handle_input(self, input_str: str):
|
| 362 |
"""Handles user input and executes the corresponding action."""
|
|
|
|
| 444 |
layout="wide",
|
| 445 |
initial_sidebar_state="expanded",
|
| 446 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 447 |
|
| 448 |
+
# --- Tabbed Navigation ---
|
| 449 |
+
tabs = st.tabs(["Agent Generation", "Chat App"])
|
| 450 |
+
|
| 451 |
+
# --- Agent Generation Tab ---
|
| 452 |
+
with tabs[0]:
|
| 453 |
+
st.title("AI Agent Generation")
|
| 454 |
+
st.sidebar.title("Agent Settings")
|
| 455 |
+
|
| 456 |
+
# --- Command Dropdown ---
|
| 457 |
+
command_options = [
|
| 458 |
+
"SEARCH",
|
| 459 |
+
"CODEGEN",
|
| 460 |
+
"REFINE-CODE",
|
| 461 |
+
"TEST-CODE",
|
| 462 |
+
"INTEGRATE-CODE",
|
| 463 |
+
"TEST-APP",
|
| 464 |
+
"GENERATE-REPORT",
|
| 465 |
+
"WORKSPACE-EXPLORER",
|
| 466 |
+
"ADD_PROMPT",
|
| 467 |
+
"ACTION_PROMPT",
|
| 468 |
+
"COMPRESS_HISTORY_PROMPT",
|
| 469 |
+
"LOG_PROMPT",
|
| 470 |
+
"LOG_RESPONSE",
|
| 471 |
+
"MODIFY_PROMPT",
|
| 472 |
+
"PREFIX",
|
| 473 |
+
"SEARCH_QUERY",
|
| 474 |
+
"READ_PROMPT",
|
| 475 |
+
"TASK_PROMPT",
|
| 476 |
+
"UNDERSTAND_TEST_RESULTS_PROMPT",
|
| 477 |
+
]
|
| 478 |
+
selected_command = st.sidebar.selectbox("Command", command_options)
|
| 479 |
+
|
| 480 |
+
# --- Model Dropdown ---
|
| 481 |
+
selected_model = st.sidebar.selectbox(
|
| 482 |
+
"Model",
|
| 483 |
+
agent.available_models,
|
| 484 |
+
index=agent.available_models.index(agent.selected_model),
|
| 485 |
+
)
|
| 486 |
+
agent.selected_model = selected_model
|
| 487 |
+
|
| 488 |
+
# --- Input Field ---
|
| 489 |
+
input_str = st.text_input(f"Enter input for {selected_command}:")
|
| 490 |
+
|
| 491 |
+
# --- Execute Command ---
|
| 492 |
+
if st.button("Execute"):
|
| 493 |
+
if input_str:
|
| 494 |
+
agent.handle_input(f"{selected_command} {input_str}")
|
| 495 |
+
st.write(f"Output: {agent.task_history[-1]['output']}")
|
| 496 |
+
|
| 497 |
+
# --- Task History ---
|
| 498 |
+
st.subheader("Task History")
|
| 499 |
+
for task in agent.task_history:
|
| 500 |
+
st.write(f"**Action:** {task['action']}")
|
| 501 |
+
st.write(f"**Input:** {task['input']}")
|
| 502 |
+
st.write(f"**Output:** {task['output']}")
|
| 503 |
+
|
| 504 |
+
# --- Workspace Explorer ---
|
| 505 |
+
st.subheader("Workspace Explorer")
|
| 506 |
+
with st.expander("Explore Workspace"):
|
| 507 |
+
try:
|
| 508 |
+
workspace_output = agent.workspace_explorer()
|
| 509 |
+
st.write(workspace_output)
|
| 510 |
+
except WorkspaceExplorerError as e:
|
| 511 |
+
st.error(f"Error exploring workspace: {e}")
|
| 512 |
+
|
| 513 |
+
# --- Chat App Tab ---
|
| 514 |
+
with tabs[1]:
|
| 515 |
+
st.title("Chat App")
|
| 516 |
+
|
| 517 |
+
# --- Chat History ---
|
| 518 |
+
chat_history = st.empty()
|
| 519 |
+
chat_history.text("Chat History:")
|
| 520 |
+
|
| 521 |
+
# --- Input Field ---
|
| 522 |
+
user_input = st.text_input("Enter your message:")
|
| 523 |
+
|
| 524 |
+
# --- Send Message ---
|
| 525 |
+
if st.button("Send"):
|
| 526 |
+
if user_input:
|
| 527 |
+
# --- Display User Message ---
|
| 528 |
+
chat_history.text(f"You: {user_input}")
|
| 529 |
+
|
| 530 |
+
# --- Process User Input ---
|
| 531 |
+
try:
|
| 532 |
+
# --- Extract Command and Arguments ---
|
| 533 |
+
action, *args = user_input.split()
|
| 534 |
+
if action in agent.tools:
|
| 535 |
+
if args:
|
| 536 |
+
output = agent.tools[action](" ".join(args))
|
| 537 |
+
else:
|
| 538 |
+
output = agent.tools[action]()
|
| 539 |
+
# --- Display Agent Response ---
|
| 540 |
+
chat_history.text(f"Agent: {output}")
|
| 541 |
+
else:
|
| 542 |
+
# --- Treat as regular chat message ---
|
| 543 |
+
output = agent.code_generation(user_input)
|
| 544 |
+
# --- Display Agent Response ---
|
| 545 |
+
chat_history.text(f"Agent: {output}")
|
| 546 |
+
except Exception as e:
|
| 547 |
+
# --- Display Error Message ---
|
| 548 |
+
chat_history.text(f"Agent: Error: {e}")
|
| 549 |
+
|
| 550 |
+
# --- Clear Input Field ---
|
| 551 |
+
user_input = ""
|
| 552 |
|
| 553 |
# --- Gradio Integration ---
|
| 554 |
def gradio_interface(input_text):
|