Spaces:
Sleeping
Sleeping
Shreyavalte
commited on
Commit
•
c6181b0
1
Parent(s):
d3e1f06
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### 1. Imports and class names setup ###
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
import torch
|
5 |
+
|
6 |
+
from model import create_leaderss_model
|
7 |
+
from timeit import default_timer as timer
|
8 |
+
from typing import Tuple, Dict
|
9 |
+
|
10 |
+
# Setup class names
|
11 |
+
class_names = ['Barack Obama', 'Narendra Modi', 'Vladimir Vladimirovich Putin']
|
12 |
+
|
13 |
+
### 2. Model and transforms preparation ###
|
14 |
+
|
15 |
+
|
16 |
+
Leaders, Leaders_transforms = create_Leaders_model(
|
17 |
+
num_classes=3, # len(class_names) would also work
|
18 |
+
)
|
19 |
+
|
20 |
+
# Load saved weights
|
21 |
+
Leaders.load_state_dict(
|
22 |
+
torch.load(
|
23 |
+
f="Leaders_model_save.pth",
|
24 |
+
map_location=torch.device("cpu"), # load to CPU
|
25 |
+
)
|
26 |
+
)
|
27 |
+
|
28 |
+
### 3. Predict function ###
|
29 |
+
|
30 |
+
# Create predict function
|
31 |
+
def predict(img) -> Tuple[Dict, float]:
|
32 |
+
"""Transforms and performs a prediction on img and returns prediction and time taken.
|
33 |
+
"""
|
34 |
+
# Start the timer
|
35 |
+
start_time = timer()
|
36 |
+
|
37 |
+
# Transform the target image and add a batch dimension
|
38 |
+
img = Leaders_transforms(img).unsqueeze(0)
|
39 |
+
|
40 |
+
# Put model into evaluation mode and turn on inference mode
|
41 |
+
Leaders.eval()
|
42 |
+
with torch.inference_mode():
|
43 |
+
# Pass the transformed image through the model and turn the prediction logits into prediction probabilities
|
44 |
+
pred_probs = torch.softmax(gadget(img), dim=1)
|
45 |
+
|
46 |
+
# Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
|
47 |
+
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
48 |
+
|
49 |
+
# Calculate the prediction time
|
50 |
+
pred_time = round(timer() - start_time, 5)
|
51 |
+
|
52 |
+
# Return the prediction dictionary and prediction time
|
53 |
+
return pred_labels_and_probs, pred_time
|
54 |
+
|
55 |
+
### 4. Gradio app ###
|
56 |
+
|
57 |
+
# Create title, description and article strings
|
58 |
+
title = "Leaders Classifier "
|
59 |
+
description = "An computer vision model to classify images of Leaders as Barack Obama,Narendra Modi,Vladimir Vladimirovich Putin"
|
60 |
+
article = "Created by Shreya Valte"
|
61 |
+
|
62 |
+
|
63 |
+
|
64 |
+
# Create the Gradio demo
|
65 |
+
demo = gr.Interface(fn=predict, # mapping function from input to output
|
66 |
+
inputs=gr.Image(type="pil"), # what are the inputs?
|
67 |
+
outputs=[gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
|
68 |
+
gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
|
69 |
+
# Create examples list from "examples/" directory
|
70 |
+
examples=["Image_1.jpg","Image_108.jpg"],
|
71 |
+
title=title,
|
72 |
+
description=description,
|
73 |
+
article=article)
|
74 |
+
|
75 |
+
# Launch the demo!
|
76 |
+
demo.launch()
|