smdbs commited on
Commit
a87e981
·
1 Parent(s): a265d1d

fix: update app.py and test.py

Browse files

Update app.py's resnet50() function to make second argument optional. Update test.py with new test cases using local model.

Files changed (2) hide show
  1. app.py +1 -1
  2. test.py +21 -9
app.py CHANGED
@@ -23,7 +23,7 @@ def load_model():
23
  model = ResNetForImageClassification.from_pretrained("microsoft/resnet-50")
24
  print("Model loaded.")
25
 
26
- def resnet50(image: Union[np.ndarray, Image.Image, str, None], is_local: bool) -> dict[str, float]:
27
  if image is None:
28
  return {}
29
 
 
23
  model = ResNetForImageClassification.from_pretrained("microsoft/resnet-50")
24
  print("Model loaded.")
25
 
26
+ def resnet50(image: Union[np.ndarray, Image.Image, str, None], is_local: bool = False) -> dict[str, float]:
27
  if image is None:
28
  return {}
29
 
test.py CHANGED
@@ -12,18 +12,30 @@ TOKEN_MSG = "Set HF_TOKEN to your Hugging Face token for using InferenceClient"
12
  SKIP_MSG = "Skipping test because HF_TOKEN is not set"
13
 
14
 
15
-
16
  class TestGeneralSetup(unittest.TestCase):
17
  def test_setup(self):
18
  self.assertTrue(HF_TOKEN != "YOUR_HF_TOKEN", TOKEN_MSG)
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  class TestLocalImageArray(unittest.TestCase):
21
  @unittest.skipIf(HF_TOKEN == "YOUR_HF_TOKEN", SKIP_MSG)
22
  def test_array_1(self):
23
  p = path.join(path.dirname(__file__), "sample", "pomdog.jpg")
24
  image = Image.open(p)
25
  image = np.array(image)
26
- prediction = resnet50(image)
27
  self.assertTrue(len(prediction))
28
 
29
 
@@ -32,7 +44,7 @@ class TestLocalImageArray(unittest.TestCase):
32
  p = path.join(path.dirname(__file__), "sample", "persiancat.jpg")
33
  image = Image.open(p)
34
  image = np.array(image)
35
- prediction = resnet50(image)
36
  self.assertTrue(len(prediction))
37
 
38
  class TestLocalImagePIL(unittest.TestCase):
@@ -40,40 +52,40 @@ class TestLocalImagePIL(unittest.TestCase):
40
  def test_pil_1(self):
41
  p = path.join(path.dirname(__file__), "sample", "pomdog.jpg")
42
  image = Image.open(p)
43
- prediction = resnet50(image)
44
  self.assertTrue(len(prediction))
45
 
46
  @unittest.skipIf(HF_TOKEN == "YOUR_HF_TOKEN", SKIP_MSG)
47
  def test_pil_2(self):
48
  p = path.join(path.dirname(__file__), "sample", "persiancat.jpg")
49
  image = Image.open(p)
50
- prediction = resnet50(image)
51
  self.assertTrue(len(prediction))
52
 
53
  class TestLocalImageStr(unittest.TestCase):
54
  @unittest.skipIf(HF_TOKEN == "YOUR_HF_TOKEN", SKIP_MSG)
55
  def test_str_1(self):
56
  p = path.join(path.dirname(__file__), "sample", "pomdog.jpg")
57
- prediction = resnet50(p)
58
  self.assertTrue(len(prediction))
59
 
60
  @unittest.skipIf(HF_TOKEN == "YOUR_HF_TOKEN", SKIP_MSG)
61
  def test_str_2(self):
62
  p = path.join(path.dirname(__file__), "sample", "persiancat.jpg")
63
- prediction = resnet50(p)
64
  self.assertTrue(len(prediction))
65
 
66
  class TestRemoteImageUrl(unittest.TestCase):
67
  @unittest.skipIf(HF_TOKEN == "YOUR_HF_TOKEN", SKIP_MSG)
68
  def test_url_1(self):
69
  url = "https://i.ytimg.com/vi/AQSTQ4VUPH4/maxresdefault.jpg"
70
- prediction = resnet50(url)
71
  self.assertTrue(len(prediction))
72
 
73
  @unittest.skipIf(HF_TOKEN == "YOUR_HF_TOKEN", SKIP_MSG)
74
  def test_url_2(self):
75
  url = "https://img.thrfun.com/img/200/610/breed_information_persian_x1.jpg"
76
- prediction = resnet50(url)
77
  self.assertTrue(len(prediction))
78
 
79
  if __name__ == "__main__":
 
12
  SKIP_MSG = "Skipping test because HF_TOKEN is not set"
13
 
14
 
 
15
  class TestGeneralSetup(unittest.TestCase):
16
  def test_setup(self):
17
  self.assertTrue(HF_TOKEN != "YOUR_HF_TOKEN", TOKEN_MSG)
18
 
19
+ class TestLocalModelImage(unittest.TestCase):
20
+ def test_local_model_image1(self):
21
+ p = path.join(path.dirname(__file__), "sample", "pomdog.jpg")
22
+ image = Image.open(p)
23
+ prediction = resnet50(image, is_local=True)
24
+ self.assertTrue(len(prediction))
25
+
26
+ def test_local_model_image2(self):
27
+ p = path.join(path.dirname(__file__), "sample", "persiancat.jpg")
28
+ image = Image.open(p)
29
+ prediction = resnet50(image, is_local=True)
30
+ self.assertTrue(len(prediction))
31
+
32
  class TestLocalImageArray(unittest.TestCase):
33
  @unittest.skipIf(HF_TOKEN == "YOUR_HF_TOKEN", SKIP_MSG)
34
  def test_array_1(self):
35
  p = path.join(path.dirname(__file__), "sample", "pomdog.jpg")
36
  image = Image.open(p)
37
  image = np.array(image)
38
+ prediction = resnet50(image, is_local=False)
39
  self.assertTrue(len(prediction))
40
 
41
 
 
44
  p = path.join(path.dirname(__file__), "sample", "persiancat.jpg")
45
  image = Image.open(p)
46
  image = np.array(image)
47
+ prediction = resnet50(image, is_local=False)
48
  self.assertTrue(len(prediction))
49
 
50
  class TestLocalImagePIL(unittest.TestCase):
 
52
  def test_pil_1(self):
53
  p = path.join(path.dirname(__file__), "sample", "pomdog.jpg")
54
  image = Image.open(p)
55
+ prediction = resnet50(image, is_local=False)
56
  self.assertTrue(len(prediction))
57
 
58
  @unittest.skipIf(HF_TOKEN == "YOUR_HF_TOKEN", SKIP_MSG)
59
  def test_pil_2(self):
60
  p = path.join(path.dirname(__file__), "sample", "persiancat.jpg")
61
  image = Image.open(p)
62
+ prediction = resnet50(image, is_local=False)
63
  self.assertTrue(len(prediction))
64
 
65
  class TestLocalImageStr(unittest.TestCase):
66
  @unittest.skipIf(HF_TOKEN == "YOUR_HF_TOKEN", SKIP_MSG)
67
  def test_str_1(self):
68
  p = path.join(path.dirname(__file__), "sample", "pomdog.jpg")
69
+ prediction = resnet50(p, is_local=False)
70
  self.assertTrue(len(prediction))
71
 
72
  @unittest.skipIf(HF_TOKEN == "YOUR_HF_TOKEN", SKIP_MSG)
73
  def test_str_2(self):
74
  p = path.join(path.dirname(__file__), "sample", "persiancat.jpg")
75
+ prediction = resnet50(p, is_local=False)
76
  self.assertTrue(len(prediction))
77
 
78
  class TestRemoteImageUrl(unittest.TestCase):
79
  @unittest.skipIf(HF_TOKEN == "YOUR_HF_TOKEN", SKIP_MSG)
80
  def test_url_1(self):
81
  url = "https://i.ytimg.com/vi/AQSTQ4VUPH4/maxresdefault.jpg"
82
+ prediction = resnet50(url, is_local=False)
83
  self.assertTrue(len(prediction))
84
 
85
  @unittest.skipIf(HF_TOKEN == "YOUR_HF_TOKEN", SKIP_MSG)
86
  def test_url_2(self):
87
  url = "https://img.thrfun.com/img/200/610/breed_information_persian_x1.jpg"
88
+ prediction = resnet50(url, is_local=False)
89
  self.assertTrue(len(prediction))
90
 
91
  if __name__ == "__main__":