Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import PIL
|
| 2 |
+
from torchvision import datasets, transforms, models
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import torch.nn as nn
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import numpy as np
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
class_names = ['apple_pie',
|
| 11 |
+
'bibimbap',
|
| 12 |
+
'cannoli',
|
| 13 |
+
'edamame',
|
| 14 |
+
'falafel',
|
| 15 |
+
'french_toast',
|
| 16 |
+
'ramen',
|
| 17 |
+
'sushi',
|
| 18 |
+
'tiramisu']
|
| 19 |
+
|
| 20 |
+
def pil_loader(path):
|
| 21 |
+
with open(path, 'rb') as f:
|
| 22 |
+
img = Image.open(f)
|
| 23 |
+
return img.convert('RGB')
|
| 24 |
+
|
| 25 |
+
def predict(img_path):
|
| 26 |
+
# Load and preprocess the image
|
| 27 |
+
# image = pil_loader(img_path)
|
| 28 |
+
# Convert Gradio image input to a NumPy array
|
| 29 |
+
img_array = img_path.astype(np.uint8)
|
| 30 |
+
|
| 31 |
+
# # Convert NumPy array to PIL Image
|
| 32 |
+
image = Image.fromarray(img_array)
|
| 33 |
+
|
| 34 |
+
test_transforms = transforms.Compose([
|
| 35 |
+
transforms.Resize(256),
|
| 36 |
+
transforms.CenterCrop(224),
|
| 37 |
+
transforms.ToTensor(),
|
| 38 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 39 |
+
])
|
| 40 |
+
|
| 41 |
+
# Apply transformations
|
| 42 |
+
image = test_transforms(image)
|
| 43 |
+
|
| 44 |
+
inf_model = models.resnet18(pretrained=False)
|
| 45 |
+
num_ftrs = inf_model.fc.in_features
|
| 46 |
+
# Here the size of each output sample is set to 2.
|
| 47 |
+
# Alternatively, it can be generalized to nn.Linear(num_ftrs, len(class_names)).
|
| 48 |
+
inf_model.fc = nn.Linear(num_ftrs, len(class_names))
|
| 49 |
+
|
| 50 |
+
# model_1 = model_1.to(device)
|
| 51 |
+
inf_model.to(torch.device('cpu'))
|
| 52 |
+
inf_model.load_state_dict(torch.load('./resnet18_tinyfood_classifier.pth', map_location='cpu'))
|
| 53 |
+
|
| 54 |
+
# Perform inference
|
| 55 |
+
with torch.no_grad():
|
| 56 |
+
inf_model.eval()
|
| 57 |
+
out = inf_model(image.unsqueeze(0)) # Add batch dimension
|
| 58 |
+
|
| 59 |
+
# Get the predicted class and confidence
|
| 60 |
+
_, preds = torch.max(out, 1)
|
| 61 |
+
idx = preds.cpu().numpy()[0]
|
| 62 |
+
pred_class = class_names[idx]
|
| 63 |
+
|
| 64 |
+
# Assuming `out` is logits, you may need to apply softmax instead of sigmoid
|
| 65 |
+
probabilities = torch.softmax(out, dim=1) # Apply softmax to get probabilities
|
| 66 |
+
confidence = probabilities[0, idx].item() * 100 # Get confidence for the predicted class
|
| 67 |
+
|
| 68 |
+
nutrition_data_path = './food-data.csv'
|
| 69 |
+
# Membaca file CSV
|
| 70 |
+
df = pd.read_csv(nutrition_data_path)
|
| 71 |
+
|
| 72 |
+
# Mencocokkan prediksi dengan data CSV
|
| 73 |
+
if pred_class.capitalize() in df["Makanan"].values:
|
| 74 |
+
row = df.loc[df["Makanan"] == pred_class.capitalize()]
|
| 75 |
+
|
| 76 |
+
# Mengambil informasi gizi
|
| 77 |
+
calories = row["Kalori"].values[0]
|
| 78 |
+
protein = row["Protein"].values[0]
|
| 79 |
+
fat = row["Lemak"].values[0]
|
| 80 |
+
carbs = row["Karbohidrat"].values[0]
|
| 81 |
+
fiber = row["Serat"].values[0]
|
| 82 |
+
sugar = row["Gula"].values[0]
|
| 83 |
+
price = row["Harga (Rp)"].values[0]
|
| 84 |
+
|
| 85 |
+
return pred_class, calories, protein, fat, carbs, fiber, sugar, price
|
| 86 |
+
else:
|
| 87 |
+
nutrition_info = None
|
| 88 |
+
return 'Food not found', 0, 0, 0, 0, 0, 0
|
| 89 |
+
|
| 90 |
+
# return pred_class, confidence
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# img_path = '/content/drive/MyDrive/Assignment-Citra-SkillacademyAI/bibimbap.jpeg'
|
| 94 |
+
# print(predict(img_path))
|
| 95 |
+
|
| 96 |
+
interface = gr.Interface(
|
| 97 |
+
predict,
|
| 98 |
+
inputs="image",
|
| 99 |
+
title="Selera Cafe App",
|
| 100 |
+
description="This App will provide the information of your food choice in Selera Cafe. The menu includes: Apple Pie, Bibimbap, Cannoli, Edamame, Falafel, French Toast, Ramen, Sushi, Tiramisu. Enjoy your food!",
|
| 101 |
+
|
| 102 |
+
outputs=[
|
| 103 |
+
gr.Text(label="Food Label"),
|
| 104 |
+
gr.Number(label="Calories"),
|
| 105 |
+
gr.Number(label="Protein"),
|
| 106 |
+
gr.Number(label="Fat"),
|
| 107 |
+
gr.Number(label="Carbs"),
|
| 108 |
+
gr.Number(label="Fiber"),
|
| 109 |
+
gr.Number(label="Sugar"),
|
| 110 |
+
gr.Number(label="Price")
|
| 111 |
+
],
|
| 112 |
+
examples = [
|
| 113 |
+
'/content/drive/MyDrive/Assignment-Citra-SkillacademyAI/bibimbap.jpeg',
|
| 114 |
+
'/content/drive/MyDrive/Assignment-Citra-SkillacademyAI/food-101-tiny/apple-pie.jpeg',
|
| 115 |
+
'/content/drive/MyDrive/Assignment-Citra-SkillacademyAI/food-101-tiny/cannoli.jpeg'
|
| 116 |
+
])
|
| 117 |
+
interface.launch()
|