- 1#!/usr/bin/env python3
- 2# Script to install dependencies for the Grok Flask API.
- 3# Run this manually after creating a virtualenv: python3 -m venv env; source env/bin/activate; python3 install_dependencies.py
- 4
- 5import subprocess
- 6import sys
- 7
- 8def install_dependencies():
- 9 # List of required packages with pinned versions for stability
- 10 packages = [
- 11 'flask>=3.0.0',
- 12 'openai>=1.0.0',
- 13 'gunicorn>=22.0.0',
- 14 'requests>=2.31.0',
- 15 'bleach>=6.1.0',
- 16 'geopy>=2.4.0', # For geocoding in weather
- 17 'cachetools>=5.3.0', # For TTL caches
- 18 'redis>=5.0.0', # For shared state in prod
- 19 'huggingface-hub>=0.23.0', # For HF image gen
- 20 'Pillow>=10.0.0' # For image handling in huggingface_hub
- 21 ]
- 22 # Check if pip is available
- 23 try:
- 24 subprocess.check_output([sys.executable, '-m', 'pip', '--version'])
- 25 except subprocess.CalledProcessError:
- 26 print("pip not found. Ensure Python is installed correctly.")
- 27 sys.exit(1)
- 28
- 29 # Get list of installed packages for more accurate check
- 30 installed_packages = {}
- 31 installed_output = subprocess.check_output([sys.executable, '-m', 'pip', 'list', '--format=freeze']).decode('utf-8')
- 32 for line in installed_output.splitlines():
- 33 if '==' in line:
- 34 name, version = line.split('==', 1)
- 35 installed_packages[name.lower()] = version
- 36
- 37 for spec in packages:
- 38 name = spec.split('>')[0].split('=')[0].lower() # Normalize name
- 39 if name in installed_packages:
- 40 print(f"{name} already installed (version {installed_packages[name]}).")
- 41 continue
- 42 print(f"Installing {spec}...")
- 43 try:
- 44 subprocess.check_call([sys.executable, '-m', 'pip', 'install', spec])
- 45 print(f"Installed {spec}.")
- 46 except subprocess.CalledProcessError as e:
- 47 print(f"Failed to install {spec}: {e}")
- 48 sys.exit(1)
- 49
- 50if __name__ == '__main__':
- 51 install_dependencies()
Raw Paste