Spaces:
Sleeping
Sleeping
Update App to compare to Zero Shot model
Browse filesFiles changed (2)
- app.py+34-7
- requirements.txt+3-1
12345678910119 unmodified lines212223242526272829import gradio as grfrom transformers import pipelineclassifier = pipeline("image-classification", model="Skorm/food11-vit")def classify_food(image):results = classifier(image)return {result["label"]: round(result["score"], 4) for result in results}# Example image pathsexamples = [9 unmodified linesiface = gr.Interface(fn=classify_food,inputs=gr.Image(type="filepath"),outputs=gr.Label(num_top_classes=3),title="🍽️ Food Classification with ViT",description="Upload a food image to classify it into 1 of 11 food categories.",examples=examples)12345678910111213141516171819202122232425262728293031323334359 unmodified lines454647484950515253545556import gradio as grfrom transformers import pipeline, CLIPProcessor, CLIPModelfrom PIL import Imageimport torchclassifier = pipeline("image-classification", model="Skorm/food11-vit")# Load CLIP modelclip_model = CLIPModel.from_pretrained("openai/clip-vit-large-patch14")clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-large-patch14")# Define CLIP labelsclip_labels = ["bread", "dairy product", "dessert", "egg", "fried food","meat", "noodles or pasta", "rice", "seafood", "soup", "vegetables or fruits"]def classify_food(image_path):image = Image.open(image_path)# ----- ViT prediction -----vit_results = classifier(image_path)vit_output = {result["label"]: round(result["score"], 4) for result in vit_results}# ----- CLIP zero-shot prediction -----inputs = clip_processor(text=clip_labels, images=image, return_tensors="pt", padding=True)outputs = clip_model(**inputs)probs = outputs.logits_per_image.softmax(dim=1)[0]clip_output = {label: round(float(score), 4) for label, score in zip(clip_labels, probs)}return vit_output, clip_output# Example image pathsexamples = [9 unmodified linesiface = gr.Interface(fn=classify_food,inputs=gr.Image(type="filepath"),outputs=[gr.Label(num_top_classes=3, label="ViT (Fine-tuned) Prediction"),gr.Label(num_top_classes=3, label="CLIP Zero-Shot Prediction")],title="🍽️ Food Classification with ViT and Zero-Shot CLIP",description="Upload a food image. The app compares predictions between your fine-tuned ViT model and zero-shot CLIP.",examples=examples)
12transformerstorch1234transformerstorchgradioPillow
app.py+34-7
requirements.txt+3-1