Repository: davanstrien/flyswot (language: python)

Issue:
add initial support for 🤗 transformers inference
closes #458 


Discussion / hints:

Fix patch:
diff --git a/src/flyswot/inference.py b/src/flyswot/inference.py
index 1a06a75a..274b7b3d 100644
--- a/src/flyswot/inference.py
+++ b/src/flyswot/inference.py
@@ -364,7 +364,7 @@ def create_csv_fname(csv_directory: Path) -> Path:
     """Creates a csv filename"""
     date_now = datetime.now()
     date_now = date_now.strftime("%Y_%m_%d_%H_%M")
-    fname = Path(date_now + ".csv")
+    fname = Path(f"{date_now}.csv")
     return Path(csv_directory / fname)
 
 
@@ -507,7 +507,7 @@ def predict_image(
         if len(self.vocab) < 2:
             pred = self._postprocess(raw_result)
             arg_max = int(np.array(pred).argmax())
-            predicted_label = self.vocab_mappings[0][int(arg_max)]
+            predicted_label = self.vocab_mappings[0][arg_max]
             confidence = float(np.array(pred).max())
             return ImagePredictionItem(image, predicted_label, confidence)
         else:
@@ -579,8 +579,7 @@ def __init__(self):
 
     def predict_image(self, image: Path):
         """Predict single Image."""
-        prediction = self.session(image, top_k=10)
-        return prediction
+        return self.session(image, top_k=10)
 
     def predict_batch(self, batch: Iterable[Path], bs: int):
         """Predict batch of images"""
@@ -591,10 +590,8 @@ def predict_batch(self, batch: Iterable[Path], bs: int):
         prediction_dicts = [self._process_prediction_dict(pred) for pred in predictions]
         all_pred = []
         for file, pred in zip(str_batch, prediction_dicts):
-            item_pred = []
             flysheet_other_pred = self._create_flysheet_other_predictions(pred)
-            item_pred.append(flysheet_other_pred)
-            item_pred.append(pred)
+            item_pred = [flysheet_other_pred, pred]
             prediction = MultiLabelImagePredictionItem(Path(file), item_pred)
             all_pred.append(prediction)
         return MultiPredictionBatch(all_pred)


Test patch:
diff --git a/tests/test_inference.py b/tests/test_inference.py
index f259439f..e0ad5135 100644
--- a/tests/test_inference.py
+++ b/tests/test_inference.py
@@ -98,9 +98,6 @@ def test_multi_prediction_batch(confidence: float, label: str, imfile: Any):
     assert batch.batch_labels
     assert len(list(batch.batch_labels)) == 2
     assert hasattr(batch.batch_labels, "__next__")
-    for labels in batch.batch_labels:
-        for label in labels:
-            assert True
 
 
 FIXTURE_DIR = os.path.join(