Spaces:
Sleeping
Sleeping
File size: 11,417 Bytes
e2a1a46 | 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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | #!/bin/bash
# AI Prescription Assistant - Automated Setup Script
# This script sets up the complete application environment
echo "π§ π AI Prescription Assistant - Setup Script"
echo "=============================================="
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_step() {
echo -e "${BLUE}[STEP]${NC} $1"
}
# Check if running on supported OS
check_os() {
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
OS="linux"
print_status "Detected Linux OS"
elif [[ "$OSTYPE" == "darwin"* ]]; then
OS="macos"
print_status "Detected macOS"
elif [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "win32" ]]; then
OS="windows"
print_status "Detected Windows OS"
else
print_error "Unsupported operating system: $OSTYPE"
exit 1
fi
}
# Check if Python is installed
check_python() {
print_step "Checking Python installation..."
if command -v python3 &> /dev/null; then
PYTHON_CMD="python3"
elif command -v python &> /dev/null; then
PYTHON_CMD="python"
else
print_error "Python is not installed. Please install Python 3.8+ first."
exit 1
fi
# Check Python version
PYTHON_VERSION=$($PYTHON_CMD --version 2>&1 | cut -d' ' -f2)
print_status "Found Python $PYTHON_VERSION"
# Check if version is 3.8+
if ! $PYTHON_CMD -c "import sys; exit(0 if sys.version_info >= (3, 8) else 1)"; then
print_error "Python 3.8 or higher is required. Found: $PYTHON_VERSION"
exit 1
fi
}
# Install system dependencies
install_system_deps() {
print_step "Installing system dependencies..."
if [[ "$OS" == "linux" ]]; then
# Check if running with sudo privileges for system packages
if [[ $EUID -eq 0 ]]; then
apt update
apt install -y tesseract-ocr tesseract-ocr-eng python3-dev python3-pip python3-venv
print_status "System dependencies installed"
else
print_warning "Need sudo privileges to install system packages"
echo "Please run the following commands manually:"
echo "sudo apt update"
echo "sudo apt install -y tesseract-ocr tesseract-ocr-eng python3-dev python3-pip python3-venv"
read -p "Have you installed the system dependencies? (y/n): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_error "System dependencies are required. Exiting."
exit 1
fi
fi
elif [[ "$OS" == "macos" ]]; then
if command -v brew &> /dev/null; then
brew install tesseract
print_status "Tesseract installed via Homebrew"
else
print_warning "Homebrew not found. Please install Tesseract manually:"
echo "1. Install Homebrew: /bin/bash -c \"\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\""
echo "2. Install Tesseract: brew install tesseract"
exit 1
fi
elif [[ "$OS" == "windows" ]]; then
print_warning "Please install Tesseract manually on Windows:"
echo "1. Download from: https://github.com/UB-Mannheim/tesseract/wiki"
echo "2. Add tesseract.exe to your PATH"
read -p "Have you installed Tesseract? (y/n): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_error "Tesseract is required. Exiting."
exit 1
fi
fi
}
# Setup Python virtual environment
setup_venv() {
print_step "Setting up Python virtual environment..."
if [ -d "venv" ]; then
print_warning "Virtual environment already exists. Removing..."
rm -rf venv
fi
$PYTHON_CMD -m venv venv
# Activate virtual environment
if [[ "$OS" == "windows" ]]; then
source venv/Scripts/activate
else
source venv/bin/activate
fi
print_status "Virtual environment created and activated"
}
# Install Python dependencies
install_python_deps() {
print_step "Installing Python dependencies..."
# Upgrade pip
pip install --upgrade pip
# Install core dependencies first
print_status "Installing core dependencies..."
pip install fastapi uvicorn python-multipart jinja2
# Install OCR dependencies
print_status "Installing OCR dependencies..."
pip install pillow opencv-python numpy
# Install EasyOCR (may take time)
print_status "Installing EasyOCR (this may take several minutes)..."
pip install easyocr
# Install Tesseract Python wrapper
pip install pytesseract
# Install NLP dependencies
print_status "Installing NLP dependencies..."
pip install spacy nltk regex
# Install PDF generation
pip install reportlab fpdf2
# Install utility packages
pip install pandas python-dotenv pydantic aiofiles
print_status "All Python dependencies installed"
}
# Create necessary directories
create_directories() {
print_step "Creating necessary directories..."
mkdir -p uploads
mkdir -p outputs
mkdir -p static/images
mkdir -p sample_prescriptions
mkdir -p logs
print_status "Directories created"
}
# Create sample prescription image
create_sample_image() {
print_step "Creating sample prescription image..."
# Create a simple sample prescription using Python
cat > create_sample.py << 'EOF'
from PIL import Image, ImageDraw, ImageFont
import os
# Create sample prescription
width, height = 600, 800
image = Image.new('RGB', (width, height), 'white')
draw = ImageDraw.Draw(image)
# Use default font
font = ImageFont.load_default()
# Draw prescription content
y_pos = 50
draw.text((50, y_pos), "MEDICAL CENTER", fill='black', font=font)
y_pos += 40
draw.text((50, y_pos), "Dr. Sarah Johnson, MD", fill='black', font=font)
y_pos += 30
draw.text((50, y_pos), "123 Health Street, Medical City", fill='black', font=font)
y_pos += 50
draw.text((50, y_pos), "Patient: John Smith", fill='black', font=font)
y_pos += 30
draw.text((50, y_pos), "DOB: 01/15/1980", fill='black', font=font)
y_pos += 25
draw.text((50, y_pos), "Date: 12/20/2024", fill='black', font=font)
y_pos += 50
draw.text((50, y_pos), "Rx:", fill='black', font=font)
y_pos += 30
draw.text((70, y_pos), "Amoxicillin 500mg", fill='black', font=font)
y_pos += 25
draw.text((70, y_pos), "Take 1 tablet twice daily", fill='black', font=font)
y_pos += 20
draw.text((70, y_pos), "for 10 days with food", fill='black', font=font)
y_pos += 40
draw.text((70, y_pos), "Ibuprofen 200mg", fill='black', font=font)
y_pos += 25
draw.text((70, y_pos), "1-0-1 for 5 days", fill='black', font=font)
y_pos += 20
draw.text((70, y_pos), "after meals", fill='black', font=font)
# Save
image.save("sample_prescriptions/sample_prescription.jpg", "JPEG", quality=95)
print("Sample prescription created")
EOF
$PYTHON_CMD create_sample.py
rm create_sample.py
print_status "Sample prescription image created"
}
# Test the installation
test_installation() {
print_step "Testing installation..."
# Test OCR engines
print_status "Testing OCR engines..."
cat > test_ocr.py << 'EOF'
import sys
try:
import easyocr
print("β EasyOCR available")
except ImportError as e:
print(f"β EasyOCR error: {e}")
try:
import pytesseract
pytesseract.get_tesseract_version()
print("β Tesseract available")
except Exception as e:
print(f"β Tesseract error: {e}")
try:
import cv2
print("β OpenCV available")
except ImportError as e:
print(f"β OpenCV error: {e}")
try:
from PIL import Image
print("β PIL/Pillow available")
except ImportError as e:
print(f"β PIL error: {e}")
print("Basic test completed")
EOF
$PYTHON_CMD test_ocr.py
rm test_ocr.py
print_status "Installation test completed"
}
# Create startup script
create_startup_script() {
print_step "Creating startup script..."
cat > start.sh << 'EOF'
#!/bin/bash
echo "π§ π Starting AI Prescription Assistant..."
# Activate virtual environment
if [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "win32" ]]; then
source venv/Scripts/activate
else
source venv/bin/activate
fi
# Start the application
python app.py
EOF
chmod +x start.sh
# Create Windows batch file
cat > start.bat << 'EOF'
@echo off
echo π§ π Starting AI Prescription Assistant...
REM Activate virtual environment
call venv\Scripts\activate.bat
REM Start the application
python app.py
pause
EOF
print_status "Startup scripts created"
}
# Create configuration file
create_config() {
print_step "Creating configuration file..."
cat > config.json << 'EOF'
{
"ocr": {
"languages": ["en"],
"confidence_threshold": 0.3,
"use_gpu": false,
"fallback_to_tesseract": true
},
"nlp": {
"confidence_threshold": 0.5,
"enable_spacy": false
},
"file_handling": {
"max_file_size_mb": 10,
"allowed_extensions": [".jpg", ".jpeg", ".png", ".pdf"],
"upload_dir": "uploads",
"output_dir": "outputs"
},
"processing": {
"timeout_seconds": 60,
"enable_batch_processing": true,
"max_batch_size": 10
},
"export": {
"enable_pdf": true,
"enable_json": true,
"include_confidence_scores": true
}
}
EOF
print_status "Configuration file created"
}
# Main setup function
main() {
echo ""
print_step "Starting setup process..."
# Check prerequisites
check_os
check_python
# Install dependencies
install_system_deps
# Setup Python environment
setup_venv
# Activate virtual environment for the rest of the script
if [[ "$OS" == "windows" ]]; then
source venv/Scripts/activate
else
source venv/bin/activate
fi
install_python_deps
# Setup application
create_directories
create_sample_image
create_startup_script
create_config
# Test installation
test_installation
# Final instructions
echo ""
echo "π Setup completed successfully!"
echo "=============================================="
print_status "AI Prescription Assistant is ready to use!"
echo ""
echo "π Quick Start:"
echo " 1. Start the application:"
if [[ "$OS" == "windows" ]]; then
echo " ./start.bat"
else
echo " ./start.sh"
fi
echo " 2. Open browser: http://localhost:8000"
echo " 3. Upload a prescription image"
echo " 4. Click 'Extract Text' then 'AI Analysis'"
echo ""
echo "π Sample prescription available in: sample_prescriptions/"
echo "π Full documentation: README.md"
echo "π API Documentation: http://localhost:8000/api/docs"
echo ""
echo "β οΈ Medical Disclaimer: This tool is for informational purposes only."
echo " Always consult healthcare professionals for medical advice."
echo ""
print_status "Happy analyzing! π§ π"
}
# Run main function
main "$@" |