Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,36 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
-
from transformers import pipeline
|
4 |
from PIL import Image
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Fungsi untuk mengambil informasi nutrisi
|
11 |
def get_nutritional_info(food):
|
@@ -13,8 +38,8 @@ def get_nutritional_info(food):
|
|
13 |
url = "https://api.nal.usda.gov/fdc/v1/foods/search"
|
14 |
|
15 |
params = {
|
16 |
-
"query": food,
|
17 |
-
"pageSize": 5,
|
18 |
"api_key": api_key
|
19 |
}
|
20 |
|
@@ -22,7 +47,6 @@ def get_nutritional_info(food):
|
|
22 |
data = response.json()
|
23 |
|
24 |
if "foods" in data and len(data["foods"]) > 0:
|
25 |
-
# Inisialisasi total untuk nutrisi yang diinginkan
|
26 |
nutrients_totals = {
|
27 |
"Energy": 0,
|
28 |
"Carbohydrate, by difference": 0,
|
@@ -31,30 +55,34 @@ def get_nutritional_info(food):
|
|
31 |
}
|
32 |
item_count = len(data["foods"])
|
33 |
|
34 |
-
# Iterasi melalui setiap makanan
|
35 |
for food in data["foods"]:
|
36 |
for nutrient in food['foodNutrients']:
|
37 |
nutrient_name = nutrient['nutrientName']
|
38 |
nutrient_value = nutrient['value']
|
39 |
|
40 |
-
# Cek apakah nutrisi termasuk yang diinginkan
|
41 |
if nutrient_name in nutrients_totals:
|
42 |
nutrients_totals[nutrient_name] += nutrient_value
|
43 |
|
44 |
-
# Menghitung rata-rata nilai nutrisi
|
45 |
average_nutrients = {name: total / item_count for name, total in nutrients_totals.items()}
|
46 |
|
47 |
return average_nutrients
|
48 |
else:
|
49 |
return None
|
50 |
|
51 |
-
# Fungsi utama
|
52 |
def classify_and_get_nutrition(image):
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
-
# Ambil informasi nutrisi
|
58 |
nutrisi = get_nutritional_info(predicted_label)
|
59 |
|
60 |
if nutrisi:
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
|
|
3 |
from PIL import Image
|
4 |
+
import torch
|
5 |
+
from torchvision import models
|
6 |
+
from huggingface_hub import hf_hub_download
|
7 |
+
from torchvision import transforms
|
8 |
|
9 |
+
# Mengunduh dan mempersiapkan model
|
10 |
+
model_path = hf_hub_download(repo_id="ahmadalfian/fruits_vegetables_classifier", filename="resnet50_finetuned.pth")
|
11 |
+
model = models.resnet50(pretrained=False)
|
12 |
+
num_classes = 36
|
13 |
+
model.fc = torch.nn.Linear(in_features=2048, out_features=num_classes)
|
14 |
+
model.load_state_dict(torch.load(model_path))
|
15 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
16 |
+
model.to(device)
|
17 |
+
model.eval()
|
18 |
+
|
19 |
+
# Transformasi untuk pra-pemrosesan gambar
|
20 |
+
preprocess = transforms.Compose([
|
21 |
+
transforms.Resize((224, 224)),
|
22 |
+
transforms.ToTensor(),
|
23 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
24 |
+
])
|
25 |
+
|
26 |
+
# Fungsi untuk memprediksi kelas
|
27 |
+
def predict(image):
|
28 |
+
image = image.convert("RGB")
|
29 |
+
image_tensor = preprocess(image).unsqueeze(0).to(device) # Tambahkan dimensi batch dan pindahkan ke device
|
30 |
+
with torch.no_grad():
|
31 |
+
outputs = model(image_tensor)
|
32 |
+
predictions = outputs.argmax(dim=1)
|
33 |
+
return predictions.item()
|
34 |
|
35 |
# Fungsi untuk mengambil informasi nutrisi
|
36 |
def get_nutritional_info(food):
|
|
|
38 |
url = "https://api.nal.usda.gov/fdc/v1/foods/search"
|
39 |
|
40 |
params = {
|
41 |
+
"query": food,
|
42 |
+
"pageSize": 5,
|
43 |
"api_key": api_key
|
44 |
}
|
45 |
|
|
|
47 |
data = response.json()
|
48 |
|
49 |
if "foods" in data and len(data["foods"]) > 0:
|
|
|
50 |
nutrients_totals = {
|
51 |
"Energy": 0,
|
52 |
"Carbohydrate, by difference": 0,
|
|
|
55 |
}
|
56 |
item_count = len(data["foods"])
|
57 |
|
|
|
58 |
for food in data["foods"]:
|
59 |
for nutrient in food['foodNutrients']:
|
60 |
nutrient_name = nutrient['nutrientName']
|
61 |
nutrient_value = nutrient['value']
|
62 |
|
|
|
63 |
if nutrient_name in nutrients_totals:
|
64 |
nutrients_totals[nutrient_name] += nutrient_value
|
65 |
|
|
|
66 |
average_nutrients = {name: total / item_count for name, total in nutrients_totals.items()}
|
67 |
|
68 |
return average_nutrients
|
69 |
else:
|
70 |
return None
|
71 |
|
72 |
+
# Fungsi utama Gradio
|
73 |
def classify_and_get_nutrition(image):
|
74 |
+
predicted_class_idx = predict(image)
|
75 |
+
class_labels = [
|
76 |
+
'apple', 'banana', 'beetroot', 'bell pepper', 'cabbage', 'capsicum',
|
77 |
+
'carrot', 'cauliflower', 'chilli pepper', 'corn', 'cucumber',
|
78 |
+
'eggplant', 'garlic', 'ginger', 'grapes', 'jalepeno', 'kiwi',
|
79 |
+
'lemon', 'lettuce', 'mango', 'onion', 'orange', 'paprika',
|
80 |
+
'pear', 'peas', 'pineapple', 'pomegranate', 'potato', 'raddish',
|
81 |
+
'soy beans', 'spinach', 'sweetcorn', 'sweetpotato', 'tomato',
|
82 |
+
'turnip', 'watermelon'
|
83 |
+
]
|
84 |
+
predicted_label = class_labels[predicted_class_idx]
|
85 |
|
|
|
86 |
nutrisi = get_nutritional_info(predicted_label)
|
87 |
|
88 |
if nutrisi:
|