Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,13 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
import pandas as pd
|
| 5 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Retrieve the token from the environment variables
|
| 8 |
token = os.environ.get("token")
|
|
@@ -15,21 +20,6 @@ repo = Repository(
|
|
| 15 |
)
|
| 16 |
repo.git_pull()
|
| 17 |
|
| 18 |
-
import torch
|
| 19 |
-
from torch.utils.data import Dataset, DataLoader
|
| 20 |
-
import pandas as pd
|
| 21 |
-
import numpy as np
|
| 22 |
-
import io
|
| 23 |
-
import joblib
|
| 24 |
-
import requests
|
| 25 |
-
from tqdm import tqdm
|
| 26 |
-
from PIL import Image
|
| 27 |
-
from torchvision import transforms
|
| 28 |
-
from sklearn.preprocessing import LabelEncoder
|
| 29 |
-
from sklearn.model_selection import train_test_split
|
| 30 |
-
from torchvision import models
|
| 31 |
-
import gradio as gr
|
| 32 |
-
|
| 33 |
device = 'cpu'
|
| 34 |
le = LabelEncoder()
|
| 35 |
le = joblib.load("SVD/le.gz")
|
|
@@ -45,7 +35,6 @@ class ModelPre(torch.nn.Module):
|
|
| 45 |
torch.nn.ReLU(),
|
| 46 |
torch.nn.Linear(in_features=512,out_features=len_classes),
|
| 47 |
)
|
| 48 |
-
# Freeze all layers
|
| 49 |
|
| 50 |
def forward(self, data):
|
| 51 |
return self.embedding(data)
|
|
@@ -55,9 +44,6 @@ model = torch.load("SVD/GeoG.pth", map_location=torch.device(device))
|
|
| 55 |
modelm = ModelPre()
|
| 56 |
modelm.load_state_dict(model['model'])
|
| 57 |
|
| 58 |
-
import warnings
|
| 59 |
-
warnings.filterwarnings("ignore", category=RuntimeWarning, module="multiprocessing.popen_fork")
|
| 60 |
-
|
| 61 |
cmp = transforms.Compose([
|
| 62 |
transforms.ToTensor(),
|
| 63 |
transforms.Resize(size=(224, 224), antialias=True),
|
|
@@ -76,29 +62,17 @@ def predict(input_img):
|
|
| 76 |
results = {top_10_predictions[i]: float(top_10_probabilities[i]) for i in range(10)}
|
| 77 |
return results
|
| 78 |
|
| 79 |
-
def
|
| 80 |
-
|
| 81 |
-
max_prob = data["Probability"].max()
|
| 82 |
-
return gr.BarPlot(
|
| 83 |
-
data,
|
| 84 |
-
x="Location",
|
| 85 |
-
y="Probability",
|
| 86 |
-
title="Top 10 Predictions with Probabilities",
|
| 87 |
-
tooltip=["Location", "Probability"],
|
| 88 |
-
y_lim=[0, max_prob],
|
| 89 |
-
width=800, # Set the width of the plot
|
| 90 |
-
height=600 # Set the height of the plot
|
| 91 |
-
)
|
| 92 |
|
| 93 |
def predict_and_plot(input_img):
|
| 94 |
predictions = predict(input_img)
|
| 95 |
-
return
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
gradio_app = gr.Interface(
|
| 99 |
fn=predict_and_plot,
|
| 100 |
-
inputs=
|
| 101 |
-
outputs=
|
| 102 |
title="Predict the Location of this Image"
|
| 103 |
)
|
| 104 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
import torch
|
| 3 |
+
from torch.utils.data import Dataset, DataLoader
|
| 4 |
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
import joblib
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from torchvision import transforms
|
| 9 |
+
from sklearn.preprocessing import LabelEncoder
|
| 10 |
+
from gradio import Interface, Image, Label
|
| 11 |
|
| 12 |
# Retrieve the token from the environment variables
|
| 13 |
token = os.environ.get("token")
|
|
|
|
| 20 |
)
|
| 21 |
repo.git_pull()
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
device = 'cpu'
|
| 24 |
le = LabelEncoder()
|
| 25 |
le = joblib.load("SVD/le.gz")
|
|
|
|
| 35 |
torch.nn.ReLU(),
|
| 36 |
torch.nn.Linear(in_features=512,out_features=len_classes),
|
| 37 |
)
|
|
|
|
| 38 |
|
| 39 |
def forward(self, data):
|
| 40 |
return self.embedding(data)
|
|
|
|
| 44 |
modelm = ModelPre()
|
| 45 |
modelm.load_state_dict(model['model'])
|
| 46 |
|
|
|
|
|
|
|
|
|
|
| 47 |
cmp = transforms.Compose([
|
| 48 |
transforms.ToTensor(),
|
| 49 |
transforms.Resize(size=(224, 224), antialias=True),
|
|
|
|
| 62 |
results = {top_10_predictions[i]: float(top_10_probabilities[i]) for i in range(10)}
|
| 63 |
return results
|
| 64 |
|
| 65 |
+
def create_label_output(predictions):
|
| 66 |
+
return predictions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
def predict_and_plot(input_img):
|
| 69 |
predictions = predict(input_img)
|
| 70 |
+
return create_label_output(predictions)
|
| 71 |
+
|
| 72 |
+
gradio_app = Interface(
|
|
|
|
| 73 |
fn=predict_and_plot,
|
| 74 |
+
inputs=Image(label="Upload an Image", type="pil"),
|
| 75 |
+
outputs=Label(num_top_classes=10),
|
| 76 |
title="Predict the Location of this Image"
|
| 77 |
)
|
| 78 |
|