Hassan145 commited on
Commit
9ae7b7e
·
1 Parent(s): b83e613

Initial commit

Browse files
Files changed (3) hide show
  1. app.py +38 -0
  2. requirements.txt +5 -0
  3. resnet_model.pth +3 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from torchvision import models, transforms
4
+ from PIL import Image
5
+ import torch.nn as nn
6
+
7
+ # Define class names
8
+ class_names = ['Cabbage', 'Carrot', 'Cucumber', 'Tomato'] # replace with your actual classes
9
+
10
+ # Load the model
11
+ def load_model():
12
+ model = models.resnet18(weights=None)
13
+ num_ftrs = model.fc.in_features
14
+ model.fc = nn.Linear(num_ftrs, len(class_names))
15
+ model.load_state_dict(torch.load("model.pth", map_location=torch.device("cpu")))
16
+ model.eval()
17
+ return model
18
+
19
+ model = load_model()
20
+
21
+ # Preprocessing
22
+ transform = transforms.Compose([
23
+ transforms.Resize((224, 224)),
24
+ transforms.ToTensor(),
25
+ ])
26
+
27
+ # Prediction function
28
+ def classify_image(img):
29
+ img = transform(img).unsqueeze(0)
30
+ with torch.no_grad():
31
+ output = model(img)
32
+ _, pred = torch.max(output, 1)
33
+ return class_names[pred.item()]
34
+
35
+ # Gradio interface
36
+ demo = gr.Interface(fn=classify_image, inputs=gr.Image(type="pil"), outputs="text", title="Vegetable Classifier")
37
+ demo.launch()
38
+
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ torch
2
+ torchvision
3
+ gradio
4
+ Pillow
5
+
resnet_model.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8d9092b51f1fde37fa95bcd2e3a20b469bcf003999e5fe2cc46cf5433b4f9be1
3
+ size 44791988