app / setup.sh
sabareesh4317
Initial deploy (Clean)
e2a1a46
Raw History Blame Contribute Delete
11.4 kB
#!/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 "$@"