Spaces:
Sleeping
Sleeping
more info displayed
Browse files- app.py +13 -2
- classinfo.txt +90 -0
app.py
CHANGED
@@ -12,6 +12,12 @@ def load_class_names(file_path):
|
|
12 |
with open(file_path, 'r') as f:
|
13 |
class_names = [line.strip() for line in f.readlines()]
|
14 |
return class_names
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
# Function to load the model from a .pkl file
|
16 |
def load_model(model_path, model_type='resnet'):
|
17 |
# Load the model state dictionary
|
@@ -58,13 +64,18 @@ def predict_image(image):
|
|
58 |
confidence, preds = torch.max(probabilities, 0)
|
59 |
|
60 |
class_name = class_names[preds.item()]
|
|
|
61 |
confidence_score = confidence.item() * 100
|
62 |
-
|
|
|
|
|
63 |
|
64 |
# Load trained model and class names
|
65 |
model_path = 'resnet30EpochsPretrainedNFeatureX_model.pkl'
|
66 |
class_file_path = 'classes.txt'
|
|
|
67 |
class_names = load_class_names(class_file_path)
|
|
|
68 |
num_classes = len(class_names)
|
69 |
model = load_model(model_path)
|
70 |
|
@@ -72,7 +83,7 @@ model = load_model(model_path)
|
|
72 |
iface = gr.Interface(
|
73 |
fn=predict_image,
|
74 |
inputs=gr.Image(),
|
75 |
-
outputs=gr.Label(num_top_classes=1),
|
76 |
title="Image Classification",
|
77 |
description="Upload an image to get the predicted label",
|
78 |
allow_flagging="never",
|
|
|
12 |
with open(file_path, 'r') as f:
|
13 |
class_names = [line.strip() for line in f.readlines()]
|
14 |
return class_names
|
15 |
+
# Function to load Wikipedia links from a file
|
16 |
+
def load_class_info(file_path):
|
17 |
+
with open(file_path, 'r') as f:
|
18 |
+
class_info = [line.strip() for line in f.readlines()]
|
19 |
+
return class_info
|
20 |
+
|
21 |
# Function to load the model from a .pkl file
|
22 |
def load_model(model_path, model_type='resnet'):
|
23 |
# Load the model state dictionary
|
|
|
64 |
confidence, preds = torch.max(probabilities, 0)
|
65 |
|
66 |
class_name = class_names[preds.item()]
|
67 |
+
wiki_link = class_info[preds.item()]
|
68 |
confidence_score = confidence.item() * 100
|
69 |
+
result = f"{class_name}: {confidence_score:.2f}%"
|
70 |
+
html_result = f"<h1><br><a href='{wiki_link}' target='_blank'>More Info</a></h1>"
|
71 |
+
return result, html_result
|
72 |
|
73 |
# Load trained model and class names
|
74 |
model_path = 'resnet30EpochsPretrainedNFeatureX_model.pkl'
|
75 |
class_file_path = 'classes.txt'
|
76 |
+
class_info_path = 'classinfo.txt'
|
77 |
class_names = load_class_names(class_file_path)
|
78 |
+
class_info = load_class_info(class_info_path)
|
79 |
num_classes = len(class_names)
|
80 |
model = load_model(model_path)
|
81 |
|
|
|
83 |
iface = gr.Interface(
|
84 |
fn=predict_image,
|
85 |
inputs=gr.Image(),
|
86 |
+
outputs=[gr.Label(num_top_classes=1),gr.HTML()],
|
87 |
title="Image Classification",
|
88 |
description="Upload an image to get the predicted label",
|
89 |
allow_flagging="never",
|
classinfo.txt
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
https://en.wikipedia.org/wiki/antelope
|
2 |
+
https://en.wikipedia.org/wiki/badger
|
3 |
+
https://en.wikipedia.org/wiki/bat
|
4 |
+
https://en.wikipedia.org/wiki/bear
|
5 |
+
https://en.wikipedia.org/wiki/bee
|
6 |
+
https://en.wikipedia.org/wiki/beetle
|
7 |
+
https://en.wikipedia.org/wiki/bison
|
8 |
+
https://en.wikipedia.org/wiki/boar
|
9 |
+
https://en.wikipedia.org/wiki/butterfly
|
10 |
+
https://en.wikipedia.org/wiki/cat
|
11 |
+
https://en.wikipedia.org/wiki/caterpillar
|
12 |
+
https://en.wikipedia.org/wiki/chimpanzee
|
13 |
+
https://en.wikipedia.org/wiki/cockroach
|
14 |
+
https://en.wikipedia.org/wiki/cow
|
15 |
+
https://en.wikipedia.org/wiki/coyote
|
16 |
+
https://en.wikipedia.org/wiki/crab
|
17 |
+
https://en.wikipedia.org/wiki/crow
|
18 |
+
https://en.wikipedia.org/wiki/deer
|
19 |
+
https://en.wikipedia.org/wiki/dog
|
20 |
+
https://en.wikipedia.org/wiki/dolphin
|
21 |
+
https://en.wikipedia.org/wiki/donkey
|
22 |
+
https://en.wikipedia.org/wiki/dragonfly
|
23 |
+
https://en.wikipedia.org/wiki/duck
|
24 |
+
https://en.wikipedia.org/wiki/eagle
|
25 |
+
https://en.wikipedia.org/wiki/elephant
|
26 |
+
https://en.wikipedia.org/wiki/flamingo
|
27 |
+
https://en.wikipedia.org/wiki/fly
|
28 |
+
https://en.wikipedia.org/wiki/fox
|
29 |
+
https://en.wikipedia.org/wiki/goat
|
30 |
+
https://en.wikipedia.org/wiki/goldfish
|
31 |
+
https://en.wikipedia.org/wiki/goose
|
32 |
+
https://en.wikipedia.org/wiki/gorilla
|
33 |
+
https://en.wikipedia.org/wiki/grasshopper
|
34 |
+
https://en.wikipedia.org/wiki/hamster
|
35 |
+
https://en.wikipedia.org/wiki/hare
|
36 |
+
https://en.wikipedia.org/wiki/hedgehog
|
37 |
+
https://en.wikipedia.org/wiki/hippopotamus
|
38 |
+
https://en.wikipedia.org/wiki/hornbill
|
39 |
+
https://en.wikipedia.org/wiki/horse
|
40 |
+
https://en.wikipedia.org/wiki/hummingbird
|
41 |
+
https://en.wikipedia.org/wiki/hyena
|
42 |
+
https://en.wikipedia.org/wiki/jellyfish
|
43 |
+
https://en.wikipedia.org/wiki/kangaroo
|
44 |
+
https://en.wikipedia.org/wiki/koala
|
45 |
+
https://en.wikipedia.org/wiki/ladybugs
|
46 |
+
https://en.wikipedia.org/wiki/leopard
|
47 |
+
https://en.wikipedia.org/wiki/lion
|
48 |
+
https://en.wikipedia.org/wiki/lizard
|
49 |
+
https://en.wikipedia.org/wiki/lobster
|
50 |
+
https://en.wikipedia.org/wiki/mosquito
|
51 |
+
https://en.wikipedia.org/wiki/moth
|
52 |
+
https://en.wikipedia.org/wiki/mouse
|
53 |
+
https://en.wikipedia.org/wiki/octopus
|
54 |
+
https://en.wikipedia.org/wiki/okapi
|
55 |
+
https://en.wikipedia.org/wiki/orangutan
|
56 |
+
https://en.wikipedia.org/wiki/otter
|
57 |
+
https://en.wikipedia.org/wiki/owl
|
58 |
+
https://en.wikipedia.org/wiki/ox
|
59 |
+
https://en.wikipedia.org/wiki/oyster
|
60 |
+
https://en.wikipedia.org/wiki/panda
|
61 |
+
https://en.wikipedia.org/wiki/parrot
|
62 |
+
https://en.wikipedia.org/wiki/pelecaniformes
|
63 |
+
https://en.wikipedia.org/wiki/penguin
|
64 |
+
https://en.wikipedia.org/wiki/pig
|
65 |
+
https://en.wikipedia.org/wiki/pigeon
|
66 |
+
https://en.wikipedia.org/wiki/porcupine
|
67 |
+
https://en.wikipedia.org/wiki/possum
|
68 |
+
https://en.wikipedia.org/wiki/raccoon
|
69 |
+
https://en.wikipedia.org/wiki/rat
|
70 |
+
https://en.wikipedia.org/wiki/reindeer
|
71 |
+
https://en.wikipedia.org/wiki/rhinoceros
|
72 |
+
https://en.wikipedia.org/wiki/sandpiper
|
73 |
+
https://en.wikipedia.org/wiki/seahorse
|
74 |
+
https://en.wikipedia.org/wiki/Pinniped
|
75 |
+
https://en.wikipedia.org/wiki/shark
|
76 |
+
https://en.wikipedia.org/wiki/sheep
|
77 |
+
https://en.wikipedia.org/wiki/snake
|
78 |
+
https://en.wikipedia.org/wiki/sparrow
|
79 |
+
https://en.wikipedia.org/wiki/squid
|
80 |
+
https://en.wikipedia.org/wiki/squirrel
|
81 |
+
https://en.wikipedia.org/wiki/starfish
|
82 |
+
https://en.wikipedia.org/wiki/swan
|
83 |
+
https://en.wikipedia.org/wiki/tiger
|
84 |
+
https://en.wikipedia.org/wiki/turkey
|
85 |
+
https://en.wikipedia.org/wiki/turtle
|
86 |
+
https://en.wikipedia.org/wiki/whale
|
87 |
+
https://en.wikipedia.org/wiki/wolf
|
88 |
+
https://en.wikipedia.org/wiki/wombat
|
89 |
+
https://en.wikipedia.org/wiki/woodpecker
|
90 |
+
https://en.wikipedia.org/wiki/zebra
|