flowers / app.py
InklingSutra's picture
Update app.py
82bfc7a
import subprocess
# Install torch using pip
subprocess.check_call(['pip', 'install', 'torch', 'transformers'])
import torch
from transformers import AutoModelForImageClassification, AutoFeatureExtractor
import gradio as gr
model_id = f'InklingSutra/vit-base-patch16-224-finetuned-flower'
labels = ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']
def classify_image(image):
model = AutoModelForImageClassification.from_pretrained(model_id)
feature_extractor = AutoFeatureExtractor.from_pretrained(model_id)
inp = feature_extractor(image, return_tensors='pt')
outp = model(**inp)
pred = torch.nn.functional.softmax(outp.logits, dim=-1)
preds = pred[0].cpu().detach().numpy()
confidence = {label: float(preds[i]) for i, label in enumerate(labels)}
return confidence
interface = gr.Interface(fn=classify_image,
inputs='image',
examples=['InklingSutra_daisy_flowers_52b0c0bb-ce51-4301-8e68-9797e64352e4.png', 'InklingSutra_roses_719cc3f1-7dcb-4b68-b593-108c9f029819.png'],
outputs='label').launch()