Spaces:
Runtime error
Runtime error
| import os | |
| import requests | |
| import json | |
| import torch | |
| import timm | |
| import torch.nn as nn | |
| import torchvision | |
| from torchvision import transforms, datasets, models | |
| import torch.nn.functional as F | |
| import PIL | |
| import PIL.Image as Image | |
| import numpy as np | |
| from transformers import CLIPProcessor, CLIPModel | |
| classes_outside_india = ['apple pie', 'baby back ribs', 'baklava', 'beef carpaccio', 'beef tartare', | |
| 'beet salad', 'beignets', 'bibimbap', 'bread pudding', 'breakfast burrito', | |
| 'bruschetta', 'caesar_salad', 'cannoli', 'caprese salad', 'carrot cake', | |
| 'ceviche', 'cheese plate', 'cheesecake', 'chicken curry', | |
| 'chicken quesadilla', 'chicken wings', 'chocolate cake', 'chocolate mousse', | |
| 'churros', 'clam chowder', 'club sandwich', 'crab cakes', 'creme brulee', | |
| 'croque madame', 'cup cakes', 'deviled eggs', 'donuts', 'dumplings', 'edamame', | |
| 'eggs benedict', 'escargots', 'falafel', 'filet mignon', 'fish and chips', | |
| 'foie gras', 'french fries', 'french onion soup', 'french toast', | |
| 'fried calamari', 'fried rice', 'frozen yogurt', 'garlic bread', 'gnocchi', | |
| 'greek salad', 'grilled cheese sandwich', 'grilled salmon', 'guacamole', | |
| 'gyoza', 'hamburger', 'hot and sour soup', 'hot dog', 'huevos rancheros', | |
| 'hummus', 'ice cream', 'lasagna', 'lobster bisque', 'lobster roll sandwich', | |
| 'macaroni and cheese', 'macarons', 'miso soup', 'mussels', 'nachos', | |
| 'omelette', 'onion rings', 'oysters', 'pad thai', 'paella', 'pancakes', | |
| 'panna cotta', 'peking duck', 'pho', 'pizza', 'pork chop', 'poutine', | |
| 'prime rib', 'pulled pork sandwich', 'ramen', 'ravioli', 'red velvet cake', | |
| 'risotto', 'samosa', 'sashimi', 'scallops', 'seaweed salad', | |
| 'shrimp and grits', 'spaghetti bolognese', 'spaghetti carbonara', | |
| 'spring rolls', 'steak', 'strawberry_shortcake', 'sushi', 'tacos', 'takoyaki', | |
| 'tiramisu', 'tuna tartare', 'waffles'] | |
| classes_india = ['burger','butter_naan', 'chai', 'chapati', 'chole_bhature', 'dal_makhani', 'dhokla', 'fried_rice', 'idli', | |
| 'jalebi', 'kaathi_rolls', 'kadai_paneer', 'kulfi', 'masala_dosa', 'momos', 'paani_puri', 'pakode', 'pav_bhaji', | |
| 'pizza', 'samosa'] | |
| def food_nofood_pred(input_image): | |
| # input labels for clip model | |
| labels = ['food', 'not food'] | |
| # CLIP Model for classification | |
| food_nofood_model = CLIPModel.from_pretrained("flax-community/clip-rsicd-v2") | |
| processor = CLIPProcessor.from_pretrained("flax-community/clip-rsicd-v2") | |
| # image = Image.open(requests.get(uploaded_file, stream=True).raw) | |
| inputs = processor(text=[f"a photo of a {l}" for l in labels], images=input_image, return_tensors="pt", padding=True) | |
| outputs = food_nofood_model(**inputs) | |
| logits_per_image = outputs.logits_per_image | |
| probs = logits_per_image.softmax(dim=1) | |
| print(probs) | |
| pred = probs.detach().cpu().numpy().argmax(axis=1) | |
| pred_class = labels[pred[0]] | |
| return pred_class | |
| def make_pred_outside_india(input_img, model, device, user_location): | |
| input_img = input_img.unsqueeze(0) | |
| model.eval() | |
| pred = model(input_img) | |
| # if torch.cuda.is_available(): | |
| # pred = F.softmax(pred).detach().cpu().numpy() | |
| # y_prob = pred.argmax(axis=1)[0] #return index with highest class probability | |
| # else: | |
| pred = F.softmax(pred).detach().numpy() | |
| y_prob = pred.argmax(axis=1)[0] | |
| if(user_location=='Outside India'): | |
| class_label = classes_outside_india[y_prob] | |
| elif(user_location=='India'): | |
| class_label = classes_india[y_prob] | |
| return class_label | |
| def getmodel_outside_india(model_path): | |
| # device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | |
| effnet_b0 = timm.create_model(pretrained=True, model_name='tf_efficientnet_b0') | |
| for param in effnet_b0.parameters(): | |
| param.requires_grad = True | |
| effnet_b0.classifier = nn.Linear(1280, len(classes_outside_india)) | |
| effnet_b0 = effnet_b0 | |
| #Model Loading | |
| effnet_b0.load_state_dict(torch.load(model_path,map_location='cpu')) | |
| return effnet_b0 | |
| def getmodel_india(model_path): | |
| #defining model | |
| # device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | |
| effnet_b0 = timm.create_model(pretrained=True, model_name='tf_efficientnet_b0') | |
| for param in effnet_b0.parameters(): | |
| param.requires_grad = True | |
| effnet_b0.classifier = nn.Linear(1280, len(classes_india)) | |
| effnet_b0 = effnet_b0 | |
| #Model Loading | |
| effnet_b0.load_state_dict(torch.load(model_path, map_location='cpu')) | |
| return effnet_b0 | |
| def load_prepare_img(image): | |
| normalize = transforms.Normalize( | |
| [0.485, 0.456, 0.406], | |
| [0.229, 0.224, 0.225] | |
| ) | |
| test_transform = transforms.Compose([ | |
| transforms.Resize((225, 225)), | |
| transforms.CenterCrop(224), | |
| transforms.ToTensor(), | |
| normalize, | |
| ]) | |
| input_img = test_transform(image) | |
| device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | |
| return input_img,device | |
| def fetch_recipe(food_name): | |
| url = "https://recipesapi2.p.rapidapi.com/recipes/"+food_name | |
| querystring = {"maxRecipes":"1"} | |
| headers = { | |
| 'x-rapidapi-host': "recipesapi2.p.rapidapi.com", | |
| 'x-rapidapi-key': "f6f6823b91msh9e92fed91d5356ap136f5djsn494d8f582fb3" | |
| } | |
| response = requests.request("GET", url, headers=headers, params=querystring) | |
| json_data = json.loads(response.text) | |
| recipe_data = json_data['data'][0] | |
| return recipe_data |