Spaces:
Running
on
Zero
Running
on
Zero
Create indian_western_food_classify.py
Browse files
indian_western_food_classify.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import spaces
|
3 |
+
from transformers import AutoImageProcessor
|
4 |
+
from transformers import SiglipForImageClassification
|
5 |
+
from transformers.image_utils import load_image
|
6 |
+
from PIL import Image
|
7 |
+
import torch
|
8 |
+
|
9 |
+
# Load model and processor
|
10 |
+
model_name = "prithivMLmods/Indian-Western-Food-34"
|
11 |
+
model = SiglipForImageClassification.from_pretrained(model_name)
|
12 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
13 |
+
|
14 |
+
@spaces.GPU
|
15 |
+
def food_classification(image):
|
16 |
+
"""Predicts the type of food in an image."""
|
17 |
+
image = Image.fromarray(image).convert("RGB")
|
18 |
+
inputs = processor(images=image, return_tensors="pt")
|
19 |
+
|
20 |
+
with torch.no_grad():
|
21 |
+
outputs = model(**inputs)
|
22 |
+
logits = outputs.logits
|
23 |
+
probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist()
|
24 |
+
|
25 |
+
labels = {
|
26 |
+
"0": "Baked Potato", "1": "Crispy Chicken", "2": "Donut", "3": "Fries",
|
27 |
+
"4": "Hot Dog", "5": "Sandwich", "6": "Taco", "7": "Taquito", "8": "Apple Pie",
|
28 |
+
"9": "Burger", "10": "Butter Naan", "11": "Chai", "12": "Chapati", "13": "Cheesecake",
|
29 |
+
"14": "Chicken Curry", "15": "Chole Bhature", "16": "Dal Makhani", "17": "Dhokla",
|
30 |
+
"18": "Fried Rice", "19": "Ice Cream", "20": "Idli", "21": "Jalebi", "22": "Kaathi Rolls",
|
31 |
+
"23": "Kadai Paneer", "24": "Kulfi", "25": "Masala Dosa", "26": "Momos", "27": "Omelette",
|
32 |
+
"28": "Paani Puri", "29": "Pakode", "30": "Pav Bhaji", "31": "Pizza", "32": "Samosa",
|
33 |
+
"33": "Sushi"
|
34 |
+
}
|
35 |
+
predictions = {labels[str(i)]: round(probs[i], 3) for i in range(len(probs))}
|
36 |
+
|
37 |
+
return predictions
|
38 |
+
|
39 |
+
# Create Gradio interface
|
40 |
+
iface = gr.Interface(
|
41 |
+
fn=food_classification,
|
42 |
+
inputs=gr.Image(type="numpy"),
|
43 |
+
outputs=gr.Label(label="Prediction Scores"),
|
44 |
+
title="Indian & Western Food Classification",
|
45 |
+
description="Upload a food image to classify it into one of the 34 food types."
|
46 |
+
)
|
47 |
+
|
48 |
+
# Launch the app
|
49 |
+
if __name__ == "__main__":
|
50 |
+
iface.launch()
|