smdbs commited on
Commit
3e1b1c6
·
1 Parent(s): 8c56c35

feat: add sample images and test.py

Browse files

Added two sample images for unittesting and a test.py file for testing the HF_TOKEN environment variable and the image inference process.

Files changed (4) hide show
  1. .gitignore +1 -1
  2. sample/persiancat.jpg +0 -0
  3. sample/pomdog.jpg +0 -0
  4. test.py +58 -0
.gitignore CHANGED
@@ -2,4 +2,4 @@ __pycache__
2
  *.pyc
3
  *venv*
4
  flagged
5
-
 
2
  *.pyc
3
  *venv*
4
  flagged
5
+ .pytest_cache
sample/persiancat.jpg ADDED
sample/pomdog.jpg ADDED
test.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+ from huggingface_hub import InferenceClient
3
+ from os import path
4
+ import os
5
+
6
+ HF_TOKEN = os.environ.get("HF_TOKEN", "YOUR_HF_TOKEN")
7
+ TOKEN_MSG = "Set HF_TOKEN to your Hugging Face token for using InferenceClient"
8
+ SKIP_MSG = "Skipping test because HF_TOKEN is not set"
9
+
10
+ client = InferenceClient("microsoft/resnet-50", token=HF_TOKEN)
11
+
12
+ class TestGeneralSetup(unittest.TestCase):
13
+ def test_setup(self):
14
+ self.assertTrue(HF_TOKEN != "YOUR_HF_TOKEN", TOKEN_MSG)
15
+
16
+ class TestLocalImageBytes(unittest.TestCase):
17
+ @unittest.skipIf(HF_TOKEN == "YOUR_HF_TOKEN", SKIP_MSG)
18
+ def test_bytes_1(self):
19
+ p = path.join(path.dirname(__file__), "sample", "pomdog.jpg")
20
+ with open(p, "rb") as f:
21
+ image_bytes = f.read()
22
+ prediction = client.image_classification(image_bytes)
23
+ self.assertTrue(len(prediction))
24
+
25
+ @unittest.skipIf(HF_TOKEN == "YOUR_HF_TOKEN", SKIP_MSG)
26
+ def test_bytes_2(self):
27
+ p = path.join(path.dirname(__file__), "sample", "persiancat.jpg")
28
+ prediction = client.image_classification(p)
29
+ self.assertTrue(len(prediction))
30
+
31
+ class TestLocalImagePath(unittest.TestCase):
32
+ @unittest.skipIf(HF_TOKEN == "YOUR_HF_TOKEN", SKIP_MSG)
33
+ def test_path_1(self):
34
+ p = path.join(path.dirname(__file__), "sample", "pomdog.jpg")
35
+ prediction = client.image_classification(p)
36
+ self.assertTrue(len(prediction))
37
+
38
+ @unittest.skipIf(HF_TOKEN == "YOUR_HF_TOKEN", SKIP_MSG)
39
+ def test_path_2(self):
40
+ p = path.join(path.dirname(__file__), "sample", "persiancat.jpg")
41
+ prediction = client.image_classification(p)
42
+ self.assertTrue(len(prediction))
43
+
44
+ class TestRemoteImageUrl(unittest.TestCase):
45
+ @unittest.skipIf(HF_TOKEN == "YOUR_HF_TOKEN", SKIP_MSG)
46
+ def test_url_1(self):
47
+ url = "https://i.ytimg.com/vi/AQSTQ4VUPH4/maxresdefault.jpg"
48
+ prediction = client.image_classification(url)
49
+ self.assertTrue(len(prediction))
50
+
51
+ @unittest.skipIf(HF_TOKEN == "YOUR_HF_TOKEN", SKIP_MSG)
52
+ def test_url_2(self):
53
+ url = "https://img.thrfun.com/img/200/610/breed_information_persian_x1.jpg"
54
+ prediction = client.image_classification(url)
55
+ self.assertTrue(len(prediction))
56
+
57
+ if __name__ == "__main__":
58
+ unittest.main()