jatin1233232 commited on
Commit
06007f4
·
verified ·
1 Parent(s): a4168df

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoImageProcessor, SiglipForImageClassification
3
+ from PIL import Image
4
+ import torch
5
+
6
+ # Load model and processor
7
+ model_name = "prithivMLmods/Food-101-93M"
8
+ model = SiglipForImageClassification.from_pretrained(model_name)
9
+ processor = AutoImageProcessor.from_pretrained(model_name)
10
+
11
+ # Food-101 labels
12
+ labels = {
13
+ "0": "apple_pie", "1": "baby_back_ribs", "2": "baklava", "3": "beef_carpaccio", "4": "beef_tartare",
14
+ "5": "beet_salad", "6": "beignets", "7": "bibimbap", "8": "bread_pudding", "9": "breakfast_burrito",
15
+ "10": "bruschetta", "11": "caesar_salad", "12": "cannoli", "13": "caprese_salad", "14": "carrot_cake",
16
+ "15": "ceviche", "16": "cheesecake", "17": "cheese_plate", "18": "chicken_curry", "19": "chicken_quesadilla",
17
+ "20": "chicken_wings", "21": "chocolate_cake", "22": "chocolate_mousse", "23": "churros", "24": "clam_chowder",
18
+ "25": "club_sandwich", "26": "crab_cakes", "27": "creme_brulee", "28": "croque_madame", "29": "cup_cakes",
19
+ "30": "deviled_eggs", "31": "donuts", "32": "dumplings", "33": "edamame", "34": "eggs_benedict",
20
+ "35": "escargots", "36": "falafel", "37": "filet_mignon", "38": "fish_and_chips", "39": "foie_gras",
21
+ "40": "french_fries", "41": "french_onion_soup", "42": "french_toast", "43": "fried_calamari", "44": "fried_rice",
22
+ "45": "frozen_yogurt", "46": "garlic_bread", "47": "gnocchi", "48": "greek_salad", "49": "grilled_cheese_sandwich",
23
+ "50": "grilled_salmon", "51": "guacamole", "52": "gyoza", "53": "hamburger", "54": "hot_and_sour_soup",
24
+ "55": "hot_dog", "56": "huevos_rancheros", "57": "hummus", "58": "ice_cream", "59": "lasagna",
25
+ "60": "lobster_bisque", "61": "lobster_roll_sandwich", "62": "macaroni_and_cheese", "63": "macarons", "64": "miso_soup",
26
+ "65": "mussels", "66": "nachos", "67": "omelette", "68": "onion_rings", "69": "oysters",
27
+ "70": "pad_thai", "71": "paella", "72": "pancakes", "73": "panna_cotta", "74": "peking_duck",
28
+ "75": "pho", "76": "pizza", "77": "pork_chop", "78": "poutine", "79": "prime_rib",
29
+ "80": "pulled_pork_sandwich", "81": "ramen", "82": "ravioli", "83": "red_velvet_cake", "84": "risotto",
30
+ "85": "samosa", "86": "sashimi", "87": "scallops", "88": "seaweed_salad", "89": "shrimp_and_grits",
31
+ "90": "spaghetti_bolognese", "91": "spaghetti_carbonara", "92": "spring_rolls", "93": "steak", "94": "strawberry_shortcake",
32
+ "95": "sushi", "96": "tacos", "97": "takoyaki", "98": "tiramisu", "99": "tuna_tartare", "100": "waffles"
33
+ }
34
+
35
+ def classify_food(image):
36
+ """Predicts the type of food in the image."""
37
+ image = Image.fromarray(image).convert("RGB")
38
+ inputs = processor(images=image, return_tensors="pt")
39
+
40
+ with torch.no_grad():
41
+ outputs = model(**inputs)
42
+ logits = outputs.logits
43
+ probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist()
44
+
45
+ predictions = {labels[str(i)]: round(probs[i], 3) for i in range(len(probs))}
46
+ # Sort by descending probability
47
+ predictions = dict(sorted(predictions.items(), key=lambda item: item[1], reverse=True)[:5])
48
+
49
+ return predictions
50
+
51
+ # Gradio Interface
52
+ iface = gr.Interface(
53
+ fn=classify_food,
54
+ inputs=gr.Image(type="numpy"),
55
+ outputs=gr.Label(num_top_classes=5, label="Top 5 Prediction Scores"),
56
+ title="Food-101-93M 🍽️",
57
+ description="Upload an image of food to classify it into one of 101 dish categories based on the Food-101 dataset."
58
+ )
59
+
60
+ # Launch app
61
+ if __name__ == "__main__":
62
+ iface.launch()