Redhotchilipoppy
commited on
Commit
•
690fe91
1
Parent(s):
02be56f
Big model update
Browse files
app.py
CHANGED
@@ -1,15 +1,38 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
image = gr.Image(type='pil')
|
4 |
-
label = gr.Label()
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
def classify_image(img):
|
10 |
-
#
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
|
|
|
|
|
|
|
14 |
iface = gr.Interface(fn=classify_image, inputs=image, outputs=label)
|
15 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import pathlib
|
4 |
+
from fastbook import *
|
5 |
+
from fastai.vision.widgets import *
|
6 |
+
from fastai.vision.all import *
|
7 |
|
|
|
|
|
8 |
|
9 |
+
# Load the model. "PosixPath" is something with windows/linux, I dont know really.
|
10 |
+
temp = pathlib.PosixPath
|
11 |
+
pathlib.PosixPath = pathlib.WindowsPath
|
12 |
+
learn = load_learner('model.pkl') # Load the model itself
|
13 |
+
pathlib.PosixPath = temp
|
14 |
+
categories = learn.dls.vocab # Get the list of labels from the model
|
15 |
+
|
16 |
+
# Load the list of trash-sorting, use the items as index.
|
17 |
+
df_sort = pd.read_csv('Lista.csv',sep =";").set_index('Avfall')
|
18 |
|
19 |
def classify_image(img):
|
20 |
+
# Make the prediction
|
21 |
+
trash,idx,probs = learn.predict(PILImage.create(img)) # Make prediction
|
22 |
+
df = pd.DataFrame() # Create dataframe
|
23 |
+
df['categories'] = categories # Add categories to dataframe
|
24 |
+
df['probabilities'] = probs.numpy() # Add probabilities to dataframe
|
25 |
+
sorted_df = df.sort_values(by=['probabilities'], ascending=False).head() # Sort by probability, highest first, take the top 5
|
26 |
+
predictions = dict(zip(sorted_df['categories'].tolist(),map(float,sorted_df['probabilities'].tolist()))) # Now convert to a dictionary that we return later
|
27 |
+
|
28 |
+
|
29 |
+
# Create sorting statement
|
30 |
+
sort_text = "Sorteras som " + df_sort.loc[trash].tolist()[0]
|
31 |
+
return "Det där är...", predictions, sort_text # Return the dictionary
|
32 |
|
33 |
|
34 |
+
image = gr.Image(type='pil')
|
35 |
+
label = ["text",gr.Label(),"text"]
|
36 |
+
|
37 |
iface = gr.Interface(fn=classify_image, inputs=image, outputs=label)
|
38 |
iface.launch()
|