Spaces:
Runtime error
Runtime error
remove Docker files
Browse files- Dockerfile +0 -15
- app/main.py +0 -90
- docker-compose.yml +0 -12
Dockerfile
DELETED
|
@@ -1,15 +0,0 @@
|
|
| 1 |
-
# Dockerfile
|
| 2 |
-
|
| 3 |
-
FROM python:3.11-slim
|
| 4 |
-
|
| 5 |
-
# Install dependencies
|
| 6 |
-
RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*
|
| 7 |
-
|
| 8 |
-
WORKDIR /app
|
| 9 |
-
|
| 10 |
-
COPY requirements.txt .
|
| 11 |
-
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
-
|
| 13 |
-
COPY . .
|
| 14 |
-
|
| 15 |
-
CMD ["python", "app.py"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/main.py
DELETED
|
@@ -1,90 +0,0 @@
|
|
| 1 |
-
# app/main.py
|
| 2 |
-
|
| 3 |
-
import os
|
| 4 |
-
from fastapi import FastAPI, UploadFile, HTTPException
|
| 5 |
-
from fastapi.responses import JSONResponse
|
| 6 |
-
from PIL import Image
|
| 7 |
-
import torch
|
| 8 |
-
from torchvision import transforms as T
|
| 9 |
-
from src.train import loss_fn # Load loss function from src/train.py
|
| 10 |
-
from src.model_config import load_model, set_device
|
| 11 |
-
import io
|
| 12 |
-
from io import BytesIO
|
| 13 |
-
|
| 14 |
-
import base64
|
| 15 |
-
import matplotlib.pyplot as plt
|
| 16 |
-
|
| 17 |
-
import logging
|
| 18 |
-
log = logging.getLogger(__name__)
|
| 19 |
-
logging.basicConfig(level=logging.INFO)
|
| 20 |
-
|
| 21 |
-
app = FastAPI()
|
| 22 |
-
|
| 23 |
-
device = set_device()
|
| 24 |
-
log.info("Using device: %s", device)
|
| 25 |
-
model = load_model(device=device)
|
| 26 |
-
log.info("Model loaded. App is ready.")
|
| 27 |
-
|
| 28 |
-
model.eval()
|
| 29 |
-
|
| 30 |
-
# Transformations
|
| 31 |
-
transform = T.Compose([
|
| 32 |
-
T.Grayscale(num_output_channels=1),
|
| 33 |
-
T.Resize((28, 28)),
|
| 34 |
-
T.ToTensor()
|
| 35 |
-
])
|
| 36 |
-
|
| 37 |
-
@app.get("/health")
|
| 38 |
-
def health():
|
| 39 |
-
return {"status": "ok"}
|
| 40 |
-
|
| 41 |
-
@app.post("/predict", summary="Predict anomaly score from image")
|
| 42 |
-
async def predict(file: UploadFile):
|
| 43 |
-
try:
|
| 44 |
-
contents = await file.read()
|
| 45 |
-
image = Image.open(io.BytesIO(contents)).convert("L")
|
| 46 |
-
image = transform(image)
|
| 47 |
-
image = image.unsqueeze(0).to(device)
|
| 48 |
-
|
| 49 |
-
with torch.no_grad():
|
| 50 |
-
decoded = model(image)
|
| 51 |
-
score = loss_fn(decoded, image).item()
|
| 52 |
-
diff = (decoded - image).squeeze().cpu().numpy() ** 2
|
| 53 |
-
|
| 54 |
-
# Create heatmap image
|
| 55 |
-
fig, ax = plt.subplots()
|
| 56 |
-
ax.imshow(diff, cmap="hot")
|
| 57 |
-
ax.axis("off")
|
| 58 |
-
buf = BytesIO()
|
| 59 |
-
plt.savefig(buf, format="png", bbox_inches="tight", pad_inches=0)
|
| 60 |
-
plt.close(fig)
|
| 61 |
-
buf.seek(0)
|
| 62 |
-
|
| 63 |
-
# Encode to base64
|
| 64 |
-
heatmap_b64 = base64.b64encode(buf.read()).decode("utf-8")
|
| 65 |
-
|
| 66 |
-
# Create reconstructed image
|
| 67 |
-
decoded_img = decoded.squeeze().cpu().numpy()
|
| 68 |
-
|
| 69 |
-
# Visualize the decoded image as png
|
| 70 |
-
fig2, ax2 = plt.subplots()
|
| 71 |
-
ax2.imshow(decoded_img, cmap="gray")
|
| 72 |
-
ax2.axis("off")
|
| 73 |
-
buf2 = BytesIO()
|
| 74 |
-
plt.savefig(buf2, format="png", bbox_inches="tight", pad_inches=0)
|
| 75 |
-
plt.close(fig2)
|
| 76 |
-
buf2.seek(0)
|
| 77 |
-
|
| 78 |
-
# Decode to base64
|
| 79 |
-
decoded_b64 = base64.b64encode(buf2.read()).decode("utf-8")
|
| 80 |
-
|
| 81 |
-
# Return everything as JSON
|
| 82 |
-
return JSONResponse(content={
|
| 83 |
-
"anomaly_score": score,
|
| 84 |
-
"heatmap": heatmap_b64,
|
| 85 |
-
"decoded_image": decoded_b64
|
| 86 |
-
})
|
| 87 |
-
|
| 88 |
-
except Exception as e:
|
| 89 |
-
raise HTTPException(status_code=500, detail=f"Unexpected error: {str(e)}")
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
docker-compose.yml
DELETED
|
@@ -1,12 +0,0 @@
|
|
| 1 |
-
version: "3.8"
|
| 2 |
-
|
| 3 |
-
services:
|
| 4 |
-
frontend:
|
| 5 |
-
build: .
|
| 6 |
-
ports:
|
| 7 |
-
- "7860:7860"
|
| 8 |
-
volumes:
|
| 9 |
-
- .:/app
|
| 10 |
-
environment:
|
| 11 |
-
- PYTHONUNBUFFERED=1
|
| 12 |
-
restart: unless-stopped
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|