Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
-
from transformers import
|
4 |
from PIL import Image
|
5 |
-
import torch
|
6 |
|
7 |
-
#
|
8 |
-
model_name = "ahmadalfian/fruits_vegetables_classifier"
|
9 |
-
|
10 |
-
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
11 |
-
|
12 |
-
# Fungsi untuk memprediksi kelas
|
13 |
-
def predict(image):
|
14 |
-
# Preprocess gambar menggunakan feature extractor
|
15 |
-
inputs = feature_extractor(images=image, return_tensors="pt")
|
16 |
-
with torch.no_grad():
|
17 |
-
outputs = model(**inputs)
|
18 |
-
predictions = outputs.logits.argmax(dim=1)
|
19 |
-
return predictions.item()
|
20 |
|
21 |
# Fungsi untuk mengambil informasi nutrisi
|
22 |
def get_nutritional_info(food):
|
@@ -59,20 +48,13 @@ def get_nutritional_info(food):
|
|
59 |
else:
|
60 |
return None
|
61 |
|
62 |
-
# Fungsi utama
|
63 |
def classify_and_get_nutrition(image):
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
'carrot', 'cauliflower', 'chilli pepper', 'corn', 'cucumber',
|
68 |
-
'eggplant', 'garlic', 'ginger', 'grapes', 'jalepeno', 'kiwi',
|
69 |
-
'lemon', 'lettuce', 'mango', 'onion', 'orange', 'paprika',
|
70 |
-
'pear', 'peas', 'pineapple', 'pomegranate', 'potato', 'raddish',
|
71 |
-
'soy beans', 'spinach', 'sweetcorn', 'sweetpotato', 'tomato',
|
72 |
-
'turnip', 'watermelon'
|
73 |
-
] # Semua label kelas
|
74 |
-
predicted_label = class_labels[predicted_class_idx]
|
75 |
|
|
|
76 |
nutrisi = get_nutritional_info(predicted_label)
|
77 |
|
78 |
if nutrisi:
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
+
from transformers import pipeline
|
4 |
from PIL import Image
|
|
|
5 |
|
6 |
+
# Menggunakan pipeline dari Hugging Face
|
7 |
+
model_name = "ahmadalfian/fruits_vegetables_classifier" # Model klasifikasi
|
8 |
+
classifier = pipeline("image-classification", model=model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Fungsi untuk mengambil informasi nutrisi
|
11 |
def get_nutritional_info(food):
|
|
|
48 |
else:
|
49 |
return None
|
50 |
|
51 |
+
# Fungsi utama untuk klasifikasi dan informasi nutrisi
|
52 |
def classify_and_get_nutrition(image):
|
53 |
+
# Prediksi menggunakan pipeline
|
54 |
+
predictions = classifier(image)
|
55 |
+
predicted_label = predictions[0]['label'].lower() # Ambil label prediksi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
# Ambil informasi nutrisi
|
58 |
nutrisi = get_nutritional_info(predicted_label)
|
59 |
|
60 |
if nutrisi:
|