digital-art-api / lib /hugging_face.py
Ruzzel Mendoza
rework descriptive prompts for better art classification
0f5fa48
Raw History Blame Contribute Delete
2.48 kB
import io
from PIL import Image
from fastapi import UploadFile, File
from transformers import pipeline
# Load Google's SigLIP model for zero-shot classification
print("Loading SigLIP Zero-Shot Model...")
classifier = pipeline(
"zero-shot-image-classification",
model="google/siglip-base-patch16-224"
)
print("Model loaded successfully!")
# ORIGINAL DATABASE LABELS (DO NOT CHANGE ORDER)
DIGITAL_ART_GENRES = [
"Anime", "Manga", "Cartoon", "Semi-Realism", "Realism",
"Hyperrealism", "Abstract", "Minimalist", "Impressionism",
"Surrealism", "Pop Art", "Pixel Art", "Low Poly",
"Vector Art", "Line Art", "Sketch Art", "Flat Design",
"3D Render", "Isometric"
]
# MODEL-FRIENDLY VISUAL DESCRIPTIONS
CLASSIFICATION_PROMPTS = [
"anime style digital illustration",
"manga comic illustration in black and white",
"western cartoon style illustration",
"semi realistic digital painting",
"realistic digital painting",
"hyper realistic digital painting",
"abstract digital artwork with shapes and colors",
"minimalist digital artwork with very simple shapes",
"impressionist style digital painting with visible brush strokes",
"surreal dreamlike digital artwork",
"pop art style digital illustration with bold colors",
"pixel art made of small square pixels",
"low poly 3D artwork with polygon shapes",
"vector illustration with clean scalable shapes",
"line art drawing with outlines only",
"pencil sketch style drawing",
"flat design illustration with solid colors",
"3D rendered digital artwork",
"isometric 3D illustration with angled perspective"
]
async def classify_art(file: UploadFile = File(...)):
try:
image_bytes = await file.read()
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
results = classifier(image, candidate_labels=CLASSIFICATION_PROMPTS)
mapped_results = []
for result in results:
prompt = result["label"]
# Get index from prompt list
idx = CLASSIFICATION_PROMPTS.index(prompt)
mapped_results.append({
"label": DIGITAL_ART_GENRES[idx], # original database label
"score": result["score"],
"index": idx + 1 # preserve database index
})
return {"success": True, "results": mapped_results}
except Exception as e:
return {"success": False, "error": str(e)}