mirror of
				https://github.com/immich-app/immich.git
				synced 2025-11-03 19:17:11 -05:00 
			
		
		
		
	* basic refactor and styling * removed batching * module entrypoint * removed unused imports * model superclass, model cache now in app state * fixed cache dir and enforced abstract method --------- Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
		
			
				
	
	
		
			41 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			41 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
from pathlib import Path
 | 
						|
 | 
						|
from PIL.Image import Image
 | 
						|
from transformers.pipelines import pipeline
 | 
						|
 | 
						|
from ..config import settings
 | 
						|
from ..schemas import ModelType
 | 
						|
from .base import InferenceModel
 | 
						|
 | 
						|
 | 
						|
class ImageClassifier(InferenceModel):
 | 
						|
    _model_type = ModelType.IMAGE_CLASSIFICATION
 | 
						|
 | 
						|
    def __init__(
 | 
						|
        self,
 | 
						|
        model_name: str,
 | 
						|
        min_score: float = settings.min_tag_score,
 | 
						|
        cache_dir: Path | None = None,
 | 
						|
        **model_kwargs,
 | 
						|
    ):
 | 
						|
        super().__init__(model_name, cache_dir)
 | 
						|
        self.min_score = min_score
 | 
						|
 | 
						|
        self.model = pipeline(
 | 
						|
            self.model_type.value,
 | 
						|
            self.model_name,
 | 
						|
            model_kwargs={"cache_dir": self.cache_dir, **model_kwargs},
 | 
						|
        )
 | 
						|
 | 
						|
    def predict(self, image: Image) -> list[str]:
 | 
						|
        predictions = self.model(image)
 | 
						|
        tags = list(
 | 
						|
            {
 | 
						|
                tag
 | 
						|
                for pred in predictions
 | 
						|
                for tag in pred["label"].split(", ")
 | 
						|
                if pred["score"] >= self.min_score
 | 
						|
            }
 | 
						|
        )
 | 
						|
        return tags
 |