Skip to content

Commit

Permalink
added [browser] extra; refac the pip install utils
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-gauthier committed Jul 14, 2024
1 parent 057c1af commit 76dad84
Show file tree
Hide file tree
Showing 13 changed files with 257 additions and 151 deletions.
31 changes: 17 additions & 14 deletions aider/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import git

from aider import models, prompts, voice
from aider.help import Help, PipInstallHF
from aider import models, prompts, utils, voice
from aider.help import Help
from aider.llm import litellm
from aider.scrape import Scraper
from aider.utils import is_image_file
Expand Down Expand Up @@ -662,19 +662,22 @@ def cmd_help(self, args):
from aider.coders import Coder

if not self.help:
try:
self.help = Help()
except PipInstallHF as err:
self.io.tool_error(str(err))
if self.io.confirm_ask("Run pip install?", default="y"):
try:
self.help = Help(pip_install=True)
except PipInstallHF:
pass
pip_install_cmd = [
"aider-chat[hf-embed]",
"--extra-index-url",
"https://download.pytorch.org/whl/cpu",
]
res = utils.check_pip_install_extra(
self.io,
"llama_index.embeddings.huggingface",
"To use interactive /help you need to install HuggingFace embeddings",
pip_install_cmd,
)
if not res:
self.io.tool_error("Unable to initialize interactive help.")
return

if not self.help:
self.io.tool_error("Unable to initialize interactive help.")
return
self.help = Help()

coder = Coder.create(
main_model=self.coder.main_model,
Expand Down
31 changes: 3 additions & 28 deletions aider/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import importlib_resources

from aider import __version__, utils
from aider import __version__
from aider.dump import dump # noqa: F401
from aider.help_pats import exclude_website_pats

Expand Down Expand Up @@ -87,35 +87,10 @@ def get_index():
return index


class PipInstallHF(Exception):
pass


pip_install_cmd = [
"aider-chat[hf-embed]",
"--extra-index-url",
"https://download.pytorch.org/whl/cpu",
]

pip_install_error = """
To use interactive /help you need to install HuggingFace embeddings:
{cmd}
""" # noqa: E231


class Help:
def __init__(self, pip_install=False):
cmd = utils.get_pip_install(pip_install_cmd)
if pip_install:
utils.run_install(cmd)

try:
from llama_index.core import Settings
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
except ImportError:
raise PipInstallHF(pip_install_error.format(cmd=' '.join(cmd)))
from llama_index.core import Settings
from llama_index.embeddings.huggingface import HuggingFaceEmbedding

os.environ["TOKENIZERS_PARALLELISM"] = "true"
Settings.embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
Expand Down
19 changes: 15 additions & 4 deletions aider/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ def scrub_sensitive_info(args, text):
return text


def check_streamlit_install(io):
return utils.check_pip_install_extra(
io,
"streamlit",
"You need to install the aider browser feature",
["aider-chat[browser]"],
)


def launch_gui(args):
from streamlit.web import cli

Expand Down Expand Up @@ -318,10 +327,6 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F

litellm.client_session = httpx.Client(verify=False)

if args.gui and not return_coder:
launch_gui(argv)
return

if args.dark_mode:
args.user_input_color = "#32FF32"
args.tool_error_color = "#FF3333"
Expand Down Expand Up @@ -355,6 +360,12 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F
editingmode=editing_mode,
)

if args.gui and not return_coder:
if not check_streamlit_install(io):
return
launch_gui(argv)
return

for fname in loaded_dotenvs:
io.tool_output(f"Loaded {fname}")

Expand Down
52 changes: 45 additions & 7 deletions aider/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import itertools
import os
import subprocess
import sys
import tempfile
import itertools
from pathlib import Path

import git
Expand Down Expand Up @@ -182,7 +182,6 @@ def append_msg(role, lines):


def get_pip_install(args):

cmd = [
sys.executable,
"-m",
Expand All @@ -192,14 +191,22 @@ def get_pip_install(args):
cmd += args
return cmd


def run_install(cmd):
print()
print("Installing: ", ' '.join(cmd))
print("Installing: ", " ".join(cmd))

try:
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1, universal_newlines=True)
output = []
spinner = itertools.cycle(['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'])
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1,
universal_newlines=True,
)
spinner = itertools.cycle(["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"])

for line in process.stdout:
output.append(line)
Expand All @@ -208,14 +215,45 @@ def run_install(cmd):
return_code = process.wait()

if return_code == 0:
print("\rInstallation completed successfully.")
print("\rInstallation complete.")
print()
return True
return True, output

except subprocess.CalledProcessError as e:
print(f"\nError running pip install: {e}")

print("\nInstallation failed.\n")

return False, output


def check_pip_install_extra(io, module, prompt, pip_install_cmd):
try:
__import__(module)
return True
except (ImportError, ModuleNotFoundError):
pass

cmd = get_pip_install(pip_install_cmd)

text = f"{prompt}:\n\n{' '.join(cmd)}\n\n"
io.tool_error(text)

if not io.confirm_ask("Run pip install?", default="y"):
return

success, output = run_install(cmd)
if not success:
return

try:
__import__(module)
return True
except (ImportError, ModuleNotFoundError):
pass

for line in output:
print(line)

print()
print(f"Failed to install {pip_install_cmd[0]}")
3 changes: 2 additions & 1 deletion aider/versioncheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def check_version(io, just_check=False):
io.tool_error(text)

if io.confirm_ask("Run pip install?"):
if utils.run_install(cmd):
success, _output = utils.run_install(cmd)
if success:
io.tool_output("Re-run aider to use new version.")
sys.exit()

Expand Down
Loading

0 comments on commit 76dad84

Please sign in to comment.