Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,29 @@
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import torchvision.transforms as transforms
|
| 3 |
import torchvision.models as models
|
| 4 |
from PIL import Image
|
| 5 |
import json
|
| 6 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Load the model with updated weights parameter
|
| 9 |
model = models.resnet50(weights=models.ResNet50_Weights.DEFAULT)
|
| 10 |
model.eval() # Set model to evaluation mode
|
| 11 |
|
| 12 |
# Load the model's custom state_dict
|
| 13 |
-
model_path = 'food_classification_model.pth'
|
| 14 |
try:
|
| 15 |
state_dict = torch.load(model_path, map_location=torch.device('cpu'))
|
| 16 |
model.load_state_dict(state_dict)
|
|
|
|
| 1 |
+
import os
|
| 2 |
import torch
|
| 3 |
import torchvision.transforms as transforms
|
| 4 |
import torchvision.models as models
|
| 5 |
from PIL import Image
|
| 6 |
import json
|
| 7 |
import gradio as gr
|
| 8 |
+
import requests
|
| 9 |
+
|
| 10 |
+
# Path to the model file and Hugging Face URL
|
| 11 |
+
model_path = 'food_classification_model.pth'
|
| 12 |
+
model_url = "https://huggingface.co/KabeerAmjad/food_classification_model/resolve/main/food_classification_model.pth"
|
| 13 |
+
|
| 14 |
+
# Download the model file if it's not already available
|
| 15 |
+
if not os.path.exists(model_path):
|
| 16 |
+
print(f"Downloading the model from {model_url}...")
|
| 17 |
+
response = requests.get(model_url)
|
| 18 |
+
with open(model_path, 'wb') as f:
|
| 19 |
+
f.write(response.content)
|
| 20 |
+
print("Model downloaded successfully.")
|
| 21 |
|
| 22 |
# Load the model with updated weights parameter
|
| 23 |
model = models.resnet50(weights=models.ResNet50_Weights.DEFAULT)
|
| 24 |
model.eval() # Set model to evaluation mode
|
| 25 |
|
| 26 |
# Load the model's custom state_dict
|
|
|
|
| 27 |
try:
|
| 28 |
state_dict = torch.load(model_path, map_location=torch.device('cpu'))
|
| 29 |
model.load_state_dict(state_dict)
|