Skip to content

Commit

Permalink
Added Upgrade --flag
Browse files Browse the repository at this point in the history
  • Loading branch information
haseeb-heaven committed Mar 5, 2024
1 parent 724985f commit 61c4aa2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 28 deletions.
7 changes: 7 additions & 0 deletions interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import traceback
import warnings
from libs.markdown_code import display_markdown_message
from libs.utility_manager import UtilityManager

# The main version of the interpreter.
INTERPRETER_VERSION = "2.1"
Expand All @@ -35,6 +36,7 @@ def main():
parser.add_argument('--lang', '-l', type=str, default='python', help='Set the interpreter language. (Defaults to Python)')
parser.add_argument('--display_code', '-dc', action='store_true', default=False, help='Display the code in output')
parser.add_argument('--history', '-hi', action='store_true', default=False, help='Use history as memory')
parser.add_argument('--upgrade', '-up', action='store_true', default=False, help='Upgrade the interpreter')
args = parser.parse_args()

# Check if only the application name is passed
Expand All @@ -44,6 +46,11 @@ def main():

warnings.filterwarnings("ignore") # To ignore all warnings

# Upgrade the interpreter if the --upgrade flag is passed.
if args.upgrade:
UtilityManager.upgrade_interpreter()
return

# Create an instance of the Interpreter class and call the main method.
interpreter = Interpreter(args)
interpreter.interpreter_main(INTERPRETER_VERSION)
Expand Down
24 changes: 1 addition & 23 deletions libs/interpreter_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,29 +515,7 @@ def interpreter_main(self,version):

# UPGRAGE - Command section.
elif task.lower() == '/upgrade':

# Download the requirements file
requirements_file_url = 'https://raw.githubusercontent.com/haseeb-heaven/code-interpreter/main/requirements.txt'
requirements_file_downloaded = self.utility_manager.download_file(requirements_file_url,'requirements.txt')

# Commands to execute.
command_pip_upgrade = 'pip install open-code-interpreter --upgrade'
command_pip_requirements = 'pip install -r requirements.txt --upgrade'

# Execute the commands.
command_output,_ = self.code_interpreter.execute_command(command_pip_upgrade)
display_markdown_message(f"Command Upgrade executed successfully.")
if requirements_file_downloaded:
command_output,_ = self.code_interpreter.execute_command(command_pip_requirements)
display_markdown_message(f"Command Requirements executed successfully.")
else:
self.logger.warn(f"Requirements file not downloaded.")
display_markdown_message(f"Warning: Requirements file not downloaded.")

if command_output:
self.logger.info(f"Command executed successfully.")
display_code(command_output)
self.logger.info(f"Output: {command_output[:100]}")
self.utility_manager.upgrade_interpreter()
continue

# EXECUTE - Command section.
Expand Down
41 changes: 36 additions & 5 deletions libs/utility_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import platform
import re
import subprocess
from libs.code_interpreter import CodeInterpreter
from libs.logger import Logger
import csv
import glob
from datetime import datetime

from libs.markdown_code import display_markdown_message
from libs.markdown_code import display_code, display_markdown_message

class UtilityManager:
logger = None
def __init__(self):
try:
if not os.path.exists('logs'):
Expand Down Expand Up @@ -222,17 +224,46 @@ def clear_screen(self):

# method to download file from Web and save it

def download_file(self,url,file_name):
@staticmethod
def _download_file(url,file_name):
try:
logger = Logger.initialize_logger("logs/interpreter.log")
import requests
self.logger.info(f"Downloading file: {url}")
logger.info(f"Downloading file: {url}")
response = requests.get(url, allow_redirects=True)
response.raise_for_status()

with open(file_name, 'wb') as file:
file.write(response.content)
self.logger.info(f"Reuquirements.txt file downloaded.")
logger.info(f"Reuquirements.txt file downloaded.")
return True
except Exception as exception:
self.logger.error(f"Error in downloading file: {str(exception)}")
logger.error(f"Error in downloading file: {str(exception)}")
return False

@staticmethod
def upgrade_interpreter():
code_interpreter = CodeInterpreter()
logger = Logger.initialize_logger("logs/interpreter.log")
# Download the requirements file
requirements_file_url = 'https://raw.githubusercontent.com/haseeb-heaven/code-interpreter/main/requirements.txt'
requirements_file_downloaded = UtilityManager._download_file(requirements_file_url,'requirements.txt')

# Commands to execute.
command_pip_upgrade = 'pip install open-code-interpreter --upgrade'
command_pip_requirements = 'pip install -r requirements.txt --upgrade'

# Execute the commands.
command_output,_ = code_interpreter.execute_command(command_pip_upgrade)
display_markdown_message(f"Command Upgrade executed successfully.")
if requirements_file_downloaded:
command_output,_ = code_interpreter.execute_command(command_pip_requirements)
display_markdown_message(f"Command Requirements executed successfully.")
else:
logger.warn(f"Requirements file not downloaded.")
display_markdown_message(f"Warning: Requirements file not downloaded.")

if command_output:
logger.info(f"Command executed successfully.")
display_code(command_output)
logger.info(f"Output: {command_output[:100]}")

0 comments on commit 61c4aa2

Please sign in to comment.