|
import torch |
|
from transformers import ViTForImageClassification, ViTFeatureExtractor, AutoConfig |
|
import gradio as gr |
|
from PIL import Image |
|
import os |
|
import logging |
|
from safetensors.torch import load_file |
|
|
|
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') |
|
|
|
|
|
model_dir = "./" |
|
|
|
|
|
model_path = os.path.join(model_dir, "model.safetensors") |
|
config_path = os.path.join(model_dir, "config.json") |
|
preprocessor_path = os.path.join(model_dir, "preprocessor_config.json") |
|
|
|
|
|
for path in [model_path, config_path, preprocessor_path]: |
|
if not os.path.exists(path): |
|
logging.error(f"File not found: {path}") |
|
raise FileNotFoundError(f"Required file not found: {path}") |
|
else: |
|
logging.info(f"Found file: {path}") |
|
|
|
|
|
config = AutoConfig.from_pretrained(config_path) |
|
|
|
|
|
labels = list(config.id2label.values()) |
|
logging.info(f"Labels: {labels}") |
|
|
|
|
|
feature_extractor = ViTFeatureExtractor.from_pretrained(preprocessor_path) |
|
|
|
|
|
state_dict = load_file(model_path) |
|
model = ViTForImageClassification.from_pretrained( |
|
pretrained_model_name_or_path=None, |
|
config=config, |
|
state_dict=state_dict |
|
) |
|
|
|
|
|
model.eval() |
|
logging.info("Model set to evaluation mode") |
|
|
|
|
|
def predict(image): |
|
logging.info("Starting prediction") |
|
logging.info(f"Input image shape: {image.size}") |
|
|
|
|
|
logging.info("Preprocessing image") |
|
inputs = feature_extractor(images=image, return_tensors="pt") |
|
logging.info(f"Preprocessed input shape: {inputs['pixel_values'].shape}") |
|
|
|
logging.info("Running inference") |
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
logits = outputs.logits |
|
probabilities = torch.nn.functional.softmax(logits[0], dim=0) |
|
|
|
logging.info(f"Raw logits: {logits}") |
|
logging.info(f"Probabilities: {probabilities}") |
|
|
|
|
|
result = {labels[i]: float(probabilities[i]) for i in range(len(labels))} |
|
logging.info(f"Prediction result: {result}") |
|
|
|
return result |
|
|
|
|
|
logging.info("Setting up Gradio interface") |
|
gradio_app = gr.Interface( |
|
fn=predict, |
|
inputs=gr.Image(type="pil"), |
|
outputs=gr.Label(num_top_classes=6), |
|
title="Dress SleeveLength Classifier" |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
logging.info("Launching the app") |
|
gradio_app.launch() |
|
|