Spaces:
Sleeping
Sleeping
acecalisto3
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -8,13 +8,8 @@ import os
|
|
8 |
import json
|
9 |
from streamlit_ace import st_ace
|
10 |
from agent import (
|
11 |
-
AppType,
|
12 |
-
|
13 |
-
createSpace,
|
14 |
-
isPythonOrGradioAppPrompt,
|
15 |
-
isReactAppPrompt,
|
16 |
-
isStreamlitAppPrompt,
|
17 |
-
generateFiles,
|
18 |
)
|
19 |
|
20 |
import importlib
|
@@ -328,7 +323,7 @@ def generate_code(code_idea):
|
|
328 |
str: The generated code.
|
329 |
"""
|
330 |
# Generate code using the Hugging Face API
|
331 |
-
generated_code = code_generator(f"
|
332 |
|
333 |
st.session_state.current_state['toolbox']['generated_code'] = generated_code
|
334 |
return generated_code
|
@@ -412,213 +407,27 @@ elif app_mode == "Tool Box":
|
|
412 |
if st.button("Analyze Sentiment"):
|
413 |
sentiment = sentiment_analysis(sentiment_text)
|
414 |
st.write(f"Sentiment: {sentiment}")
|
415 |
-
# Function to translate code using the Hugging Face API
|
416 |
-
def translate_code(code, input_language, output_language):
|
417 |
-
"""
|
418 |
-
Translates code from one programming language to another using a Hugging Face translation pipeline.
|
419 |
-
Args:
|
420 |
-
code (str): The code to be translated.
|
421 |
-
input_language (str): The source programming language.
|
422 |
-
output_language (str): The target programming language.
|
423 |
-
Returns:
|
424 |
-
str: The translated code.
|
425 |
-
"""
|
426 |
-
# Define a mapping of programming languages to natural languages
|
427 |
-
language_map = {
|
428 |
-
"python": "English",
|
429 |
-
"javascript": "French",
|
430 |
-
"java": "German",
|
431 |
-
# Add more mappings as needed
|
432 |
-
}
|
433 |
-
|
434 |
-
# Function to translate code using the Hugging Face API
|
435 |
-
def translate_code(code, input_language, output_language):
|
436 |
-
"""
|
437 |
-
Translates code from one programming language to another using a Hugging Face translation pipeline.
|
438 |
-
Args:
|
439 |
-
code (str): The code to be translated.
|
440 |
-
input_language (str): The source programming language.
|
441 |
-
output_language (str): The target programming language.
|
442 |
-
Returns:
|
443 |
-
str: The translated code.
|
444 |
-
"""
|
445 |
-
# Define a mapping of programming languages to natural languages
|
446 |
-
language_map = {
|
447 |
-
"python": "English",
|
448 |
-
"javascript": "French",
|
449 |
-
"java": "German",
|
450 |
-
# Add more mappings as needed
|
451 |
-
}
|
452 |
-
|
453 |
-
# Translate the code using the translation pipeline
|
454 |
-
translated_code = translator(code, src_lang=language_map[input_language], tgt_lang=language_map[output_language])[0]['translation_text']
|
455 |
-
|
456 |
-
st.session_state.current_state['toolbox']['translated_code'] = translated_code
|
457 |
-
return translated_code
|
458 |
-
|
459 |
-
# Function to generate code using the Hugging Face API
|
460 |
-
def generate_code(prompt):
|
461 |
-
"""
|
462 |
-
Generates code based on a given prompt using a Hugging Face code generation pipeline.
|
463 |
-
Args:
|
464 |
-
prompt (str): The prompt describing the code to be generated.
|
465 |
-
Returns:
|
466 |
-
str: The generated code.
|
467 |
-
"""
|
468 |
-
generated_code = code_generator(prompt, max_length=100, num_return_sequences=1)[0]['generated_text']
|
469 |
-
st.session_state.current_state['toolbox']['generated_code'] = generated_code
|
470 |
-
return generated_code
|
471 |
-
|
472 |
-
# Function to commit and push changes to the Hugging Face repository
|
473 |
-
def commit_and_push_changes(commit_message):
|
474 |
-
"""
|
475 |
-
Commits and pushes changes to the Hugging Face repository.
|
476 |
-
Args:
|
477 |
-
commit_message (str): The commit message.
|
478 |
-
"""
|
479 |
-
try:
|
480 |
-
subprocess.run(["git", "add", "."], check=True)
|
481 |
-
subprocess.run(["git", "commit", "-m", commit_message], check=True)
|
482 |
-
subprocess.run(["git", "push", HUGGING_FACE_REPO_URL], check=True)
|
483 |
-
except subprocess.CalledProcessError as e:
|
484 |
-
st.error(f"Error committing and pushing changes: {e}")
|
485 |
-
|
486 |
-
# Streamlit UI
|
487 |
-
st.title("AI Agent Creator and Workspace Chat App")
|
488 |
-
|
489 |
-
# Sidebar navigation
|
490 |
-
st.sidebar.title("Navigation")
|
491 |
-
app_mode = st.sidebar.selectbox("Choose the app mode", ["AI Agent Creator", "Tool Box", "Workspace Chat App"])
|
492 |
-
|
493 |
-
# Add dark mode toggle
|
494 |
-
if st.sidebar.checkbox("Dark Mode"):
|
495 |
-
st.markdown("""
|
496 |
-
<style>
|
497 |
-
.stApp {
|
498 |
-
background-color: #2b2b2b;
|
499 |
-
color: #ffffff;
|
500 |
-
}
|
501 |
-
</style>
|
502 |
-
""", unsafe_allow_html=True)
|
503 |
-
|
504 |
-
if app_mode == "AI Agent Creator":
|
505 |
-
st.header("Create an AI Agent")
|
506 |
-
|
507 |
-
col1, col2 = st.columns(2)
|
508 |
-
|
509 |
-
with col1:
|
510 |
-
agent_name = st.text_input("Enter agent name:")
|
511 |
-
agent_description = st.text_area("Enter agent description:")
|
512 |
-
agent_skills = st.text_area("Enter agent skills (one per line):")
|
513 |
-
|
514 |
-
if st.button("Create Agent"):
|
515 |
-
skills_list = agent_skills.split('\n')
|
516 |
-
new_agent = AIAgent(agent_name, agent_description, skills_list)
|
517 |
-
save_agent_to_file(new_agent)
|
518 |
-
st.success(f"Agent '{agent_name}' created successfully.")
|
519 |
-
|
520 |
-
with col2:
|
521 |
-
st.subheader("Available Agents")
|
522 |
-
for agent in st.session_state.available_agents:
|
523 |
-
st.write(f"- {agent}")
|
524 |
-
|
525 |
-
elif app_mode == "Tool Box":
|
526 |
-
st.header("AI-Powered Tools")
|
527 |
-
|
528 |
-
tool = st.selectbox("Select a tool", ["Chat", "Terminal", "Code Editor", "Summarize Text", "Sentiment Analysis", "Translate Code", "Generate Code"])
|
529 |
-
|
530 |
-
if tool == "Chat":
|
531 |
-
st.subheader("Chat with AI Assistant")
|
532 |
-
chat_input = st.text_area("Enter your message:")
|
533 |
-
agent_name = st.selectbox("Select an agent (optional):", [""] + st.session_state.available_agents)
|
534 |
-
|
535 |
-
if st.button("Send"):
|
536 |
-
if agent_name:
|
537 |
-
chat_response = chat_interface_with_agent(chat_input, agent_name)
|
538 |
-
else:
|
539 |
-
chat_response = chat_interface(chat_input)
|
540 |
-
st.session_state.chat_history.append((chat_input, chat_response))
|
541 |
-
st.write(f"AI: {chat_response}")
|
542 |
-
|
543 |
-
elif tool == "Terminal":
|
544 |
-
st.subheader("Terminal")
|
545 |
-
terminal_input = st.text_input("Enter a command:")
|
546 |
-
project_name = st.text_input("Enter project name (optional):")
|
547 |
-
|
548 |
-
if st.button("Run Command"):
|
549 |
-
terminal_output = terminal_interface(terminal_input, project_name)
|
550 |
-
st.session_state.terminal_history.append((terminal_input, terminal_output))
|
551 |
-
st.code(terminal_output, language="bash")
|
552 |
-
|
553 |
-
elif tool == "Code Editor":
|
554 |
-
st.subheader("Code Editor")
|
555 |
-
code = st_ace(
|
556 |
-
placeholder="Write your code here",
|
557 |
-
language="python",
|
558 |
-
theme="monokai",
|
559 |
-
keybinding="vscode",
|
560 |
-
font_size=14,
|
561 |
-
tab_size=4,
|
562 |
-
show_gutter=True,
|
563 |
-
show_print_margin=True,
|
564 |
-
wrap=False,
|
565 |
-
auto_update=False,
|
566 |
-
)
|
567 |
-
if st.button("Format & Lint"):
|
568 |
-
with st.spinner("Formatting and linting..."):
|
569 |
-
formatted_code, lint_message = code_editor_interface(code)
|
570 |
-
st.code(formatted_code, language="python")
|
571 |
-
st.info(lint_message)
|
572 |
-
|
573 |
-
elif tool == "Summarize Text":
|
574 |
-
st.subheader("Summarize Text")
|
575 |
-
text_to_summarize = st.text_area("Enter text to summarize:")
|
576 |
-
if st.button("Summarize"):
|
577 |
-
summary = summarize_text(text_to_summarize)
|
578 |
-
st.write("Summary:", summary)
|
579 |
-
|
580 |
-
elif tool == "Sentiment Analysis":
|
581 |
-
st.subheader("Sentiment Analysis")
|
582 |
-
text_to_analyze = st.text_area("Enter text for sentiment analysis:")
|
583 |
-
if st.button("Analyze Sentiment"):
|
584 |
-
sentiment = sentiment_analysis(text_to_analyze)
|
585 |
-
st.write("Sentiment:", sentiment)
|
586 |
-
|
587 |
-
elif tool == "Translate Code":
|
588 |
-
st.subheader("Translate Code")
|
589 |
-
code_to_translate = st.text_area("Enter code to translate:")
|
590 |
-
input_language = st.selectbox("Select input language:", ["python", "javascript", "java"])
|
591 |
-
output_language = st.selectbox("Select output language:", ["python", "javascript", "java"])
|
592 |
-
if st.button("Translate"):
|
593 |
-
translated_code = translate_code(code_to_translate, input_language, output_language)
|
594 |
-
st.code(translated_code, language=output_language)
|
595 |
-
|
596 |
-
elif tool == "Generate Code":
|
597 |
-
st.subheader("Generate Code")
|
598 |
-
code_prompt = st.text_area("Enter a prompt for code generation:")
|
599 |
-
if st.button("Generate Code"):
|
600 |
-
generated_code = generate_code(code_prompt)
|
601 |
-
st.code(generated_code, language="python")
|
602 |
|
|
|
603 |
elif app_mode == "Workspace Chat App":
|
604 |
st.header("Workspace Chat App")
|
605 |
-
|
606 |
col1, col2 = st.columns(2)
|
607 |
-
|
608 |
with col1:
|
609 |
st.subheader("Create a New Project")
|
610 |
project_name = st.text_input("Enter project name:")
|
611 |
if st.button("Create Project"):
|
612 |
workspace_status = workspace_interface(project_name)
|
613 |
st.success(workspace_status)
|
614 |
-
|
615 |
st.subheader("Add Code to Workspace")
|
616 |
code_to_add = st.text_area("Enter code to add to workspace:")
|
617 |
file_name = st.text_input("Enter file name (e.g. 'app.py'):")
|
618 |
if st.button("Add Code"):
|
619 |
add_code_status = add_code_to_workspace(project_name, code_to_add, file_name)
|
620 |
st.success(add_code_status)
|
621 |
-
|
622 |
with col2:
|
623 |
st.subheader("Chat with AI Assistant")
|
624 |
chat_input = st.text_area("Enter your message for guidance:")
|
@@ -626,17 +435,17 @@ elif app_mode == "Workspace Chat App":
|
|
626 |
chat_response = chat_interface(chat_input)
|
627 |
st.session_state.chat_history.append((chat_input, chat_response))
|
628 |
st.write(f"AI: {chat_response}")
|
629 |
-
|
630 |
st.subheader("Chat History")
|
631 |
for user_input, response in st.session_state.chat_history:
|
632 |
st.write(f"User: {user_input}")
|
633 |
st.write(f"AI: {response}")
|
634 |
-
|
635 |
st.subheader("Terminal History")
|
636 |
for command, output in st.session_state.terminal_history:
|
637 |
st.write(f"Command: {command}")
|
638 |
st.code(output, language="bash")
|
639 |
-
|
640 |
st.subheader("Workspace Projects")
|
641 |
for project, details in st.session_state.workspace_projects.items():
|
642 |
st.write(f"Project: {project}")
|
@@ -645,103 +454,5 @@ elif app_mode == "Workspace Chat App":
|
|
645 |
st.write(f"- {file}")
|
646 |
|
647 |
# Display the current state
|
648 |
-
st.sidebar.subheader("Current State"),
|
649 |
-
st.sidebar.json(st.session_state.current_state),
|
650 |
-
st.header("Workspace Chat App")
|
651 |
-
|
652 |
-
# Project Workspace Creation
|
653 |
-
st.subheader("Create a New Project")
|
654 |
-
project_name = st.text_input("Enter project name:")
|
655 |
-
if st.button("Create Project"):
|
656 |
-
workspace_status = workspace_interface(project_name)
|
657 |
-
st.success(workspace_status)
|
658 |
-
|
659 |
-
# Add Code to Workspace
|
660 |
-
st.subheader("Add Code to Workspace")
|
661 |
-
code_to_add = st.text_area("Enter code to add to workspace:")
|
662 |
-
file_name = st.text_input("Enter file name (e.g. 'app.py'):")
|
663 |
-
if st.button("Add Code"):
|
664 |
-
add_code_status = add_code_to_workspace(project_name, code_to_add, file_name)
|
665 |
-
st.success(add_code_status)
|
666 |
-
|
667 |
-
# Terminal Interface with Project Context
|
668 |
-
st.subheader("Terminal (Workspace Context)")
|
669 |
-
terminal_input = st.text_input("Enter a command within the workspace:")
|
670 |
-
if st.button("Run Command"):
|
671 |
-
terminal_output = terminal_interface(terminal_input, project_name)
|
672 |
-
st.code(terminal_output, language="bash")
|
673 |
-
|
674 |
-
# Chat Interface for Guidance
|
675 |
-
st.subheader("Chat with CodeCraft for Guidance")
|
676 |
-
chat_input = st.text_area("Enter your message for guidance:")
|
677 |
-
if st.button("Get Guidance"):
|
678 |
-
chat_response = chat_interface(chat_input)
|
679 |
-
st.session_state.chat_history.append((chat_input, chat_response))
|
680 |
-
st.write(f"CodeCraft: {chat_response}")
|
681 |
-
|
682 |
-
# Display Chat History
|
683 |
-
st.subheader("Chat History")
|
684 |
-
for user_input, response in st.session_state.chat_history:
|
685 |
-
st.write(f"User: {user_input}")
|
686 |
-
st.write(f"CodeCraft: {response}")
|
687 |
-
|
688 |
-
# Display Terminal History
|
689 |
-
st.subheader("Terminal History")
|
690 |
-
for command, output in st.session_state.terminal_history:
|
691 |
-
st.write(f"Command: {command}")
|
692 |
-
st.code(output, language="bash")
|
693 |
-
|
694 |
-
# Display Projects and Files
|
695 |
-
st.subheader("Workspace Projects")
|
696 |
-
for project, details in st.session_state.workspace_projects.items():
|
697 |
-
st.write(f"Project: {project}")
|
698 |
-
for file in details['files']:
|
699 |
-
st.write(f" - {file}")
|
700 |
-
|
701 |
-
# Chat with AI Agents
|
702 |
-
st.subheader("Chat with AI Agents")
|
703 |
-
selected_agent = st.selectbox("Select an AI agent", st.session_state.available_agents)
|
704 |
-
agent_chat_input = st.text_area("Enter your message for the agent:")
|
705 |
-
if st.button("Send to Agent"):
|
706 |
-
agent_chat_response = chat_interface_with_agent(agent_chat_input, selected_agent)
|
707 |
-
st.session_state.chat_history.append((agent_chat_input, agent_chat_response))
|
708 |
-
st.write(f"{selected_agent}: {agent_chat_response}")
|
709 |
-
|
710 |
-
# Automate Build Process
|
711 |
-
st.subheader("Automate Build Process")
|
712 |
-
if st.button("Automate"):
|
713 |
-
agent = AIAgent(selected_agent, "", []) # Load the agent without skills for now
|
714 |
-
summary, next_step = agent.autonomous_build(st.session_state.chat_history, st.session_state.workspace_projects)
|
715 |
-
st.write("Autonomous Build Summary:")
|
716 |
-
st.write(summary)
|
717 |
-
st.write("Next Step:")
|
718 |
-
st.write(next_step)
|
719 |
-
|
720 |
-
# Advanced Code Editor (Optional)
|
721 |
-
st.subheader("Advanced Code Editor")
|
722 |
-
selected_file = st.selectbox("Select a file from the workspace", st.session_state.workspace_projects[project_name]['files'])
|
723 |
-
file_path = os.path.join(PROJECT_ROOT, project_name, selected_file)
|
724 |
-
if os.path.exists(file_path):
|
725 |
-
with open(file_path, "r") as file:
|
726 |
-
file_content = file.read()
|
727 |
-
code_editor = st_ace(
|
728 |
-
file_content,
|
729 |
-
language="python",
|
730 |
-
theme="monokai",
|
731 |
-
height=300,
|
732 |
-
key="ace_editor",
|
733 |
-
)
|
734 |
-
if st.button("Save Changes"):
|
735 |
-
with open(file_path, "w") as file:
|
736 |
-
file.write(code_editor)
|
737 |
-
st.success(f"Changes saved to {selected_file}")
|
738 |
-
commit_and_push_changes(f"Update {selected_file}")
|
739 |
-
else:
|
740 |
-
st.warning(f"File {selected_file} not found in the workspace.")
|
741 |
-
|
742 |
-
# Display current state for debugging
|
743 |
st.sidebar.subheader("Current State")
|
744 |
-
st.sidebar.json(st.session_state.current_state)
|
745 |
-
|
746 |
-
if __name__ == "__main__":
|
747 |
-
os.system("streamlit run app.py")
|
|
|
8 |
import json
|
9 |
from streamlit_ace import st_ace
|
10 |
from agent import (
|
11 |
+
AppType, createLlamaPrompt, createSpace, isPythonOrGradioAppPrompt,
|
12 |
+
isReactAppPrompt, isStreamlitAppPrompt, generateFiles,
|
|
|
|
|
|
|
|
|
|
|
13 |
)
|
14 |
|
15 |
import importlib
|
|
|
323 |
str: The generated code.
|
324 |
"""
|
325 |
# Generate code using the Hugging Face API
|
326 |
+
generated_code = code_generator(f"python\n{code_idea}\n", max_length=512)[0]['generated_text']
|
327 |
|
328 |
st.session_state.current_state['toolbox']['generated_code'] = generated_code
|
329 |
return generated_code
|
|
|
407 |
if st.button("Analyze Sentiment"):
|
408 |
sentiment = sentiment_analysis(sentiment_text)
|
409 |
st.write(f"Sentiment: {sentiment}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
410 |
|
411 |
+
# Workspace Chat App
|
412 |
elif app_mode == "Workspace Chat App":
|
413 |
st.header("Workspace Chat App")
|
414 |
+
|
415 |
col1, col2 = st.columns(2)
|
416 |
+
|
417 |
with col1:
|
418 |
st.subheader("Create a New Project")
|
419 |
project_name = st.text_input("Enter project name:")
|
420 |
if st.button("Create Project"):
|
421 |
workspace_status = workspace_interface(project_name)
|
422 |
st.success(workspace_status)
|
423 |
+
|
424 |
st.subheader("Add Code to Workspace")
|
425 |
code_to_add = st.text_area("Enter code to add to workspace:")
|
426 |
file_name = st.text_input("Enter file name (e.g. 'app.py'):")
|
427 |
if st.button("Add Code"):
|
428 |
add_code_status = add_code_to_workspace(project_name, code_to_add, file_name)
|
429 |
st.success(add_code_status)
|
430 |
+
|
431 |
with col2:
|
432 |
st.subheader("Chat with AI Assistant")
|
433 |
chat_input = st.text_area("Enter your message for guidance:")
|
|
|
435 |
chat_response = chat_interface(chat_input)
|
436 |
st.session_state.chat_history.append((chat_input, chat_response))
|
437 |
st.write(f"AI: {chat_response}")
|
438 |
+
|
439 |
st.subheader("Chat History")
|
440 |
for user_input, response in st.session_state.chat_history:
|
441 |
st.write(f"User: {user_input}")
|
442 |
st.write(f"AI: {response}")
|
443 |
+
|
444 |
st.subheader("Terminal History")
|
445 |
for command, output in st.session_state.terminal_history:
|
446 |
st.write(f"Command: {command}")
|
447 |
st.code(output, language="bash")
|
448 |
+
|
449 |
st.subheader("Workspace Projects")
|
450 |
for project, details in st.session_state.workspace_projects.items():
|
451 |
st.write(f"Project: {project}")
|
|
|
454 |
st.write(f"- {file}")
|
455 |
|
456 |
# Display the current state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
457 |
st.sidebar.subheader("Current State")
|
458 |
+
st.sidebar.json(st.session_state.current_state)
|
|
|
|
|
|