File size: 1,159 Bytes
e4fb88b
3e09757
bf8ef1c
 
 
 
3e09757
bf8ef1c
 
 
 
 
 
79a67e1
bf8ef1c
 
 
 
 
 
 
 
 
70877c2
6ee700a
70877c2
 
e4fb88b
70877c2
 
e4fb88b
 
1c0c20f
70877c2
 
bf8ef1c
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
from fastai.vision.all import *
import gradio as gr
import torch
import sys
import fastai
from pathlib import Path

print(f"Python version: {sys.version}")
print(f"Torch version: {torch.__version__}")
print(f"Fastai version: {fastai.__version__}")

try:
    # Load the model with CPU as default device
    learn = load_learner('model_champi.pkl', cpu=True)
    print("Model loaded successfully!")
except TypeError as e:
    print("Error: Model version incompatibility detected.")
    print("This usually happens when the model was saved with a different PyTorch version.")
    print("Please retrain the model using the current environment versions.")
    raise e
except Exception as e:
    print(f"Unexpected error loading model: {str(e)}")
    raise e

categories = ('Amanita', 'Boletus', 'Morchella', 'Truffle')

def classify_image(img):
    pred,idx,probs = learn.predict(img)
    return dict(zip(categories, map(float,probs)))

image = gr.Image(height=192, width=192)
label = gr.Label()
examples = ['amanita.jpg', 'truffle.jpg', "boletus.jpg"]

intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
intf.launch(share=True)