Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from huggingface_hub import from_pretrained_fastai
|
4 |
+
from pathlib import Path
|
5 |
+
|
6 |
+
examples = ["./examples/image_1.png",
|
7 |
+
"./examples/image_2.png",
|
8 |
+
"./examples/image_3.png",
|
9 |
+
"./examples/image_4.png",
|
10 |
+
"./examples/image_5.png"]
|
11 |
+
|
12 |
+
repo_id = "hugginglearners/rice_image_classification"
|
13 |
+
path = Path("./")
|
14 |
+
|
15 |
+
def get_y(r):
|
16 |
+
return r["label"]
|
17 |
+
|
18 |
+
def get_x(r):
|
19 |
+
return path/r["fname"]
|
20 |
+
|
21 |
+
learner = from_pretrained_fastai(repo_id)
|
22 |
+
|
23 |
+
def inference(image):
|
24 |
+
label_predict,_,probs = learner.predict(image)
|
25 |
+
return f"This rice image is {label_predict} with {100*probs[torch.argmax(probs)].item():.2f}% probability"
|
26 |
+
|
27 |
+
gr.Interface(
|
28 |
+
fn=inference,
|
29 |
+
title="Rice image classification",
|
30 |
+
description = "Predict which type of rice belong to Arborio, Basmati, Ipsala, Jasmine, Karacadag",
|
31 |
+
inputs="image",
|
32 |
+
examples=examples,
|
33 |
+
outputs=gr.Textbox(label='Prediction'),
|
34 |
+
cache_examples=False,
|
35 |
+
article = "Author: <a href=\"https://www.linkedin.com/in/vumichien/\">Vu Minh Chien</a>",
|
36 |
+
).launch(debug=True, enable_queue=True)
|