ahmadalfian commited on
Commit
6946f75
1 Parent(s): 118c1c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -4
app.py CHANGED
@@ -3,6 +3,7 @@ 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")
@@ -11,7 +12,9 @@ model_path = hf_hub_download(repo_id="ahmadalfian/fruits_vegetables_classifier",
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):
@@ -28,7 +31,7 @@ def predict(image):
28
 
29
  with torch.no_grad():
30
  outputs = model(image_tensor)
31
-
32
  predictions = outputs.argmax(dim=1)
33
  return predictions.item()
34
 
@@ -47,7 +50,6 @@ def get_nutritional_info(food):
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,
@@ -108,4 +110,4 @@ iface = gr.Interface(
108
  description="Upload an image of a fruit or vegetable to classify and get its nutritional information."
109
  )
110
 
111
- iface.launch()
 
3
  from huggingface_hub import hf_hub_download
4
  import torch
5
  from PIL import Image
6
+ import numpy as np
7
 
8
  # Mengunduh model dari Hugging Face
9
  model_path = hf_hub_download(repo_id="ahmadalfian/fruits_vegetables_classifier", filename="resnet50_finetuned.pth")
 
12
  class FruitVegetableClassifier(torch.nn.Module):
13
  def __init__(self):
14
  super(FruitVegetableClassifier, self).__init__()
15
+ self.model = torch.nn.Sequential(
16
+ torch.load(model_path, map_location=torch.device('cpu')) # Memastikan dimuat di CPU
17
+ )
18
  self.model.eval()
19
 
20
  def forward(self, x):
 
31
 
32
  with torch.no_grad():
33
  outputs = model(image_tensor)
34
+
35
  predictions = outputs.argmax(dim=1)
36
  return predictions.item()
37
 
 
50
  data = response.json()
51
 
52
  if "foods" in data and len(data["foods"]) > 0:
 
53
  nutrients_totals = {
54
  "Energy": 0,
55
  "Carbohydrate, by difference": 0,
 
110
  description="Upload an image of a fruit or vegetable to classify and get its nutritional information."
111
  )
112
 
113
+ iface.launch()