Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
247 lines (211 loc) · 7.89 KB
/
Copy pathsetup.py
File metadata and controls
247 lines (211 loc) · 7.89 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python3
"""
Complete setup script for the Docling Document Processing System
Handles environment configuration, dependency installation, and system validation
"""
import os
import sys
import subprocess
import shutil
from pathlib import Path
def print_header(text):
print(f"\n{'='*60}")
print(f" {text}")
print(f"{'='*60}\n")
def print_step(step_num, text):
print(f"[{step_num}/8] {text}")
def check_python_version():
"""Check if Python version is compatible"""
if sys.version_info < (3, 8):
print("ERROR: Python 3.8 or higher is required")
print(f"Current version: {sys.version}")
return False
print(f"✓ Python version: {sys.version.split()[0]}")
return True
def setup_environment():
"""Setup environment variables"""
env_file = Path("Backend/.env")
env_example = Path("Backend/.env.example")
if not env_file.exists() and env_example.exists():
print("Creating .env file from template...")
shutil.copy(env_example, env_file)
print("✓ Created .env file")
print("\nIMPORTANT: Please edit Backend/.env and add your Gemini API key")
print("You can get a free API key from: https://makersuite.google.com/app/apikey")
elif env_file.exists():
print("✓ .env file already exists")
else:
print("⚠ No .env template found, creating basic configuration...")
with open(env_file, 'w') as f:
f.write("""# Gemini API Configuration
GEMINI_API_KEY=your_gemini_api_key_here
# Server Configuration
HOST=0.0.0.0
PORT=8000
DEBUG=true
# Processing Configuration
DEFAULT_THREADS=4
MAX_THREADS=16
DOCUMENT_TIMEOUT=300
""")
print("✓ Created basic .env file")
return True
def install_dependencies():
"""Install Python dependencies"""
requirements_file = Path("Backend/requirements.txt")
if not requirements_file.exists():
print("ERROR: requirements.txt not found")
return False
print("Installing Python dependencies...")
try:
subprocess.run([
sys.executable, "-m", "pip", "install", "-r", str(requirements_file)
], check=True, capture_output=True, text=True)
print("✓ Dependencies installed successfully")
return True
except subprocess.CalledProcessError as e:
print(f"ERROR: Failed to install dependencies: {e}")
print(f"STDOUT: {e.stdout}")
print(f"STDERR: {e.stderr}")
return False
def validate_docling_installation():
"""Validate that Docling is properly installed"""
try:
# Test basic Docling import
subprocess.run([
sys.executable, "-c",
"from docling.document_converter import DocumentConverter; print('✓ Docling import successful')"
], check=True, capture_output=True, text=True, cwd="Backend")
return True
except subprocess.CalledProcessError as e:
print(f"ERROR: Docling installation validation failed: {e}")
print("Attempting to install Docling manually...")
try:
subprocess.run([sys.executable, "-m", "pip", "install", "docling"], check=True)
print("✓ Docling installed successfully")
return True
except subprocess.CalledProcessError:
print("ERROR: Failed to install Docling")
return False
def validate_server_setup():
"""Validate that the server can start"""
try:
# Test FastAPI import and basic server validation
result = subprocess.run([
sys.executable, "-c", """
import sys
sys.path.insert(0, 'Backend')
from fastapi import FastAPI
import uvicorn
print('✓ Server components validated')
"""
], check=True, capture_output=True, text=True)
return True
except subprocess.CalledProcessError as e:
print(f"ERROR: Server validation failed: {e}")
return False
def create_startup_scripts():
"""Create convenient startup scripts"""
# Windows batch file
batch_content = """@echo off
cd /d "%~dp0"
echo Starting Docling API Server...
python Backend/api_server.py
pause
"""
with open("start_windows.bat", "w") as f:
f.write(batch_content)
# Unix shell script
shell_content = """#!/bin/bash
cd "$(dirname "$0")"
echo "Starting Docling API Server..."
python3 Backend/api_server.py
"""
with open("start_unix.sh", "w") as f:
f.write(shell_content)
# Make shell script executable
try:
os.chmod("start_unix.sh", 0o755)
except:
pass
print("✓ Created startup scripts (start_windows.bat, start_unix.sh)")
def validate_frontend_files():
"""Validate frontend files are present"""
frontend_file = Path("Frontend/Frontend.html")
if not frontend_file.exists():
print("ERROR: Frontend.html not found")
return False
print("✓ Frontend files validated")
return True
def run_basic_server_test():
"""Run a basic server test to ensure everything works"""
print("Running basic server test...")
try:
# Start server test in background for a few seconds
proc = subprocess.Popen([
sys.executable, "Backend/test_server.py"
], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# Give it time to start
import time
time.sleep(3)
# Try to connect
import urllib.request
try:
response = urllib.request.urlopen("http://localhost:8000", timeout=5)
if response.getcode() == 200:
print("✓ Basic server test passed")
success = True
else:
print(f"⚠ Server responded with status code: {response.getcode()}")
success = False
except Exception as e:
print(f"⚠ Could not connect to test server: {e}")
success = False
# Terminate test server
proc.terminate()
proc.wait(timeout=5)
return success
except Exception as e:
print(f"⚠ Server test encountered issues: {e}")
return False
def main():
"""Main setup function"""
print_header("Docling Document Processing System Setup")
setup_steps = [
("Checking Python version", check_python_version),
("Setting up environment", setup_environment),
("Installing dependencies", install_dependencies),
("Validating Docling installation", validate_docling_installation),
("Validating server setup", validate_server_setup),
("Creating startup scripts", create_startup_scripts),
("Validating frontend files", validate_frontend_files),
("Running basic tests", run_basic_server_test),
]
success_count = 0
for i, (description, function) in enumerate(setup_steps, 1):
print_step(i, description)
try:
if function():
success_count += 1
else:
print("❌ Step failed")
except Exception as e:
print(f"❌ Step failed with exception: {e}")
print_header("Setup Complete")
print(f"Successfully completed: {success_count}/{len(setup_steps)} steps")
if success_count == len(setup_steps):
print("🎉 All setup steps completed successfully!")
print("\nNext steps:")
print("1. Edit Backend/.env and add your Gemini API key")
print("2. Run: python Backend/api_server.py")
print("3. Open your browser to: http://localhost:8000")
print("\nAlternatively, use the startup scripts:")
print("- Windows: double-click start_windows.bat")
print("- Unix/Mac: ./start_unix.sh")
else:
print("⚠ Some setup steps failed. Please review the errors above.")
print("You may still be able to run the system, but some features might not work.")
return success_count == len(setup_steps)
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)