Original article
http://gigasquidsoftware.com/blog/2021/03/15/breakfast-with-zero-shot-nlp/
Code
http://github.com/mullikine/zero-shot-nlp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
(ns zero-shot-nlp.core
  (:gen-class)
  (:require [libpython-clj2.require :refer [require-python]]
            [libpython-clj2.python :as py :refer [py. py.. py.-]]))

(require-python '[transformers :bind-ns])

(require '[clojure.pprint :as p])

(use '[clojure.java.shell :only [sh]])

;; (in-ns 'clojure.pprint)

(defn -main
  "I classify stuff."
  [& args]
  ;; import transformers; transformers.pipeline("zero-shot-classification")
  (def classifier (py. transformers "pipeline" "zero-shot-classification"))

  (def text "French Toast with egg and bacon in the center with maple syrup on top. Sprinkle with powdered sugar if desired.")

  (def labels ["breakfast" "lunch" "dinner"])

  (let [s (classifier text labels)
        j (clojure.data.json/write-str s)]
    (sh "sh" "-c" "jq . | tv" :in j)))

In python, the following syntax is used to get a classifier.

1
import transformers; transformers.pipeline("zero-shot-classification")

In clojure, this is how it’s done with the interop.

1
(py. transformers "pipeline" "zero-shot-classification")
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{
  "sequence": "French Toast with egg and bacon in the center with maple syrup on top. Sprinkle with powdered sugar if desired.",
  "labels": [
    "breakfast",
    "lunch",
    "dinner"
  ],
  "scores": [
    0.989736795425415,
    0.007010197266936302,
    0.003252969356253743
  ]
}

Breakfast wins.