Spaces:
Runtime error
Runtime error
ahmadalfian
commited on
Commit
•
118c1c8
1
Parent(s):
3d24279
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,34 @@
|
|
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 |
-
|
|
|
8 |
|
9 |
-
# Mengunduh
|
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 |
-
#
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# Fungsi untuk memprediksi kelas
|
27 |
def predict(image):
|
|
|
28 |
image = image.convert("RGB")
|
29 |
-
|
|
|
|
|
30 |
with torch.no_grad():
|
31 |
outputs = model(image_tensor)
|
|
|
32 |
predictions = outputs.argmax(dim=1)
|
33 |
return predictions.item()
|
34 |
|
@@ -47,6 +47,7 @@ def get_nutritional_info(food):
|
|
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,
|
@@ -64,7 +65,6 @@ def get_nutritional_info(food):
|
|
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
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
|
|
|
|
|
|
3 |
from huggingface_hub import hf_hub_download
|
4 |
+
import torch
|
5 |
+
from PIL import Image
|
6 |
|
7 |
+
# Mengunduh model dari Hugging Face
|
8 |
model_path = hf_hub_download(repo_id="ahmadalfian/fruits_vegetables_classifier", filename="resnet50_finetuned.pth")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Menginisialisasi model
|
11 |
+
class FruitVegetableClassifier(torch.nn.Module):
|
12 |
+
def __init__(self):
|
13 |
+
super(FruitVegetableClassifier, self).__init__()
|
14 |
+
self.model = torch.load(model_path)
|
15 |
+
self.model.eval()
|
16 |
+
|
17 |
+
def forward(self, x):
|
18 |
+
return self.model(x)
|
19 |
+
|
20 |
+
model = FruitVegetableClassifier()
|
21 |
|
22 |
# Fungsi untuk memprediksi kelas
|
23 |
def predict(image):
|
24 |
+
# Proses gambar di sini (ubah ukuran, normalisasi, dll.)
|
25 |
image = image.convert("RGB")
|
26 |
+
image = image.resize((224, 224)) # Resize ke ukuran yang diharapkan model
|
27 |
+
image_tensor = torch.from_numpy(np.array(image)).permute(2, 0, 1).float().unsqueeze(0) # Ubah menjadi tensor
|
28 |
+
|
29 |
with torch.no_grad():
|
30 |
outputs = model(image_tensor)
|
31 |
+
|
32 |
predictions = outputs.argmax(dim=1)
|
33 |
return predictions.item()
|
34 |
|
|
|
47 |
data = response.json()
|
48 |
|
49 |
if "foods" in data and len(data["foods"]) > 0:
|
50 |
+
# Inisialisasi total untuk nutrisi yang diinginkan
|
51 |
nutrients_totals = {
|
52 |
"Energy": 0,
|
53 |
"Carbohydrate, by difference": 0,
|
|
|
65 |
nutrients_totals[nutrient_name] += nutrient_value
|
66 |
|
67 |
average_nutrients = {name: total / item_count for name, total in nutrients_totals.items()}
|
|
|
68 |
return average_nutrients
|
69 |
else:
|
70 |
return None
|