Onurcan Genç commited on
Commit
6b3c76e
·
1 Parent(s): ff023bb
Files changed (1) hide show
  1. app.py +14 -16
app.py CHANGED
@@ -4,6 +4,7 @@ from interpreter import interpreter
4
  import argparse
5
  import subprocess
6
  import sys
 
7
 
8
  # Fetch the Hugging Face API key from environment variables
9
  api_key = os.getenv("HUGGINGFACE_API_KEY")
@@ -11,19 +12,25 @@ api_key = os.getenv("HUGGINGFACE_API_KEY")
11
  if not api_key:
12
  raise ValueError("API key not found. Set the HUGGINGFACE_API_KEY environment variable.")
13
 
14
- # Set the API key for the interpreter
15
  interpreter.llm.api_key = api_key
16
- interpreter.llm.model = "meta-llama/Llama-3.1-8B-Instruct" # Example model
 
17
 
18
  # Function to execute shell commands if detected in model output
19
  def execute_shell_command(command):
20
- result = subprocess.run(command, shell=True, capture_output=True, text=True)
 
 
21
  return result.stdout if result.returncode == 0 else result.stderr
22
 
23
  # Function to process tasks using OpenInterpreter and detect shell commands
24
  def execute_task(task):
25
- # Get the model-generated response first
26
- response = interpreter.chat(task)
 
 
 
27
 
28
  # Check if the response indicates a shell command
29
  if "run shell" in response.lower():
@@ -42,16 +49,7 @@ def cli_interface():
42
  result = execute_task(args.task)
43
  print(result)
44
 
45
- # Gradio interface for the web app
46
- def run_gradio_interface():
47
- iface = gr.Interface(fn=execute_task, inputs="text", outputs="text")
48
- iface.launch()
49
-
50
  # Main entry point for the script
51
  if __name__ == "__main__":
52
- if len(sys.argv) > 1:
53
- # Run the CLI if arguments are provided
54
- cli_interface()
55
- else:
56
- # Run the Gradio web app by default
57
- run_gradio_interface()
 
4
  import argparse
5
  import subprocess
6
  import sys
7
+ import shlex
8
 
9
  # Fetch the Hugging Face API key from environment variables
10
  api_key = os.getenv("HUGGINGFACE_API_KEY")
 
12
  if not api_key:
13
  raise ValueError("API key not found. Set the HUGGINGFACE_API_KEY environment variable.")
14
 
15
+ # Set the API key and model for the interpreter
16
  interpreter.llm.api_key = api_key
17
+ interpreter.llm.provider = "huggingface" # Specify the provider
18
+ interpreter.llm.model = "meta-llama/Llama-3.1-8B-Instruct" # Use the correct model identifier
19
 
20
  # Function to execute shell commands if detected in model output
21
  def execute_shell_command(command):
22
+ # Sanitize user input using shlex.split to avoid security vulnerabilities
23
+ sanitized_command = shlex.split(command)
24
+ result = subprocess.run(sanitized_command, capture_output=True, text=True)
25
  return result.stdout if result.returncode == 0 else result.stderr
26
 
27
  # Function to process tasks using OpenInterpreter and detect shell commands
28
  def execute_task(task):
29
+ try:
30
+ # Get the model-generated response first
31
+ response = "".join([chunk for chunk in interpreter.chat(task, stream=True)])
32
+ except Exception as e:
33
+ return f"Error while interacting with the model: {str(e)}"
34
 
35
  # Check if the response indicates a shell command
36
  if "run shell" in response.lower():
 
49
  result = execute_task(args.task)
50
  print(result)
51
 
 
 
 
 
 
52
  # Main entry point for the script
53
  if __name__ == "__main__":
54
+ # Run the CLI interface only, no web app
55
+ cli_interface()