File size: 7,073 Bytes
3df89a1 1239566 |
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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
"""
Deployment module for KerdosAI.
"""
from typing import Dict, Any, Optional
import torch
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import uvicorn
import docker
import yaml
import logging
from pathlib import Path
logger = logging.getLogger(__name__)
class TextRequest(BaseModel):
"""Request model for text generation."""
text: str
max_length: Optional[int] = 100
temperature: Optional[float] = 0.7
top_p: Optional[float] = 0.9
class Deployer:
"""
Handles model deployment in various environments.
"""
def __init__(
self,
model: Any,
tokenizer: Any,
device: Optional[str] = None
):
"""
Initialize the deployer.
Args:
model: The trained model
tokenizer: The model's tokenizer
device: Device to run inference on
"""
self.model = model
self.tokenizer = tokenizer
self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
self.model.to(self.device)
self.model.eval()
def deploy(
self,
deployment_type: str = "rest",
host: str = "0.0.0.0",
port: int = 8000,
**kwargs
) -> None:
"""
Deploy the model.
Args:
deployment_type: Type of deployment (rest/docker/kubernetes)
host: Host address for REST API
port: Port number for REST API
**kwargs: Additional deployment parameters
"""
if deployment_type == "rest":
self._deploy_rest(host, port)
elif deployment_type == "docker":
self._deploy_docker(**kwargs)
elif deployment_type == "kubernetes":
self._deploy_kubernetes(**kwargs)
else:
raise ValueError(f"Unsupported deployment type: {deployment_type}")
def _deploy_rest(self, host: str, port: int) -> None:
"""
Deploy the model as a REST API.
Args:
host: Host address
port: Port number
"""
app = FastAPI(title="KerdosAI API")
@app.post("/generate")
async def generate_text(request: TextRequest):
try:
# Tokenize input
inputs = self.tokenizer(
request.text,
return_tensors="pt",
padding=True,
truncation=True
).to(self.device)
# Generate text
with torch.no_grad():
outputs = self.model.generate(
**inputs,
max_length=request.max_length,
temperature=request.temperature,
top_p=request.top_p,
pad_token_id=self.tokenizer.eos_token_id
)
# Decode output
generated_text = self.tokenizer.decode(
outputs[0],
skip_special_tokens=True
)
return {"generated_text": generated_text}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Start the server
uvicorn.run(app, host=host, port=port)
def _deploy_docker(self, **kwargs) -> None:
"""
Deploy the model using Docker.
Args:
**kwargs: Additional Docker deployment parameters
"""
# Create Dockerfile
dockerfile_content = """
FROM python:3.8-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "-m", "kerdosai.deployer", "--deploy", "rest"]
"""
# Save Dockerfile
with open("Dockerfile", "w") as f:
f.write(dockerfile_content)
# Build and run Docker container
client = docker.from_env()
try:
# Build image
image, _ = client.images.build(
path=".",
tag="kerdosai:latest",
dockerfile="Dockerfile"
)
# Run container
container = client.containers.run(
image.id,
ports={'8000/tcp': 8000},
detach=True
)
logger.info(f"Docker container started: {container.id}")
except Exception as e:
logger.error(f"Error deploying with Docker: {str(e)}")
raise
def _deploy_kubernetes(self, **kwargs) -> None:
"""
Deploy the model using Kubernetes.
Args:
**kwargs: Additional Kubernetes deployment parameters
"""
# Create Kubernetes deployment manifest
deployment_manifest = {
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "kerdosai"
},
"spec": {
"replicas": 1,
"selector": {
"matchLabels": {
"app": "kerdosai"
}
},
"template": {
"metadata": {
"labels": {
"app": "kerdosai"
}
},
"spec": {
"containers": [{
"name": "kerdosai",
"image": "kerdosai:latest",
"ports": [{
"containerPort": 8000
}]
}]
}
}
}
}
# Create Kubernetes service manifest
service_manifest = {
"apiVersion": "v1",
"kind": "Service",
"metadata": {
"name": "kerdosai"
},
"spec": {
"selector": {
"app": "kerdosai"
},
"ports": [{
"port": 80,
"targetPort": 8000
}],
"type": "LoadBalancer"
}
}
# Save manifests
with open("deployment.yaml", "w") as f:
yaml.dump(deployment_manifest, f)
with open("service.yaml", "w") as f:
yaml.dump(service_manifest, f)
logger.info("Kubernetes manifests created. Apply them using:")
logger.info("kubectl apply -f deployment.yaml")
logger.info("kubectl apply -f service.yaml") |