canadianjosieharrison's picture
Update app.py
0292ce3 verified
import gradio as gr
from transformers import pipeline, AutoFeatureExtractor, AutoModel
from PIL import Image
import torch
import numpy as np
import joblib
from huggingface_hub import hf_hub_download
# Load transformer feature extractor
feature_extractor = AutoFeatureExtractor.from_pretrained("google/vit-base-patch16-224")
model = AutoModel.from_pretrained("google/vit-base-patch16-224")
# Load your trained classifier (e.g., sklearn model)
model_path = hf_hub_download(repo_id="canadianjosieharrison/texture-logistic-model", filename="classifier.joblib")
classifier = joblib.load(model_path) # Make sure this exists
# Create label map
id2label = {0:"metal", 1:"stone", 2:"wood"}
# Prediction function
def classify_image(img: Image.Image):
# Step 1: Extract features from image
inputs = feature_extractor(images=img, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
embedding = outputs.last_hidden_state[:, 0, :].numpy() # CLS token
# Step 2: Predict using classifier
prediction = classifier.predict(embedding)
return f"Predicted Material: {id2label[prediction[0]]}"
# Gradio interface
interface = gr.Interface(
fn=classify_image,
inputs=gr.Image(type="pil"),
outputs="text",
title="Material texture image classifier",
description="Upload a detail texture image of a material and the model will predict whether it's metal, stone, or wood.",
examples = ["metal.PNG", "stone.PNG", "wood.PNG"],
)
if __name__ == "__main__":
interface.launch(share=True)