-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_env.py
88 lines (73 loc) · 3.02 KB
/
setup_env.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# setup_env.py
import os
import sys
import subprocess
import logging
import platform
import json
def setup_logging():
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s:%(message)s',
handlers=[
logging.StreamHandler(sys.stdout)
]
)
def create_virtual_env(venv_path):
import venv
try:
logging.info(f"Creating virtual environment at '{venv_path}'...")
builder = venv.EnvBuilder(with_pip=True)
builder.create(venv_path)
logging.info("Virtual environment created successfully.")
except Exception as e:
logging.error(f"Failed to create virtual environment: {e}")
sys.exit(1)
def install_requirements(venv_path, requirements_file):
if not os.path.exists(requirements_file):
logging.error(f"Requirements file '{requirements_file}' not found.")
sys.exit(1)
if platform.system() == 'Windows':
pip_executable = os.path.join(venv_path, 'Scripts', 'pip.exe')
else:
pip_executable = os.path.join(venv_path, 'bin', 'pip')
try:
logging.info(f"Installing dependencies from '{requirements_file}'...")
subprocess.check_call([pip_executable, 'install', '--upgrade', 'pip'])
subprocess.check_call([pip_executable, 'install', '-r', requirements_file])
logging.info("Dependencies installed successfully.")
except subprocess.CalledProcessError as e:
logging.error(f"Failed to install dependencies: {e}")
sys.exit(1)
def download_nltk_data(venv_path):
try:
if platform.system() == 'Windows':
python_executable = os.path.join(venv_path, 'Scripts', 'python.exe')
else:
python_executable = os.path.join(venv_path, 'bin', 'python')
logging.info("Downloading NLTK data...")
subprocess.check_call([python_executable, '-m', 'nltk', 'download', 'punkt'])
subprocess.check_call([python_executable, '-m', 'nltk', 'download', 'stopwords'])
logging.info("NLTK data downloaded successfully.")
except subprocess.CalledProcessError as e:
logging.error(f"Failed to download NLTK data: {e}")
sys.exit(1)
def main():
setup_logging()
project_root = os.path.dirname(os.path.abspath(__file__))
venv_path = os.path.join(project_root, 'venv')
requirements_file = os.path.join(project_root, 'TinyAGI/requirements.txt')
if os.path.exists(venv_path):
logging.info(f"Virtual environment already exists at '{venv_path}'. Skipping creation.")
else:
create_virtual_env(venv_path)
install_requirements(venv_path, requirements_file)
download_nltk_data(venv_path)
logging.info("\nEnvironment setup is complete!")
if platform.system() == 'Windows':
activate_command = f"{venv_path}\\Scripts\\activate.bat"
else:
activate_command = f"source {venv_path}/bin/activate"
logging.info(f"To activate the virtual environment, run:\n\n {activate_command}\n")
if __name__ == '__main__':
main()