Spaces:
Running
Running
Commit
·
3df481e
1
Parent(s):
c8547b4
fixed
Browse files- Dockerfile +3 -0
- app/app.py +15 -13
- app/caption_model.py +39 -120
- app/model.py +23 -100
- requirements.txt +20 -8
Dockerfile
CHANGED
|
@@ -9,6 +9,9 @@ RUN useradd -m -u 1000 appuser
|
|
| 9 |
# Install system dependencies
|
| 10 |
RUN apt-get update && apt-get install -y \
|
| 11 |
curl \
|
|
|
|
|
|
|
|
|
|
| 12 |
&& rm -rf /var/lib/apt/lists/*
|
| 13 |
|
| 14 |
# Set up cache directory for Hugging Face
|
|
|
|
| 9 |
# Install system dependencies
|
| 10 |
RUN apt-get update && apt-get install -y \
|
| 11 |
curl \
|
| 12 |
+
gcc \
|
| 13 |
+
python3-dev \
|
| 14 |
+
libpython3-dev \
|
| 15 |
&& rm -rf /var/lib/apt/lists/*
|
| 16 |
|
| 17 |
# Set up cache directory for Hugging Face
|
app/app.py
CHANGED
|
@@ -1,13 +1,20 @@
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File, HTTPException
|
|
|
|
| 2 |
from starlette.responses import JSONResponse
|
| 3 |
-
from starlette.requests import Request
|
| 4 |
from app.model import analyze_image
|
| 5 |
from app.utils import read_image
|
| 6 |
-
from app.caption_model import
|
| 7 |
|
| 8 |
app = FastAPI(title="Image Analyzer API", version="1.0.0")
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
@app.post("/analyze")
|
| 13 |
async def analyze(file: UploadFile = File(...)):
|
|
@@ -32,27 +39,22 @@ async def analyze(file: UploadFile = File(...)):
|
|
| 32 |
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
| 33 |
|
| 34 |
@app.post("/caption")
|
| 35 |
-
async def
|
| 36 |
if not file or not file.filename:
|
| 37 |
raise HTTPException(status_code=400, detail="No file uploaded.")
|
| 38 |
-
try:
|
| 39 |
-
image = read_image(file)
|
| 40 |
-
except Exception as e:
|
| 41 |
-
raise HTTPException(status_code=400, detail=f"Failed to read image: {str(e)}")
|
| 42 |
|
| 43 |
if not file.content_type.startswith('image/'):
|
| 44 |
raise HTTPException(status_code=400, detail="File must be an image")
|
| 45 |
-
|
| 46 |
try:
|
| 47 |
-
|
|
|
|
| 48 |
return JSONResponse(content=result)
|
| 49 |
except ValueError as e:
|
| 50 |
raise HTTPException(status_code=400, detail=str(e))
|
| 51 |
-
except RuntimeError as e:
|
| 52 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 53 |
except Exception as e:
|
| 54 |
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
| 55 |
|
| 56 |
@app.get("/")
|
| 57 |
def read_root():
|
| 58 |
-
return {"message": "Image Analyzer API is running"}
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from starlette.responses import JSONResponse
|
|
|
|
| 4 |
from app.model import analyze_image
|
| 5 |
from app.utils import read_image
|
| 6 |
+
from app.caption_model import caption_image # Fixed import name
|
| 7 |
|
| 8 |
app = FastAPI(title="Image Analyzer API", version="1.0.0")
|
| 9 |
|
| 10 |
+
# ✅ Add CORS Middleware
|
| 11 |
+
app.add_middleware(
|
| 12 |
+
CORSMiddleware,
|
| 13 |
+
allow_origins=["*"], # Change to ["https://your-frontend.com"] for production
|
| 14 |
+
allow_credentials=True,
|
| 15 |
+
allow_methods=["*"],
|
| 16 |
+
allow_headers=["*"],
|
| 17 |
+
)
|
| 18 |
|
| 19 |
@app.post("/analyze")
|
| 20 |
async def analyze(file: UploadFile = File(...)):
|
|
|
|
| 39 |
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
| 40 |
|
| 41 |
@app.post("/caption")
|
| 42 |
+
async def generate_image_caption(file: UploadFile = File(...)):
|
| 43 |
if not file or not file.filename:
|
| 44 |
raise HTTPException(status_code=400, detail="No file uploaded.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
if not file.content_type.startswith('image/'):
|
| 47 |
raise HTTPException(status_code=400, detail="File must be an image")
|
| 48 |
+
|
| 49 |
try:
|
| 50 |
+
image = read_image(file)
|
| 51 |
+
result = caption_image(image) # Fixed function name
|
| 52 |
return JSONResponse(content=result)
|
| 53 |
except ValueError as e:
|
| 54 |
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
| 57 |
|
| 58 |
@app.get("/")
|
| 59 |
def read_root():
|
| 60 |
+
return {"message": "Image Analyzer API is running"}
|
app/caption_model.py
CHANGED
|
@@ -1,124 +1,43 @@
|
|
| 1 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 2 |
-
import torch
|
| 3 |
from PIL import Image
|
| 4 |
-
import
|
| 5 |
-
import time
|
| 6 |
-
from typing import Dict, Any, Optional
|
| 7 |
-
import gc
|
| 8 |
|
| 9 |
MODEL_NAME = "Salesforce/blip-image-captioning-base"
|
| 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 |
-
raise RuntimeError(f"Failed to initialize the image captioning model after {MAX_RETRIES} attempts")
|
| 49 |
-
|
| 50 |
-
def validate_image(self, image: Image.Image) -> Optional[str]:
|
| 51 |
-
"""Validate image before processing"""
|
| 52 |
-
if not isinstance(image, Image.Image):
|
| 53 |
-
return "Input must be a PIL Image"
|
| 54 |
-
|
| 55 |
-
# Check image mode
|
| 56 |
-
if image.mode not in ('RGB', 'L'):
|
| 57 |
-
return "Image must be in RGB or grayscale format"
|
| 58 |
-
|
| 59 |
-
return None
|
| 60 |
-
|
| 61 |
-
def generate_caption(self, image: Image.Image) -> Dict[str, Any]:
|
| 62 |
-
# Validate input
|
| 63 |
-
error = self.validate_image(image)
|
| 64 |
-
if error:
|
| 65 |
-
raise ValueError(error)
|
| 66 |
-
|
| 67 |
-
# Check model initialization
|
| 68 |
-
if self.model is None or self.processor is None:
|
| 69 |
-
self._initialize_model() # Try to reinitialize if models are not loaded
|
| 70 |
-
|
| 71 |
-
try:
|
| 72 |
-
# Clear CUDA cache if using GPU
|
| 73 |
-
if torch.cuda.is_available():
|
| 74 |
-
torch.cuda.empty_cache()
|
| 75 |
-
gc.collect()
|
| 76 |
-
|
| 77 |
-
# Prepare inputs
|
| 78 |
-
inputs = self.processor(image, return_tensors="pt")
|
| 79 |
-
inputs = {k: v.to(self.device) if hasattr(v, 'to') else v for k, v in inputs.items()}
|
| 80 |
-
|
| 81 |
-
# Process with error handling and memory management
|
| 82 |
-
try:
|
| 83 |
-
with torch.no_grad():
|
| 84 |
-
# Generate caption with parameters for better quality
|
| 85 |
-
out = self.model.generate(
|
| 86 |
-
**inputs,
|
| 87 |
-
max_length=MAX_LENGTH,
|
| 88 |
-
num_beams=5, # Beam search for better quality
|
| 89 |
-
temperature=1.0,
|
| 90 |
-
top_k=50,
|
| 91 |
-
top_p=0.95,
|
| 92 |
-
repetition_penalty=1.2,
|
| 93 |
-
length_penalty=1.0,
|
| 94 |
-
no_repeat_ngram_size=2
|
| 95 |
-
)
|
| 96 |
-
caption = self.processor.decode(out[0], skip_special_tokens=True)
|
| 97 |
-
|
| 98 |
-
# Process the caption
|
| 99 |
-
caption = caption.strip()
|
| 100 |
-
# Ensure caption starts with capital letter and ends with period
|
| 101 |
-
caption = caption[0].upper() + caption[1:]
|
| 102 |
-
if not caption.endswith(('.', '!', '?')):
|
| 103 |
-
caption += '.'
|
| 104 |
-
|
| 105 |
-
return {
|
| 106 |
-
"caption": caption,
|
| 107 |
-
"status": "success",
|
| 108 |
-
"model_info": {
|
| 109 |
-
"device": self.device,
|
| 110 |
-
"model_name": MODEL_NAME
|
| 111 |
-
}
|
| 112 |
-
}
|
| 113 |
-
finally:
|
| 114 |
-
# Clean up tensors
|
| 115 |
-
if torch.cuda.is_available():
|
| 116 |
-
torch.cuda.empty_cache()
|
| 117 |
-
gc.collect()
|
| 118 |
-
|
| 119 |
-
except Exception as e:
|
| 120 |
-
logging.error(f"Error during caption generation: {str(e)}")
|
| 121 |
-
raise RuntimeError(f"Failed to generate caption: {str(e)}")
|
| 122 |
-
|
| 123 |
-
# Initialize model
|
| 124 |
-
captioner = ImageCaptioner()
|
|
|
|
| 1 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
+
import torch
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
MODEL_NAME = "Salesforce/blip-image-captioning-base"
|
| 6 |
+
MAX_LENGTH = 50
|
| 7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
|
| 9 |
+
# Load model and processor only once at startup
|
| 10 |
+
processor = BlipProcessor.from_pretrained(MODEL_NAME)
|
| 11 |
+
model = BlipForConditionalGeneration.from_pretrained(MODEL_NAME).to(device)
|
| 12 |
+
model.eval()
|
| 13 |
+
|
| 14 |
+
def caption_image(image: Image.Image):
|
| 15 |
+
# Validate input
|
| 16 |
+
if not isinstance(image, Image.Image) or image.mode not in ('RGB', 'L'):
|
| 17 |
+
raise ValueError("Input must be a valid PIL Image in RGB or grayscale format")
|
| 18 |
+
|
| 19 |
+
# Preprocess input
|
| 20 |
+
inputs = processor(image, return_tensors="pt")
|
| 21 |
+
inputs = {k: v.to(device) if hasattr(v, 'to') else v for k, v in inputs.items()}
|
| 22 |
+
|
| 23 |
+
# Generate caption
|
| 24 |
+
with torch.no_grad():
|
| 25 |
+
output_ids = model.generate(
|
| 26 |
+
**inputs,
|
| 27 |
+
max_length=MAX_LENGTH,
|
| 28 |
+
num_beams=5,
|
| 29 |
+
temperature=1.0,
|
| 30 |
+
top_k=50,
|
| 31 |
+
top_p=0.95
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Decode caption
|
| 35 |
+
caption = processor.decode(output_ids[0], skip_special_tokens=True).strip()
|
| 36 |
+
caption = caption[0].upper() + caption[1:]
|
| 37 |
+
if not caption.endswith(('.', '!', '?')):
|
| 38 |
+
caption += '.'
|
| 39 |
+
|
| 40 |
+
return {
|
| 41 |
+
"caption": caption,
|
| 42 |
+
"confidence": 1.0 # BLIP doesn't return a real score
|
| 43 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/model.py
CHANGED
|
@@ -1,108 +1,31 @@
|
|
| 1 |
from transformers import CLIPProcessor, CLIPModel
|
| 2 |
-
import torch
|
| 3 |
from PIL import Image
|
| 4 |
-
import
|
| 5 |
-
import time
|
| 6 |
-
from typing import Dict, Any
|
| 7 |
-
import gc
|
| 8 |
|
| 9 |
MODEL_NAME = "openai/clip-vit-base-patch16"
|
| 10 |
-
CATEGORIES = ["food", "fitness", "healthcare"]
|
| 11 |
-
MAX_RETRIES = 3
|
| 12 |
-
RETRY_DELAY = 1 # seconds
|
| 13 |
-
|
| 14 |
-
class ImageAnalyzer:
|
| 15 |
-
def __init__(self):
|
| 16 |
-
self.processor = None
|
| 17 |
-
self.model = None
|
| 18 |
-
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 19 |
-
logging.info(f"Using device: {self.device}")
|
| 20 |
-
self._initialize_model()
|
| 21 |
-
|
| 22 |
-
def _initialize_model(self):
|
| 23 |
-
for attempt in range(MAX_RETRIES):
|
| 24 |
-
try:
|
| 25 |
-
# Clear CUDA cache if using GPU
|
| 26 |
-
if torch.cuda.is_available():
|
| 27 |
-
torch.cuda.empty_cache()
|
| 28 |
-
gc.collect()
|
| 29 |
-
|
| 30 |
-
self.processor = CLIPProcessor.from_pretrained(MODEL_NAME)
|
| 31 |
-
self.model = CLIPModel.from_pretrained(MODEL_NAME).to(self.device)
|
| 32 |
-
|
| 33 |
-
# Verify model loaded correctly
|
| 34 |
-
if self.model is None or self.processor is None:
|
| 35 |
-
raise RuntimeError("Model or processor initialization failed")
|
| 36 |
-
|
| 37 |
-
logging.info(f"Model loaded successfully on {self.device} (attempt {attempt + 1})")
|
| 38 |
-
return
|
| 39 |
-
|
| 40 |
-
except Exception as e:
|
| 41 |
-
logging.error(f"Attempt {attempt + 1} failed to load model: {str(e)}")
|
| 42 |
-
if attempt < MAX_RETRIES - 1:
|
| 43 |
-
time.sleep(RETRY_DELAY)
|
| 44 |
-
continue
|
| 45 |
-
raise RuntimeError(f"Failed to initialize the image analysis model after {MAX_RETRIES} attempts")
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
try:
|
| 55 |
-
# Clear CUDA cache if using GPU
|
| 56 |
-
if torch.cuda.is_available():
|
| 57 |
-
torch.cuda.empty_cache()
|
| 58 |
-
gc.collect()
|
| 59 |
-
|
| 60 |
-
# Prepare inputs for CLIP
|
| 61 |
-
inputs = self.processor(
|
| 62 |
-
text=CATEGORIES,
|
| 63 |
-
images=image,
|
| 64 |
-
return_tensors="pt",
|
| 65 |
-
padding=True
|
| 66 |
-
)
|
| 67 |
-
|
| 68 |
-
# Move inputs to the same device as model
|
| 69 |
-
inputs = {k: v.to(self.device) if hasattr(v, 'to') else v
|
| 70 |
-
for k, v in inputs.items()}
|
| 71 |
-
|
| 72 |
-
# Process with error handling and memory management
|
| 73 |
-
try:
|
| 74 |
-
with torch.no_grad():
|
| 75 |
-
outputs = self.model(**inputs)
|
| 76 |
-
logits_per_image = outputs.logits_per_image
|
| 77 |
-
probs = logits_per_image.softmax(dim=1).cpu().numpy()[0]
|
| 78 |
-
|
| 79 |
-
# Get top 2 predictions for more informative results
|
| 80 |
-
top_indices = probs.argsort()[-2:][::-1]
|
| 81 |
-
predictions = [
|
| 82 |
-
{
|
| 83 |
-
"category": CATEGORIES[idx],
|
| 84 |
-
"confidence": round(float(probs[idx]), 4)
|
| 85 |
-
}
|
| 86 |
-
for idx in top_indices
|
| 87 |
-
]
|
| 88 |
-
|
| 89 |
-
return {
|
| 90 |
-
"primary_prediction": predictions[0],
|
| 91 |
-
"alternative_prediction": predictions[1],
|
| 92 |
-
"status": "success"
|
| 93 |
-
}
|
| 94 |
-
finally:
|
| 95 |
-
# Clean up tensors
|
| 96 |
-
if torch.cuda.is_available():
|
| 97 |
-
torch.cuda.empty_cache()
|
| 98 |
-
gc.collect()
|
| 99 |
-
except Exception as e:
|
| 100 |
-
logging.error(f"Error during image analysis: {str(e)}")
|
| 101 |
-
raise RuntimeError(f"Failed to analyze image: {str(e)}")
|
| 102 |
-
|
| 103 |
-
# Create a single instance to be used by the API
|
| 104 |
-
analyzer = ImageAnalyzer()
|
| 105 |
|
| 106 |
-
# Function to be used by the API
|
| 107 |
def analyze_image(image: Image.Image):
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from transformers import CLIPProcessor, CLIPModel
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
+
import torch
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
MODEL_NAME = "openai/clip-vit-base-patch16"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Load model and processor only once at startup
|
| 8 |
+
processor = CLIPProcessor.from_pretrained(MODEL_NAME)
|
| 9 |
+
model = CLIPModel.from_pretrained(MODEL_NAME)
|
| 10 |
|
| 11 |
+
# Define the categories to classify into
|
| 12 |
+
CATEGORIES = ["food", "fitness", "healthcare"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
| 14 |
def analyze_image(image: Image.Image):
|
| 15 |
+
# Preprocess input
|
| 16 |
+
inputs = processor(
|
| 17 |
+
text=CATEGORIES,
|
| 18 |
+
images=image,
|
| 19 |
+
return_tensors="pt",
|
| 20 |
+
padding=True
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
outputs = model(**inputs)
|
| 25 |
+
logits_per_image = outputs.logits_per_image
|
| 26 |
+
probs = logits_per_image.softmax(dim=1).cpu().numpy()[0]
|
| 27 |
+
best_idx = probs.argmax()
|
| 28 |
+
return {
|
| 29 |
+
"category": CATEGORIES[best_idx],
|
| 30 |
+
"confidence": round(float(probs[best_idx]), 4)
|
| 31 |
+
}
|
requirements.txt
CHANGED
|
@@ -1,8 +1,20 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Web framework and server
|
| 2 |
+
fastapi
|
| 3 |
+
uvicorn
|
| 4 |
+
|
| 5 |
+
# Core ML dependencies
|
| 6 |
+
torch
|
| 7 |
+
torchvision
|
| 8 |
+
transformers
|
| 9 |
+
|
| 10 |
+
# Image processing
|
| 11 |
+
Pillow
|
| 12 |
+
|
| 13 |
+
# Utilities
|
| 14 |
+
python-multipart
|
| 15 |
+
numpy
|
| 16 |
+
tqdm
|
| 17 |
+
requests
|
| 18 |
+
safetensors
|
| 19 |
+
typing-extensions
|
| 20 |
+
pydantic
|