File size: 2,206 Bytes
f3fe718
 
 
 
 
 
6589854
 
f3fe718
 
 
 
50807d3
f3fe718
 
 
97fda69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3fe718
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d3058e
 
 
f3fe718
 
 
 
 
 
 
 
 
 
 
1251656
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import gradio as gr
import torch
from transformers import ViTForImageClassification, ViTFeatureExtractor
from PIL import Image

# Load model and feature extractor
model = ViTForImageClassification.from_pretrained('shahmi0519/fypvit', num_labels=30, ignore_mismatched_sizes=True)
feature_extractor = ViTFeatureExtractor.from_pretrained('shahmi0519/fypvit')

# Move to GPU if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
model.eval()

# Class labels (modify according to your model)
class_labels = [
    "Bellpepper_fresh",
    "Bellpepper_intermediate_fresh",
    "Bellpepper_rotten",
    "Carrot_fresh",
    "Carrot_intermediate_fresh",
    "Carrot_rotten",
    "Cucumber_fresh",
    "Cucumber_intermediate_fresh",
    "Cucumber_rotten",
    "Potato_fresh",
    "Potato_intermediate_fresh",
    "Potato_rotten",
    "Tomato_fresh",
    "Tomato_intermediate_fresh",
    "Tomato_rotten",
    "ripe_apple",
    "ripe_banana",
    "ripe_mango",
    "ripe_oranges",
    "ripe_strawberry",
    "rotten_apple",
    "rotten_banana",
    "rotten_mango",
    "rotten_oranges",
    "rotten_strawberry",
    "unripe_apple",
    "unripe_banana",
    "unripe_mango",
    "unripe_oranges",
    "unripe_strawberry"
]

def predict_freshness(image):
    # Preprocess image
    inputs = feature_extractor(images=image, return_tensors="pt").to(device)
    
    # Predict
    model.eval()
    with torch.no_grad():
        outputs = model(**inputs)
        logits = outputs.logits
        predicted_class_idx = logits.argmax(-1).item()
    
    # Get label
    try:
        label = class_labels[predicted_class_idx]
    except IndexError:
        label = f"Class {predicted_class_idx}"
    
    return label

# Create Gradio interface
title = "Freshness Detector"
description = "Upload an image of fruit/vegetable to detect its freshness state"
examples = [
    ["apple.jpeg"],
    ["banana.jpeg"],
    ["tomato.jpeg"]
]

iface = gr.Interface(
    fn=predict_freshness,
    inputs=gr.Image(type="pil", label="Upload Image"),
    outputs=gr.Label(label="Freshness State"),
    title=title,
    description=description,
    examples=examples
)

iface.launch(share=True)