File size: 5,990 Bytes
b840b29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash
# setup_cloud.sh - Setup script for cloud deployments

# Set error handling
set -e

# Function to print colored output
print_colored() {
  GREEN='\033[0;32m'
  RED='\033[0;31m'
  YELLOW='\033[0;33m'
  BLUE='\033[0;34m'
  NC='\033[0m' # No Color
  
  case $1 in
    "info")
      echo -e "${BLUE}[INFO]${NC} $2"
      ;;
    "success")
      echo -e "${GREEN}[SUCCESS]${NC} $2"
      ;;
    "warning")
      echo -e "${YELLOW}[WARNING]${NC} $2"
      ;;
    "error")
      echo -e "${RED}[ERROR]${NC} $2"
      ;;
    *)
      echo -e "$2"
      ;;
  esac
}

# Check for Python
if ! command -v python3 &> /dev/null; then
  print_colored "error" "Python 3 is required but not found. Please install Python 3 and try again."
  exit 1
fi

# Parse arguments
PLATFORM=""
DEPLOY=false

print_colored "info" "Agentic Defensor Cloud Setup"
print_colored "info" "--------------------------------"

# Parse command line arguments
while [[ $# -gt 0 ]]; do
  case $1 in
    --platform)
      PLATFORM="$2"
      shift 2
      ;;
    --deploy)
      DEPLOY=true
      shift
      ;;
    --help)
      echo "Usage: $0 [options]"
      echo "Options:"
      echo "  --platform PLATFORM   Specify the platform (replicate or huggingface)"
      echo "  --deploy              Deploy immediately after setup"
      echo "  --help                Show this help message"
      exit 0
      ;;
    *)
      print_colored "error" "Unknown argument: $1"
      exit 1
      ;;
  esac
done

# If no platform specified, prompt user
if [ -z "$PLATFORM" ]; then
  echo "Select target platform:"
  echo "1) Replicate"
  echo "2) Hugging Face Spaces"
  read -p "Enter your choice [1/2]: " platform_choice
  
  case $platform_choice in
    1)
      PLATFORM="replicate"
      ;;
    2)
      PLATFORM="huggingface"
      ;;
    *)
      print_colored "error" "Invalid choice. Exiting."
      exit 1
      ;;
  esac
fi

# Validate platform
if [ "$PLATFORM" != "replicate" ] && [ "$PLATFORM" != "huggingface" ]; then
  print_colored "error" "Invalid platform: $PLATFORM. Choose either 'replicate' or 'huggingface'."
  exit 1
fi

print_colored "info" "Setting up for $PLATFORM deployment..."

# Check if we need to install dependencies
read -p "Install required dependencies? [y/N]: " install_deps
if [[ $install_deps =~ ^[Yy]$ ]]; then
  print_colored "info" "Installing dependencies..."
  
  if [ "$PLATFORM" == "replicate" ]; then
    pip install -r requirements.txt
    pip install cog
  else
    pip install -r requirements-huggingface.txt
  fi
  
  print_colored "success" "Dependencies installed."
fi

# Setup platform-specific files
if [ "$PLATFORM" == "replicate" ]; then
  # Replicate setup
  print_colored "info" "Setting up for Replicate..."
  
  # Check for cog.yaml
  if [ ! -f "cog.yaml" ]; then
    print_colored "error" "cog.yaml not found. Please create it first."
    exit 1
  fi
  
  # Check for data download script
  if [ ! -f "download_data.py" ]; then
    print_colored "error" "download_data.py not found. Please create it first."
    exit 1
  fi
  
  # Modify replicate.py if needed
  if [ -f "replicate.py" ] && [ -f "modify_replicate.py" ]; then
    read -p "Enable runtime data downloading? [y/N]: " enable_download
    if [[ $enable_download =~ ^[Yy]$ ]]; then
      print_colored "info" "Modifying replicate.py for runtime data downloading..."
      python modify_replicate.py
    fi
  fi
  
  # Set up data hosts
  read -p "Set up data hosts in download_data.py? [y/N]: " setup_hosts
  if [[ $setup_hosts =~ ^[Yy]$ ]]; then
    print_colored "info" "Please enter your data storage URLs:"
    read -p "Base URL for data storage: " storage_base_url
    
    # Update the download_data.py file with the storage URL
    if [ ! -z "$storage_base_url" ]; then
      sed -i.bak "s|https://your-storage-url.com|$storage_base_url|g" download_data.py
      print_colored "success" "Updated storage URLs in download_data.py"
    fi
  fi
  
  # Deploy to Replicate
  if [ "$DEPLOY" = true ]; then
    print_colored "info" "Deploying to Replicate..."
    cog push
  else
    print_colored "info" "To deploy to Replicate, run: cog push"
  fi

else
  # Hugging Face setup
  print_colored "info" "Setting up for Hugging Face Spaces..."
  
  # Check for Hugging Face files
  if [ ! -f "app_huggingface.py" ]; then
    print_colored "error" "app_huggingface.py not found. Please create it first."
    exit 1
  fi
  
  if [ ! -f "requirements-huggingface.txt" ]; then
    print_colored "error" "requirements-huggingface.txt not found. Please create it first."
    exit 1
  fi
  
  # Check for download script
  if [ ! -f "download_data.py" ]; then
    print_colored "error" "download_data.py not found. Please create it first."
    exit 1
  fi
  
  # Modify app_huggingface.py to include data downloading
  read -p "Enable runtime data downloading? [y/N]: " enable_download
  if [[ $enable_download =~ ^[Yy]$ ]]; then
    print_colored "info" "Please enter your data storage URLs:"
    read -p "Base URL for data storage: " storage_base_url
    
    # Update the download_data.py file with the storage URL
    if [ ! -z "$storage_base_url" ]; then
      sed -i.bak "s|https://your-storage-url.com|$storage_base_url|g" download_data.py
      print_colored "success" "Updated storage URLs in download_data.py"
    fi
  fi
  
  # Deploy to Hugging Face
  if [ "$DEPLOY" = true ]; then
    print_colored "warning" "Automatic deployment to Hugging Face not supported."
    print_colored "info" "Please upload the files manually to your Hugging Face Space."
  fi
  
  # Instructions for manual deployment
  print_colored "info" "To deploy to Hugging Face Spaces:"
  print_colored "info" "1. Create a new Space on huggingface.co/spaces"
  print_colored "info" "2. Select 'Gradio' as the SDK"
  print_colored "info" "3. Upload your files to the Space repository"
fi

print_colored "success" "Setup complete!"
print_colored "info" "Make sure to set your OPENAI_API_KEY as a secret in your deployment platform."