Spaces:
Runtime error
Runtime error
ahmadalfian
commited on
Commit
•
32fb78d
1
Parent(s):
2491ca4
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
from transformers import AutoModelForImageClassification, AutoTokenizer
|
4 |
+
from PIL import Image
|
5 |
+
import torch
|
6 |
+
|
7 |
+
# Menginisialisasi model dan tokenizer dari Hugging Face
|
8 |
+
model_name = "ahmadalfian/fruits_vegetables_classifier" # Model yang kamu sebutkan
|
9 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
+
|
12 |
+
# Fungsi untuk memprediksi kelas
|
13 |
+
def predict(image):
|
14 |
+
image = image.convert("RGB")
|
15 |
+
inputs = tokenizer(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):
|
23 |
+
api_key = "3pm2NGZzYongVN1gRjnroVLUpsHC8rKWJFyx5moq"
|
24 |
+
url = "https://api.nal.usda.gov/fdc/v1/foods/search"
|
25 |
+
|
26 |
+
params = {
|
27 |
+
"query": food, # Nama makanan yang diprediksi
|
28 |
+
"pageSize": 5, # Ambil lima hasil
|
29 |
+
"api_key": api_key
|
30 |
+
}
|
31 |
+
|
32 |
+
response = requests.get(url, params=params)
|
33 |
+
data = response.json()
|
34 |
+
|
35 |
+
if "foods" in data and len(data["foods"]) > 0:
|
36 |
+
# Inisialisasi total untuk nutrisi yang diinginkan
|
37 |
+
nutrients_totals = {
|
38 |
+
"Energy": 0,
|
39 |
+
"Carbohydrate, by difference": 0,
|
40 |
+
"Fiber, total dietary": 0,
|
41 |
+
"Vitamin C, total ascorbic acid": 0
|
42 |
+
}
|
43 |
+
item_count = len(data["foods"])
|
44 |
+
|
45 |
+
# Iterasi melalui setiap makanan
|
46 |
+
for food in data["foods"]:
|
47 |
+
for nutrient in food['foodNutrients']:
|
48 |
+
nutrient_name = nutrient['nutrientName']
|
49 |
+
nutrient_value = nutrient['value']
|
50 |
+
|
51 |
+
# Cek apakah nutrisi termasuk yang diinginkan
|
52 |
+
if nutrient_name in nutrients_totals:
|
53 |
+
nutrients_totals[nutrient_name] += nutrient_value
|
54 |
+
|
55 |
+
# Menghitung rata-rata nilai nutrisi
|
56 |
+
average_nutrients = {name: total / item_count for name, total in nutrients_totals.items()}
|
57 |
+
|
58 |
+
return average_nutrients
|
59 |
+
else:
|
60 |
+
return None
|
61 |
+
|
62 |
+
# Fungsi utama Gradio
|
63 |
+
def classify_and_get_nutrition(image):
|
64 |
+
predicted_class_idx = predict(image)
|
65 |
+
class_labels = [
|
66 |
+
'apple', 'banana', 'beetroot', 'bell pepper', 'cabbage', 'capsicum',
|
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:
|
79 |
+
return {
|
80 |
+
"Predicted Class": predicted_label,
|
81 |
+
"Energy (kcal)": nutrisi["Energy"],
|
82 |
+
"Carbohydrates (g)": nutrisi["Carbohydrate, by difference"],
|
83 |
+
"Fiber (g)": nutrisi["Fiber, total dietary"],
|
84 |
+
"Vitamin C (mg)": nutrisi["Vitamin C, total ascorbic acid"]
|
85 |
+
}
|
86 |
+
else:
|
87 |
+
return {
|
88 |
+
"Predicted Class": predicted_label,
|
89 |
+
"Nutritional Information": "Not Found"
|
90 |
+
}
|
91 |
+
|
92 |
+
# Antarmuka Gradio
|
93 |
+
iface = gr.Interface(
|
94 |
+
fn=classify_and_get_nutrition,
|
95 |
+
inputs=gr.inputs.Image(type="pil"),
|
96 |
+
outputs=gr.outputs.JSON(),
|
97 |
+
title="Fruits and Vegetables Classifier",
|
98 |
+
description="Upload an image of a fruit or vegetable to classify and get its nutritional information."
|
99 |
+
)
|
100 |
+
|
101 |
+
iface.launch()
|