Repository navigation
Expand file tree
/
Copy pathsetup_modified.py
More file actions
181 lines (146 loc) · 6.33 KB
/
Copy pathsetup_modified.py
File metadata and controls
181 lines (146 loc) · 6.33 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python3
"""
Setup script for the Data Science Agent.
This script installs dependencies and creates the necessary scripts.
It works on Mac, Windows, and Linux.
"""
import os
import sys
import platform
import subprocess
import shutil
from pathlib import Path
# Define the project directory
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
DS_PROJECT_DIR = os.path.join(PROJECT_DIR, "ds_project")
def print_step(message):
"""Print a step message with formatting."""
print(f"\n{'='*80}\n{message}\n{'='*80}")
def run_command(command, shell=False, cwd=None):
"""Run a command and return the output."""
try:
if shell:
process = subprocess.run(command, shell=True, check=True, text=True, capture_output=True, cwd=cwd)
else:
process = subprocess.run(command, check=True, text=True, capture_output=True, cwd=cwd)
return process.stdout
except subprocess.CalledProcessError as e:
print(f"Error executing command: {e}")
print(f"Command output: {e.stdout}")
print(f"Command error: {e.stderr}")
sys.exit(1)
def check_python_version():
"""Check if Python version is 3.8 or higher."""
print_step("Checking Python version")
major, minor = sys.version_info[:2]
if major < 3 or (major == 3 and minor < 8):
print(f"Error: Python 3.8 or higher is required. You have Python {major}.{minor}.")
sys.exit(1)
print(f"Python version {major}.{minor} detected. ✓")
def create_virtual_environment():
"""Create a virtual environment using venv."""
print_step("Creating virtual environment")
venv_dir = os.path.join(DS_PROJECT_DIR, "venv")
if os.path.exists(venv_dir):
print(f"Virtual environment already exists at {venv_dir}. ✓")
return
try:
import venv
venv.create(venv_dir, with_pip=True)
print(f"Virtual environment created at {venv_dir}. ✓")
except Exception as e:
print(f"Error creating virtual environment: {e}")
sys.exit(1)
def install_dependencies():
"""Install dependencies using pip."""
print_step("Installing dependencies")
# Get the path to the requirements.txt file
requirements_path = os.path.join(DS_PROJECT_DIR, "requirements.txt")
# Determine the pip executable path based on the platform
if platform.system() == "Windows":
pip_path = os.path.join(DS_PROJECT_DIR, "venv", "Scripts", "pip")
else:
pip_path = os.path.join(DS_PROJECT_DIR, "venv", "bin", "pip")
# Install dependencies
try:
run_command([pip_path, "install", "-r", requirements_path])
print("Dependencies installed successfully. ✓")
except Exception as e:
print(f"Error installing dependencies: {e}")
sys.exit(1)
def create_run_script():
"""Create platform-specific run scripts."""
print_step("Creating run scripts")
# Create run script for Windows
with open(os.path.join(DS_PROJECT_DIR, "run.bat"), "w") as f:
f.write('@echo off\n')
f.write('echo Starting Data Science Agent...\n')
f.write('call venv\\Scripts\\activate.bat\n')
f.write('streamlit run app.py\n')
# Create run script for macOS/Linux
with open(os.path.join(DS_PROJECT_DIR, "run.sh"), "w") as f:
f.write('#!/bin/bash\n')
f.write('echo "Starting Data Science Agent..."\n')
f.write('source venv/bin/activate\n')
f.write('streamlit run app.py\n')
# Make the shell script executable on Unix-like systems
if platform.system() != "Windows":
os.chmod(os.path.join(DS_PROJECT_DIR, "run.sh"), 0o755)
print("Run scripts created successfully. ✓")
def create_cli_script():
"""Create platform-specific CLI scripts."""
print_step("Creating CLI scripts")
# Create CLI script for Windows
with open(os.path.join(DS_PROJECT_DIR, "analyze.bat"), "w") as f:
f.write('@echo off\n')
f.write('call venv\\Scripts\\activate.bat\n')
f.write('python run_analysis.py %*\n')
# Create CLI script for macOS/Linux
with open(os.path.join(DS_PROJECT_DIR, "analyze.sh"), "w") as f:
f.write('#!/bin/bash\n')
f.write('source venv/bin/activate\n')
f.write('python run_analysis.py "$@"\n')
# Make the shell script executable on Unix-like systems
if platform.system() != "Windows":
os.chmod(os.path.join(DS_PROJECT_DIR, "analyze.sh"), 0o755)
print("CLI scripts created successfully. ✓")
def create_root_scripts():
"""Create scripts in the root directory that call the scripts in ds_project."""
print_step("Creating root scripts")
# Create run script for Windows in root directory
with open(os.path.join(PROJECT_DIR, "run.bat"), "w") as f:
f.write('@echo off\n')
f.write('cd /d "%~dp0ds_project"\n') # Change to ds_project directory
f.write('call run.bat\n')
# Create run script for macOS/Linux in root directory
with open(os.path.join(PROJECT_DIR, "run.sh"), "w") as f:
f.write('#!/bin/bash\n')
f.write('cd "$(dirname "$0")/ds_project" || exit\n') # Change to ds_project directory
f.write('./run.sh\n')
# Make the shell script executable on Unix-like systems
if platform.system() != "Windows":
os.chmod(os.path.join(PROJECT_DIR, "run.sh"), 0o755)
print("Root scripts created successfully. ✓")
def main():
"""Main function to set up the Data Science Agent."""
print("\nData Science Agent Setup\n")
check_python_version()
create_virtual_environment()
install_dependencies()
create_run_script()
create_cli_script()
create_root_scripts()
print_step("Setup completed successfully!")
# Print instructions
system = platform.system()
if system == "Windows":
print("\nTo run the Data Science Agent:")
print(" - Web interface: run.bat")
print(" - Command line: ds_project\\analyze.bat --file data/train.csv --objective \"Analyze data\" --api-key YOUR_API_KEY")
else:
print("\nTo run the Data Science Agent:")
print(" - Web interface: ./run.sh")
print(" - Command line: ./ds_project/analyze.sh --file data/train.csv --objective \"Analyze data\" --api-key YOUR_API_KEY")
print("\nEnjoy using the Data Science Agent!")
if __name__ == "__main__":
main()