-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
172 lines (148 loc) Β· 5.46 KB
/
setup.py
File metadata and controls
172 lines (148 loc) Β· 5.46 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
#!/usr/bin/env python3
"""
Setup script for Context DB Manager
This script helps you set up the environment and download models.
"""
import os
import sys
import subprocess
from pathlib import Path
def run_command(command, description):
"""Run a command and handle errors."""
print(f"\nπ§ {description}...")
try:
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
print(f"β
{description} completed successfully")
return True
except subprocess.CalledProcessError as e:
print(f"β {description} failed:")
print(f"Error: {e.stderr}")
return False
def check_python_version():
"""Check if Python version is compatible."""
version = sys.version_info
if version.major < 3 or (version.major == 3 and version.minor < 8):
print("β Python 3.8+ is required. Current version:", sys.version)
return False
print(f"β
Python version {version.major}.{version.minor} is compatible")
return True
def install_dependencies():
"""Install Python dependencies."""
requirements_file = Path(__file__).parent / "requirements.txt"
if not requirements_file.exists():
print("β requirements.txt not found!")
return False
return run_command(
f"{sys.executable} -m pip install -r {requirements_file}",
"Installing Python dependencies"
)
def download_embedding_model():
"""Download the default embedding model."""
print("\nπ€ Downloading embedding model...")
print("This may take a few minutes and requires internet connection.")
try:
from sentence_transformers import SentenceTransformer
# Download the default model
model_name = "all-MiniLM-L6-v2"
print(f"Downloading {model_name}...")
model = SentenceTransformer(model_name)
print(f"β
Model {model_name} downloaded successfully!")
print(f" Vector dimension: {model.get_sentence_embedding_dimension()}")
return True
except ImportError:
print("β sentence-transformers not installed. Please install dependencies first.")
return False
except Exception as e:
print(f"β Error downloading model: {e}")
return False
def create_icons():
"""Create extension icons."""
try:
from PIL import Image
print("\nπ¨ Creating extension icons...")
# Import and run the icon creation script
exec(open("create_icons.py").read())
return True
except ImportError:
print("β οΈ Pillow not installed. Icons will not be created.")
print(" You can install it with: pip install Pillow")
return False
except Exception as e:
print(f"β οΈ Error creating icons: {e}")
return False
def create_directories():
"""Create necessary directories."""
print("\nπ Creating directories...")
try:
directories = ["context_dbs", "icons"]
for directory in directories:
Path(directory).mkdir(exist_ok=True)
print("β
Directories created successfully")
return True
except Exception as e:
print(f"β Error creating directories: {e}")
return False
def test_server():
"""Test if the server can start."""
print("\nπ Testing server startup...")
print("This will take a moment to load the embedding model...")
try:
# Import the server to test if it can load
import uvicorn
from fastapi import FastAPI
print("β
Server dependencies are working")
# Test model loading
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("all-MiniLM-L6-v2")
print("β
Embedding model loads successfully")
return True
except Exception as e:
print(f"β Server test failed: {e}")
return False
def main():
"""Main setup function."""
print("π Context DB Manager Setup")
print("=" * 50)
success = True
# Step 1: Check Python version
if not check_python_version():
success = False
return
# Step 2: Create directories
if not create_directories():
success = False
# Step 3: Install dependencies
if not install_dependencies():
success = False
return
# Step 4: Download embedding model
if not download_embedding_model():
success = False
# Step 5: Create icons
create_icons() # This is optional, so don't fail on errors
# Step 6: Test server
if not test_server():
success = False
# Final status
print("\n" + "=" * 50)
if success:
print("π Setup completed successfully!")
print("\nπ Next steps:")
print("1. Load the Chrome extension:")
print(" - Open chrome://extensions/")
print(" - Enable 'Developer mode'")
print(" - Click 'Load unpacked' and select this folder")
print("\n2. Start the server:")
print(" python server.py")
print("\n3. Start using the extension!")
print(" - Click the extension icon to manage databases")
print(" - Select text on any webpage to save it")
else:
print("β Setup encountered some issues.")
print("Please check the errors above and try again.")
print("\nπ Common solutions:")
print("- Ensure you have Python 3.8+")
print("- Check your internet connection")
print("- Try running: pip install --upgrade pip")
if __name__ == "__main__":
main()