|
import gradio as gr |
|
from transformers import AutoModelForImageClassification, AutoFeatureExtractor |
|
from PIL import Image |
|
import torch |
|
|
|
|
|
model_id = "KabeerAmjad/food_classification_model" |
|
model = AutoModelForImageClassification.from_pretrained(model_id) |
|
feature_extractor = AutoFeatureExtractor.from_pretrained(model_id) |
|
|
|
|
|
def classify_image(img): |
|
inputs = feature_extractor(images=img, return_tensors="pt") |
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
probs = torch.softmax(outputs.logits, dim=-1) |
|
|
|
|
|
top_label = model.config.id2label[probs.argmax().item()] |
|
return top_label |
|
|
|
|
|
iface = gr.Interface( |
|
fn=classify_image, |
|
inputs=gr.Image(type="pil"), |
|
outputs="text", |
|
title="Food Image Classification", |
|
description="Upload an image to classify if it’s an apple pie, etc." |
|
) |
|
|
|
|
|
iface.launch() |
|
|