GerryRaz commited on
Commit
c9bf638
·
verified ·
1 Parent(s): 05e31b9

Fix the text in the desc

Browse files
Files changed (1) hide show
  1. app.py +84 -84
app.py CHANGED
@@ -1,85 +1,85 @@
1
- import gradio as gr
2
- import os
3
- import torch
4
-
5
- #from model import create_densenet_model
6
- import model
7
- from timeit import default_timer as timer
8
-
9
-
10
- import os
11
- from pathlib import Path
12
-
13
-
14
- from pathlib import Path
15
-
16
- # 1. Get the path of the current folder where app.py is located
17
- current_dir = Path(__file__).parent
18
-
19
- # 2. Load class names from the text file
20
- # This fixes the "File Not Found" error by using the full path
21
- with open(current_dir / "class_names.txt", "r") as f:
22
- class_names = [line.strip() for line in f.readlines()]
23
-
24
- # Verify the number of classes loaded
25
- print(f"Loaded {len(class_names)} classes.")
26
-
27
- model_1, transforms = model.create_model(num_classes=120)
28
-
29
-
30
- state_dict = torch.load(
31
- f="30_epoch_model_efficientv2_2_93%_acc_dog_bread_classifier.pth",
32
- weights_only=False,
33
- map_location="cpu"
34
- )
35
-
36
- model_1.load_state_dict(state_dict)
37
-
38
-
39
- def predict_img(img):
40
-
41
- start_time = timer()
42
-
43
- img = transforms(img).unsqueeze(0)
44
-
45
- model_1.eval()
46
-
47
- with torch.inference_mode():
48
-
49
- # 1. Get the probabilities
50
- pred_probs = torch.softmax(model_1(img), dim=1)
51
-
52
- # 2. Create a dictionary of ALL classes and their probabilities
53
- all_pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
54
-
55
- # 3. Sort them by value (probability) in descending order and take the first 5
56
- pred_labels_and_probs = dict(sorted(all_pred_labels_and_probs.items(),
57
- key=lambda item: item[1],
58
- reverse=True)[:5])
59
-
60
- #pred_time = round(timer() - start_time(),5)
61
- pred_time = round(timer() - start_time, 5)
62
-
63
- return pred_labels_and_probs, pred_time
64
-
65
- title = "Covid Lung Classified"
66
- description = "Dog Breed Classifier"
67
- article = "Created at 2026"
68
-
69
- example_list = [["examples/" + example] for example in os.listdir("examples")]
70
-
71
- demo = gr.Interface(
72
- fn=predict_img,
73
- inputs=gr.Image(type="pil"),
74
- outputs=[
75
- gr.Label(num_top_classes=5,label="Predictions"),
76
- gr.Number(label="Prediction Time")
77
- ],
78
- examples = example_list,
79
- title = title,
80
- description = description,
81
- article = article
82
- )
83
-
84
-
85
  demo.launch(debug=True)
 
1
+ import gradio as gr
2
+ import os
3
+ import torch
4
+
5
+ #from model import create_densenet_model
6
+ import model
7
+ from timeit import default_timer as timer
8
+
9
+
10
+ import os
11
+ from pathlib import Path
12
+
13
+
14
+ from pathlib import Path
15
+
16
+ # 1. Get the path of the current folder where app.py is located
17
+ current_dir = Path(__file__).parent
18
+
19
+ # 2. Load class names from the text file
20
+ # This fixes the "File Not Found" error by using the full path
21
+ with open(current_dir / "class_names.txt", "r") as f:
22
+ class_names = [line.strip() for line in f.readlines()]
23
+
24
+ # Verify the number of classes loaded
25
+ print(f"Loaded {len(class_names)} classes.")
26
+
27
+ model_1, transforms = model.create_model(num_classes=120)
28
+
29
+
30
+ state_dict = torch.load(
31
+ f="30_epoch_model_efficientv2_2_93%_acc_dog_bread_classifier.pth",
32
+ weights_only=False,
33
+ map_location="cpu"
34
+ )
35
+
36
+ model_1.load_state_dict(state_dict)
37
+
38
+
39
+ def predict_img(img):
40
+
41
+ start_time = timer()
42
+
43
+ img = transforms(img).unsqueeze(0)
44
+
45
+ model_1.eval()
46
+
47
+ with torch.inference_mode():
48
+
49
+ # 1. Get the probabilities
50
+ pred_probs = torch.softmax(model_1(img), dim=1)
51
+
52
+ # 2. Create a dictionary of ALL classes and their probabilities
53
+ all_pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
54
+
55
+ # 3. Sort them by value (probability) in descending order and take the first 5
56
+ pred_labels_and_probs = dict(sorted(all_pred_labels_and_probs.items(),
57
+ key=lambda item: item[1],
58
+ reverse=True)[:5])
59
+
60
+ #pred_time = round(timer() - start_time(),5)
61
+ pred_time = round(timer() - start_time, 5)
62
+
63
+ return pred_labels_and_probs, pred_time
64
+
65
+ title = "Dog Breed Classifier"
66
+ description = "Upload a photo of your dog here to identify its breed! Our AI analyzes 120 different types to give you the top 5 most likely matches in seconds. Simply drag and drop your image, click submit, and see the results. Fast, fun, and accurate dog breed classification at your fingertips."
67
+ article = "Created at Mauaque Ressettlement Center Gozales Compound"
68
+
69
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
70
+
71
+ demo = gr.Interface(
72
+ fn=predict_img,
73
+ inputs=gr.Image(type="pil"),
74
+ outputs=[
75
+ gr.Label(num_top_classes=5,label="Predictions"),
76
+ gr.Number(label="Prediction Time")
77
+ ],
78
+ examples = example_list,
79
+ title = title,
80
+ description = description,
81
+ article = article
82
+ )
83
+
84
+
85
  demo.launch(debug=True)