File size: 4,353 Bytes
404fa56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/sh

# Function to check and set environment variables
set_env_vars() {
  if [ "$(uname)" = "Darwin" ]; then
    # macOS specific env:
    export PYTORCH_ENABLE_MPS_FALLBACK=1
    export PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.0
  elif [ "$(uname)" != "Linux" ]; then
    echo "Unsupported operating system."
    exit 1
  fi
}

# Function to activate or create virtual environment
handle_venv() {
  printf "Checking for virtual environment..."
  if [ -d ".venv" ]; then
    printf " Found.\nActivating venv..."
    . .venv/bin/activate
    printf " Done.\n"
  else
    printf " Not found.\n"
    requirements_file="requirements.txt"

    pyenv_exists=0
    if command -v pyenv >/dev/null 2>&1; then
      pyenv_exists=1
    fi

    # Check if pyenv is installed and version 3.8 is available
    pyenv_v38_installed=0
    if [ $pyenv_exists -eq 1 ]; then
      if pyenv versions --bare | grep -q "3.8"; then
        pyenv_v38_installed=1
      fi
    fi

    python_v38_exists=0
    if command -v python3.8 >/dev/null 2>&1; then
      python_v38_exists=1
    fi


    # Check if Python 3.8 is installed
    printf "Checking for Python 3.8..."
    if ! [ $pyenv_v38_installed -eq 1 ] && ! [ $python_v38_exists -eq 1 ]; then
      printf " Not found.\nInstalling Python 3.8..."
      if [ "$(uname)" = "Darwin" ] && command -v brew >/dev/null 2>&1; then
        echo "Using Homebrew..."
        brew install [email protected]
      elif [ "$(uname)" = "Linux" ]; then
        if command -v apt-get >/dev/null 2>&1; then
          echo "Using apt..."
          sudo apt-get update
          sudo apt-get install python3.8
        elif command -v pacman >/dev/null 2>&1; then
          echo "Using pacman..."
          sudo pacman -Syu python38
        elif command -v dnf >/dev/null 2>&1; then
          echo "Using dnf..."
          sudo dnf install python38
        else
          echo "Unsupported package manager for automatic Python 3.8 installation."
          echo "Please install Python 3.8 manually."
          exit 1
        fi
      else
        echo "Unsupported operating system for automatic Python 3.8 installation."
        echo "Please install Python 3.8 manually."
        exit 1
      fi
    fi
    printf " Found.\n"

    printf "Creating venv..."
    python3.8 -m venv .venv
    . .venv/bin/activate
    printf " Done.\n"

    # update pip
    printf "Updating pip..."
    python3.8 -m pip install --upgrade pip > /dev/null 2>> pkgerr.log
    printf " Done.\n"

    # Check if required packages are installed and install them if not
    echo "Checking for required packages..."
    if [ -f "${requirements_file}" ]; then
      installed_packages=$(python3.8 -m pip freeze)
      while IFS= read -r package; do
        expr "${package}" : "^#.*" > /dev/null && continue
        package_name=$(echo "${package}" | sed 's/[<>=!].*//')
        if ! echo "${installed_packages}" | grep -q "${package_name}"; then
          printf "%s not found. Installing..." "${package_name}"
          python3.8 -m pip install --upgrade "${package}" > /dev/null 2>> pkgerr.log
          printf " Done.\n"
        fi
      done < "${requirements_file}"
    else
      echo "${requirements_file} not found. Please ensure the requirements file with required packages exists."
      exit 1
    fi

    if [ -s pkgerr.log ]; then
      echo "Something happened whilst installing packages. Please check pkgerr.log for more details in case of failure."
    fi
  fi
}

# Function to download models
download_models() {
  echo "Checking if models are downloaded..."
  chmod +x tools/dlmodels.sh
  ./tools/dlmodels.sh
  echo "Models downloaded."

  if [ $? -ne 0 ]; then
    exit 1
  fi
}

# Function to run the main script
run_main_script() {
  printf "%0.s=" $(seq 1 "$(tput cols)")
  message="Running main script with args: $passargs"
  printf "%*s\n" $(((${#message}+$(tput cols))/2)) "$message"
  printf "%0.s=" $(seq 1 "$(tput cols)")
  python3.8 infer-web.py --pycmd python3.8 $passargs
}

# Parse command-line arguments
passargs=""

while getopts ":p:" opt; do
  case ${opt} in
    p)
      passargs=$OPTARG
      ;;
    \?)
      echo "Invalid option: $OPTARG" 1>&2
      ;;
    :)
      echo "Invalid option: $OPTARG requires an argument" 1>&2
      ;;
  esac
done
shift $((OPTIND -1))

# Call functions
set_env_vars
handle_venv
download_models
run_main_script