Install_dependencies.py

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

Raw Paste

Comments 0
Login to post a comment.
  • No comments yet. Be the first.
Login to post a comment. Login or Register
We use cookies. To comply with GDPR in the EU and the UK we have to show you these.

We use cookies and similar technologies to keep this website functional (including spam protection via Google reCAPTCHA or Cloudflare Turnstile), and — with your consent — to measure usage and show ads. See Privacy.