Artificial-superintelligence commited on
Commit
a2ea954
·
verified ·
1 Parent(s): 7ffc5a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -2
app.py CHANGED
@@ -4,6 +4,7 @@ import subprocess
4
  import tempfile
5
  import shutil
6
  import sys
 
7
 
8
  app = Flask(__name__)
9
 
@@ -11,6 +12,47 @@ app = Flask(__name__)
11
  temp_dir = tempfile.mkdtemp()
12
  current_dir = temp_dir
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def execute_command(command, cwd=None):
15
  """Executes a command and returns the output."""
16
  process = subprocess.Popen(
@@ -36,7 +78,31 @@ def execute_code():
36
  return jsonify({"result": "Error: No command provided."})
37
 
38
  try:
39
- if command == "show files":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  files = os.listdir(current_dir)
41
  return jsonify({"result": "Files in current directory:\n" + "\n".join(files)})
42
  elif command == "hide files":
@@ -104,4 +170,5 @@ def download_file(filename):
104
  return send_from_directory(current_dir, filename, as_attachment=True)
105
 
106
  if __name__ == "__main__":
107
- app.run(host="0.0.0.0", port=7860)
 
 
4
  import tempfile
5
  import shutil
6
  import sys
7
+ import google.generativeai as genai
8
 
9
  app = Flask(__name__)
10
 
 
12
  temp_dir = tempfile.mkdtemp()
13
  current_dir = temp_dir
14
 
15
+ # Configure Gemini AI
16
+ genai.configure(api_key=os.environ["GEMINI_API_KEY"])
17
+
18
+ generation_config = {
19
+ "temperature": 0.9,
20
+ "top_p": 1,
21
+ "top_k": 40,
22
+ "max_output_tokens": 1024,
23
+ }
24
+
25
+ model = genai.GenerativeModel(
26
+ model_name="gemini-1.5-pro",
27
+ generation_config=generation_config,
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
+ 2. Provide the exact command to be executed, without any explanation or additional text.
40
+
41
+ 3. For pip install requests, always prefix the command with '!python -m' to ensure proper execution.
42
+
43
+ 4. For git clone requests, provide the full 'git clone' command with the repository URL.
44
+
45
+ 5. For cd requests, simply provide the 'cd' command with the specified directory.
46
+
47
+ 6. For Python script execution, provide the command to run the script (e.g., '!python script_name.py').
48
+
49
+ 7. If a request is unclear or doesn't match any known command type, respond with "Unclear request. Please provide more details."
50
+
51
+ Always respond with ONLY the command to be executed, nothing else.
52
+ """
53
+
54
+ chat = model.start_chat(history=[])
55
+
56
  def execute_command(command, cwd=None):
57
  """Executes a command and returns the output."""
58
  process = subprocess.Popen(
 
78
  return jsonify({"result": "Error: No command provided."})
79
 
80
  try:
81
+ if command.lower().startswith("ai:"):
82
+ # Process command with Gemini AI
83
+ ai_command = command[3:].strip()
84
+ response = chat.send_message(system_instruction + f"\nUser request: {ai_command}")
85
+ ai_result = response.text.strip()
86
+
87
+ # Execute the AI-suggested command
88
+ if ai_result.startswith("!python -m pip install"):
89
+ result = execute_command(ai_result[1:]) # Remove the leading '!'
90
+ elif ai_result.startswith("git clone"):
91
+ result = execute_command(ai_result)
92
+ elif ai_result.startswith("cd "):
93
+ new_dir = os.path.join(current_dir, ai_result[3:])
94
+ if os.path.isdir(new_dir):
95
+ current_dir = os.path.abspath(new_dir)
96
+ result = f"Changed directory to: {current_dir}"
97
+ else:
98
+ result = f"Error: Directory not found: {new_dir}"
99
+ elif ai_result.startswith("!python "):
100
+ result = execute_command(ai_result[1:]) # Remove the leading '!'
101
+ else:
102
+ result = f"Unclear AI response: {ai_result}"
103
+
104
+ return jsonify({"result": f"AI Executed: {ai_result}\n\nOutput:\n{result}"})
105
+ elif command == "show files":
106
  files = os.listdir(current_dir)
107
  return jsonify({"result": "Files in current directory:\n" + "\n".join(files)})
108
  elif command == "hide files":
 
170
  return send_from_directory(current_dir, filename, as_attachment=True)
171
 
172
  if __name__ == "__main__":
173
+ app.run(host="0.0.0.0", port=7860)
174
+