added [browser] extra; refac the pip install utils

This commit is contained in:
Paul Gauthier 2024-07-14 17:22:17 +01:00
parent 057c1af449
commit 76dad84285
13 changed files with 257 additions and 151 deletions

View file

@ -6,8 +6,8 @@ from pathlib import Path
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
@ -662,19 +662,22 @@ class Commands:
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,

View file

@ -6,7 +6,7 @@ from pathlib import Path
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
@ -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")

View file

@ -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
@ -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"
@ -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}")

View file

@ -1,8 +1,8 @@
import itertools
import os
import subprocess
import sys
import tempfile
import itertools
from pathlib import Path
import git
@ -182,7 +182,6 @@ def split_chat_history_markdown(text, include_tool=False):
def get_pip_install(args):
cmd = [
sys.executable,
"-m",
@ -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)
@ -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]}")

View file

@ -49,7 +49,8 @@ Newer aider version v{latest_version} is available. To upgrade, run:
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()