robocan commited on
Commit
955fc23
·
verified ·
1 Parent(s): 7cddc5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -9
app.py CHANGED
@@ -1,18 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
- from transformers import pipeline
3
 
4
- pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  def predict(input_img):
7
- predictions = pipeline(input_img)
8
- return input_img, {p["label"]: p["score"] for p in predictions}
 
 
 
9
 
10
  gradio_app = gr.Interface(
11
- predict,
12
- inputs=gr.Image(label="Select hot dog candidate", sources=['upload', 'webcam'], type="pil"),
13
- outputs=[gr.Image(label="Processed Image"), gr.Label(label="Result", num_top_classes=2)],
14
- title="Hot Dog? Or Not?",
15
  )
16
 
17
  if __name__ == "__main__":
18
- gradio_app.launch()
 
1
+ import torch
2
+ from torch.utils.data import Dataset, DataLoader
3
+ import pandas as pd
4
+ import numpy as np
5
+ import io
6
+ import joblib
7
+ import requests
8
+ from tqdm import tqdm
9
+ from PIL import Image
10
+ from torchvision import transforms
11
+ from sklearn.preprocessing import LabelEncoder
12
+ from sklearn.model_selection import train_test_split
13
+ from torchvision import models
14
  import gradio as gr
 
15
 
16
+ device = 'cpu'
17
+ le = LabelEncoder()
18
+ le = joblib.load("/kaggle/working/SVD/le.gz")
19
+
20
+ class ModelPre(torch.nn.Module):
21
+ def __init__(self):
22
+ super().__init__()
23
+ self.embedding = torch.nn.Sequential(
24
+ *list(models.convnext_small(weights=models.ConvNeXt_Small_Weights.IMAGENET1K_V1).children())[:-1],
25
+ torch.nn.Flatten(),
26
+ torch.nn.Linear(in_features=768, out_features=512),
27
+ torch.nn.ReLU(),
28
+ torch.nn.Linear(in_features=512, out_features=len(le.classes_) + 1),
29
+ )
30
+
31
+ def forward(self, data):
32
+ return self.embedding(data)
33
+
34
+ model = torch.load("/SVD/GeoG.pth", map_location=torch.device(device))
35
+
36
+ modelm = ModelPre()
37
+ modelm.load_state_dict(model['model'])
38
+
39
+ import warnings
40
+ warnings.filterwarnings("ignore", category=RuntimeWarning, module="multiprocessing.popen_fork")
41
+
42
+ cmp = transforms.Compose([
43
+ transforms.ToTensor(),
44
+ transforms.Resize(size=(224, 224), antialias=True),
45
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
46
+ ])
47
 
48
  def predict(input_img):
49
+ with torch.inference_mode():
50
+ img = cmp(input_img).unsqueeze(0)
51
+ res = modelm(img.to(device))
52
+ prediction = le.inverse_transform(torch.argmax(res.cpu()).unsqueeze(0).numpy())[0]
53
+ return prediction
54
 
55
  gradio_app = gr.Interface(
56
+ fn=predict,
57
+ inputs=gr.Image(label="Upload an Image", type="pil"),
58
+ outputs=gr.Label(label="Location"),
59
+ title="Predict the Location of this Image"
60
  )
61
 
62
  if __name__ == "__main__":
63
+ gradio_app.launch()