jays009 commited on
Commit
2201868
·
verified ·
1 Parent(s): 1f6dbae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py CHANGED
@@ -1,3 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def predict(data):
2
  try:
3
  image_input = data.get('image', None)
 
1
+ import gradio as gr
2
+ import json
3
+ import torch
4
+ from torch import nn
5
+ from torchvision import models, transforms
6
+ from huggingface_hub import hf_hub_download
7
+ from PIL import Image
8
+ import requests
9
+ import base64
10
+ from io import BytesIO
11
+ import os
12
+
13
+ # Define the number of classes
14
+ num_classes = 2
15
+
16
+ # Download model from Hugging Face
17
+ def download_model():
18
+ try:
19
+ model_path = hf_hub_download(repo_id="jays009/Restnet50", filename="pytorch_model.bin")
20
+ return model_path
21
+ except Exception as e:
22
+ print(f"Error downloading model: {e}")
23
+ return None
24
+
25
+ # Load the model from Hugging Face
26
+ def load_model(model_path):
27
+ try:
28
+ model = models.resnet50(pretrained=False)
29
+ model.fc = nn.Linear(model.fc.in_features, num_classes)
30
+ model.load_state_dict(torch.load(model_path, map_location=torch.device("cpu")))
31
+ model.eval()
32
+ return model
33
+ except Exception as e:
34
+ print(f"Error loading model: {e}")
35
+ return None
36
+
37
+ # Download the model and load it
38
+ model_path = download_model()
39
+ model = load_model(model_path) if model_path else None
40
+
41
+ # Define the transformation for the input image
42
+ transform = transforms.Compose([
43
+ transforms.Resize(256),
44
+ transforms.CenterCrop(224),
45
+ transforms.ToTensor(),
46
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
47
+ ])
48
+
49
  def predict(data):
50
  try:
51
  image_input = data.get('image', None)