-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.py
More file actions
135 lines (114 loc) · 5.24 KB
/
Copy pathdeploy.py
File metadata and controls
135 lines (114 loc) · 5.24 KB
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
#!/usr/bin/env python3
"""
Deployment script for the sentiment ML service on Modal.
Run this script to test and deploy the service.
"""
import subprocess
import sys
import os
from datetime import datetime
def run_command(cmd, description):
"""Run a shell command and handle output."""
print(f"\n{'='*60}")
print(f"🔧 {description}")
print(f"{'='*60}")
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
if result.stdout:
print(result.stdout)
if result.returncode != 0:
print(f"❌ Error: {description} failed!")
if result.stderr:
print(f"Error details: {result.stderr}")
return False
print(f"✅ {description} completed successfully!")
return True
def main():
"""Main deployment process."""
print(f"""
╔════════════════════════════════════════════════════════════╗
║ Sentiment ML Service - Modal Deployment Script ║
║ ║
║ Starting deployment at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} ║
╚════════════════════════════════════════════════════════════╝
""")
# Step 1: Check Modal authentication
# Try to list apps as a way to verify authentication
result = subprocess.run("modal app list", shell=True, capture_output=True, text=True)
if result.returncode != 0:
print("\n❌ Error: Modal authentication check failed!")
print("\n⚠️ Please authenticate with Modal first:")
print(" Run: modal setup")
if result.stderr:
print(f"\nError details: {result.stderr}")
sys.exit(1)
else:
print(f"\n{'='*60}")
print(f"🔧 Checking Modal authentication")
print(f"{'='*60}")
print("✅ Modal authentication verified!")
# Step 2: Run local tests
print("\n" + "="*60)
print("🧪 Running local tests...")
print("="*60)
# First, check if we're in a virtual environment
if not os.environ.get('VIRTUAL_ENV'):
print("⚠️ Warning: Not in a virtual environment.")
print(" Consider activating your virtual environment first:")
print(" source venv/bin/activate")
# Run pytest locally
if not run_command("python -m pytest tests/ -v --tb=short", "Running local tests"):
print("\n❌ Local tests failed! Fix tests before deploying.")
sys.exit(1)
# Step 3: Deploy to Modal with integrated testing
print("\n" + "="*60)
print("🚀 Deploying to Modal (includes remote testing)...")
print("="*60)
# Run the modal_app.py script which includes testing and deployment
if not run_command("python modal_app.py", "Deploying to Modal"):
print("\n❌ Modal deployment failed!")
sys.exit(1)
# Step 4: Verify deployment
print("\n" + "="*60)
print("🔍 Verifying deployment...")
print("="*60)
# Get the deployment URL
result = subprocess.run(
"modal app list | grep sentiment-ml-service",
shell=True,
capture_output=True,
text=True
)
if result.returncode == 0:
print("✅ App is deployed and running!")
print("\n📊 Deployment Summary:")
print(" - Service: sentiment-ml-service")
print(" - GPU: NVIDIA T4")
print(" - Auto-scaling: 1-10 containers")
print(" - Endpoint: https://theresaanna--sentiment-ml-service-fastapi-app.modal.run")
print("\n📝 Available endpoints:")
print(" - GET /health - Health check")
print(" - POST /analyze-text - Single text analysis")
print(" - POST /analyze-batch - Batch text analysis")
print("\n🧪 Test your deployment:")
print(' curl https://theresaanna--sentiment-ml-service-fastapi-app.modal.run/health')
print(' curl -X POST https://theresaanna--sentiment-ml-service-fastapi-app.modal.run/analyze-text \\')
print(' -H "Content-Type: application/json" \\')
print(' -d \'{"text": "I love this service!"}\'')
else:
print("⚠️ Could not verify deployment status")
print(f"""
╔════════════════════════════════════════════════════════════╗
║ Deployment Complete! 🎉 ║
║ ║
║ Finished at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} ║
╚════════════════════════════════════════════════════════════╝
""")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\n⚠️ Deployment interrupted by user")
sys.exit(1)
except Exception as e:
print(f"\n❌ Unexpected error: {e}")
sys.exit(1)