Artificial-superintelligence
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -28,19 +28,16 @@ model = genai.GenerativeModel(
|
|
28 |
)
|
29 |
|
30 |
system_instruction = """
|
31 |
-
You are an AI-powered terminal assistant. Your role is to interpret user requests and execute appropriate terminal commands. Follow these guidelines:
|
32 |
|
33 |
1. Understand and execute various commands, including but not limited to:
|
34 |
- pip install: For installing Python packages
|
35 |
- git clone: For cloning repositories
|
36 |
- cd: For changing directories
|
37 |
- Python script execution: For running .py files
|
38 |
-
-
|
39 |
-
- hide files: Hide the file explorer
|
40 |
-
- new file: Create a new file
|
41 |
-
- edit file: Open a file for editing
|
42 |
|
43 |
-
2. Provide the exact command to be executed, without any explanation or additional text.
|
44 |
|
45 |
3. For pip install requests, always prefix the command with '!python -m' to ensure proper execution.
|
46 |
|
@@ -50,17 +47,14 @@ You are an AI-powered terminal assistant. Your role is to interpret user request
|
|
50 |
|
51 |
6. For Python script execution, provide the command to run the script (e.g., '!python script_name.py').
|
52 |
|
53 |
-
7. For
|
|
|
|
|
|
|
54 |
|
55 |
-
8.
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
10. For edit file requests, respond with 'edit filename.ext', where filename.ext is the name of the file to be edited.
|
60 |
-
|
61 |
-
11. If a request is unclear or doesn't match any known command type, respond with "Unclear request. Please provide more details."
|
62 |
-
|
63 |
-
Always respond with ONLY the command to be executed, nothing else.
|
64 |
"""
|
65 |
|
66 |
chat = model.start_chat(history=[])
|
@@ -96,8 +90,20 @@ def execute_code():
|
|
96 |
response = chat.send_message(system_instruction + f"\nUser request: {ai_command}")
|
97 |
ai_result = response.text.strip()
|
98 |
|
99 |
-
# Execute the AI-suggested command
|
100 |
-
if ai_result.startswith("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
result = execute_command(ai_result[1:]) # Remove the leading '!'
|
102 |
elif ai_result.startswith("git clone"):
|
103 |
result = execute_command(ai_result)
|
@@ -110,25 +116,6 @@ def execute_code():
|
|
110 |
result = f"Error: Directory not found: {new_dir}"
|
111 |
elif ai_result.startswith("!python "):
|
112 |
result = execute_command(ai_result[1:]) # Remove the leading '!'
|
113 |
-
elif ai_result == "show files":
|
114 |
-
files = os.listdir(current_dir)
|
115 |
-
result = "Files in current directory:\n" + "\n".join(files)
|
116 |
-
elif ai_result == "hide files":
|
117 |
-
result = "Files hidden."
|
118 |
-
elif ai_result.startswith("new file "):
|
119 |
-
filename = ai_result[9:].strip()
|
120 |
-
filepath = os.path.join(current_dir, filename)
|
121 |
-
with open(filepath, 'w') as f:
|
122 |
-
pass # Create an empty file
|
123 |
-
result = f"Created new file: {filename}"
|
124 |
-
elif ai_result.startswith("edit "):
|
125 |
-
filename = ai_result[5:].strip()
|
126 |
-
filepath = os.path.join(current_dir, filename)
|
127 |
-
if os.path.exists(filepath):
|
128 |
-
result = "Enter code:"
|
129 |
-
return jsonify({"result": result, "action": "edit", "filename": filename})
|
130 |
-
else:
|
131 |
-
result = f"Error: File {filename} not found."
|
132 |
else:
|
133 |
result = f"Unclear AI response: {ai_result}"
|
134 |
|
|
|
28 |
)
|
29 |
|
30 |
system_instruction = """
|
31 |
+
You are an AI-powered terminal assistant. Your role is to interpret user requests and execute appropriate terminal commands or actions. Follow these guidelines:
|
32 |
|
33 |
1. Understand and execute various commands, including but not limited to:
|
34 |
- pip install: For installing Python packages
|
35 |
- git clone: For cloning repositories
|
36 |
- cd: For changing directories
|
37 |
- Python script execution: For running .py files
|
38 |
+
- File creation and editing: For creating new files and adding content
|
|
|
|
|
|
|
39 |
|
40 |
+
2. Provide the exact command to be executed or action to be taken, without any explanation or additional text.
|
41 |
|
42 |
3. For pip install requests, always prefix the command with '!python -m' to ensure proper execution.
|
43 |
|
|
|
47 |
|
48 |
6. For Python script execution, provide the command to run the script (e.g., '!python script_name.py').
|
49 |
|
50 |
+
7. For file creation and editing requests:
|
51 |
+
- If asked to create a new file, respond with: "CREATE_FILE:filename.py"
|
52 |
+
- If asked to edit a file with specific content, respond with: "EDIT_FILE:filename.py:file_content"
|
53 |
+
Replace 'filename.py' with the actual filename and 'file_content' with the requested content.
|
54 |
|
55 |
+
8. If a request is unclear or doesn't match any known command type, respond with "Unclear request. Please provide more details."
|
56 |
|
57 |
+
Always respond with ONLY the command to be executed or action to be taken, nothing else.
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
"""
|
59 |
|
60 |
chat = model.start_chat(history=[])
|
|
|
90 |
response = chat.send_message(system_instruction + f"\nUser request: {ai_command}")
|
91 |
ai_result = response.text.strip()
|
92 |
|
93 |
+
# Execute the AI-suggested command or action
|
94 |
+
if ai_result.startswith("CREATE_FILE:"):
|
95 |
+
filename = ai_result.split(":")[1]
|
96 |
+
filepath = os.path.join(current_dir, filename)
|
97 |
+
with open(filepath, 'w') as f:
|
98 |
+
pass # Create an empty file
|
99 |
+
result = f"Created new file: {filename}"
|
100 |
+
elif ai_result.startswith("EDIT_FILE:"):
|
101 |
+
_, filename, content = ai_result.split(":", 2)
|
102 |
+
filepath = os.path.join(current_dir, filename)
|
103 |
+
with open(filepath, 'w') as f:
|
104 |
+
f.write(content)
|
105 |
+
result = f"File {filename} created and edited successfully."
|
106 |
+
elif ai_result.startswith("!python -m pip install"):
|
107 |
result = execute_command(ai_result[1:]) # Remove the leading '!'
|
108 |
elif ai_result.startswith("git clone"):
|
109 |
result = execute_command(ai_result)
|
|
|
116 |
result = f"Error: Directory not found: {new_dir}"
|
117 |
elif ai_result.startswith("!python "):
|
118 |
result = execute_command(ai_result[1:]) # Remove the leading '!'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
else:
|
120 |
result = f"Unclear AI response: {ai_result}"
|
121 |
|