huggingface / transformers.js

State-of-the-art Machine Learning for the web. Run 🤗 Transformers directly in your browser, with no need for a server!
https://huggingface.co/docs/transformers.js
Apache License 2.0
12.2k stars 775 forks source link

[Feature request] Add pipeline task 'zero-shot-classification' for MNLI trained models #56

Closed trenta3 closed 1 year ago

trenta3 commented 1 year ago

I'm requesting to add the pipeline "zero-shot-classification" for label extraction from a text based on a pretrained model and hypotheses. To view usage examples: https://huggingface.co/facebook/bart-large-mnli

This is a very requested feature as can be measured by the number of downloads of the linked model in the last month (> 2.5M) And thank you for the extremely useful work you are doing!

xenova commented 1 year ago

Sure! I can add this 👍

In the meantime, could you please provide some example python code (and output) using the pipeline function?

trenta3 commented 1 year ago

Yeah sure, I wrote some phrases in different languages

import json
from transformers import pipeline

classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
sequences = [
    "Devo ancora pagare la bolletta del gas",
    "I still have to pay the gas bill",
    "Ich werde morgen klettern. Kommst du mit?",
    "I will go tomorrow to hike. Do you want to come?",
    "Je suis cusinier dans un restaurant",
    "I am a cook in a restaurant",
    "El Papa entrena cada dos días",
    "The pope works out every two days",
]
candidate_labels = ["travel", "hiking", "sport", "dancing", "cooking", "authority", "utilities", "work"]

for sequence in sequences:
    for multi_label in [True, False]:
        result = classifier(sequence, candidate_labels, multi_label=multi_label)
        result["multi_label"] = multi_label
        for idx, key in enumerate(["sequence", "labels", "scores", "multi_label"]):
            prefix = "{ " if idx == 0 else "  "
            suffix = " }" if idx + 1 == len(result) else ","
            print(prefix + '"' + key + '": ' + json.dumps(result[key]) + suffix)

which when run prints

{ "sequence": "Devo ancora pagare la bolletta del gas",
  "labels": ["cooking", "utilities", "work", "authority", "travel", "sport", "hiking", "dancing"],
  "scores": [0.991447389125824, 0.9612486362457275, 0.8709714412689209, 0.2068490833044052, 0.010441462509334087, 0.0028406695928424597, 0.001590077648870647, 0.001008122693747282],
  "multi_label": true }
{ "sequence": "Devo ancora pagare la bolletta del gas",
  "labels": ["cooking", "utilities", "work", "authority", "travel", "sport", "dancing", "hiking"],
  "scores": [0.5983912348747253, 0.25206613540649414, 0.10138948261737823, 0.02908896468579769, 0.007862254977226257, 0.004890661220997572, 0.003495732555165887, 0.00281552504748106],
  "multi_label": false }
{ "sequence": "I still have to pay the gas bill",
  "labels": ["utilities", "work", "authority", "cooking", "hiking", "travel", "sport", "dancing"],
  "scores": [0.9984584450721741, 0.8743591904640198, 0.07910361140966415, 0.01081591285765171, 0.005842195358127356, 0.004699218086898327, 0.002067639259621501, 0.0006328970775939524],
  "multi_label": true }
{ "sequence": "I still have to pay the gas bill",
  "labels": ["utilities", "work", "authority", "hiking", "travel", "sport", "cooking", "dancing"],
  "scores": [0.8816797733306885, 0.08556484431028366, 0.014206331223249435, 0.005271111615002155, 0.004757406190037727, 0.0037542979698628187, 0.0027197289746254683, 0.002046391600742936],
  "multi_label": false }
{ "sequence": "Ich werde morgen klettern. Kommst du mit?",
  "labels": ["travel", "work", "cooking", "authority", "utilities", "hiking", "sport", "dancing"],
  "scores": [0.9717028141021729, 0.9534307718276978, 0.8326925039291382, 0.5644204020500183, 0.4671698808670044, 0.42344552278518677, 0.2895689010620117, 0.1618945449590683],
  "multi_label": true }
{ "sequence": "Ich werde morgen klettern. Kommst du mit?",
  "labels": ["travel", "work", "cooking", "hiking", "authority", "dancing", "utilities", "sport"],
  "scores": [0.3776012659072876, 0.3074505031108856, 0.1677756905555725, 0.04651238024234772, 0.0353471115231514, 0.02822057157754898, 0.02182605117559433, 0.015266447328031063],
  "multi_label": false }
{ "sequence": "I will go tomorrow to hike. Do you want to come?",
  "labels": ["hiking", "travel", "sport", "authority", "work", "utilities", "cooking", "dancing"],
  "scores": [0.9942196607589722, 0.9879510998725891, 0.8137379884719849, 0.8078529834747314, 0.008462291210889816, 0.0036291773431003094, 0.0003602092037908733, 0.00025504903169348836],
  "multi_label": true }
{ "sequence": "I will go tomorrow to hike. Do you want to come?",
  "labels": ["hiking", "travel", "sport", "authority", "work", "utilities", "cooking", "dancing"],
  "scores": [0.5533843636512756, 0.3380189538002014, 0.058553021401166916, 0.038313984870910645, 0.005027722101658583, 0.003661608323454857, 0.0016780057922005653, 0.001362350769340992],
  "multi_label": false }
{ "sequence": "Je suis cusinier dans un restaurant",
  "labels": ["cooking", "work", "authority", "dancing", "travel", "sport", "utilities", "hiking"],
  "scores": [0.9927762150764465, 0.5199383497238159, 0.3442917764186859, 0.04534728452563286, 0.03189418837428093, 0.010471467860043049, 0.004911400377750397, 8.186535706045106e-05],
  "multi_label": true }
{ "sequence": "Je suis cusinier dans un restaurant",
  "labels": ["cooking", "work", "authority", "travel", "dancing", "utilities", "sport", "hiking"],
  "scores": [0.8023020029067993, 0.08228448033332825, 0.06041828915476799, 0.01709311082959175, 0.012606504373252392, 0.012165196239948273, 0.011867685243487358, 0.0012627566466107965],
  "multi_label": false }
{ "sequence": "I am a cook in a restaurant",
  "labels": ["cooking", "work", "authority", "utilities", "sport", "dancing", "travel", "hiking"],
  "scores": [0.996135950088501, 0.9956350922584534, 0.0006340791587717831, 0.0001712672965368256, 0.00014821744116488844, 6.90977685735561e-05, 6.830081110820174e-05, 3.603912773542106e-05],
  "multi_label": true }
{ "sequence": "I am a cook in a restaurant",
  "labels": ["cooking", "work", "authority", "utilities", "sport", "travel", "dancing", "hiking"],
  "scores": [0.6078384518623352, 0.38483986258506775, 0.0018544865306466818, 0.001734126009978354, 0.0013757537817582488, 0.0009676438057795167, 0.0007441536290571094, 0.0006455256370827556],
  "multi_label": false }
{ "sequence": "El Papa entrena cada dos d\u00edas",
  "labels": ["authority", "work", "sport", "utilities", "dancing", "cooking", "travel", "hiking"],
  "scores": [0.9936944246292114, 0.9889096021652222, 0.8996925354003906, 0.01591523177921772, 0.007262231782078743, 0.0031147277913987637, 0.002472982043400407, 0.001052334439009428],
  "multi_label": true }
{ "sequence": "El Papa entrena cada dos d\u00edas",
  "labels": ["work", "authority", "sport", "utilities", "travel", "dancing", "cooking", "hiking"],
  "scores": [0.4954349398612976, 0.3925631046295166, 0.08369459211826324, 0.012066002935171127, 0.0060667796060442924, 0.004071310628205538, 0.0032307838555425406, 0.002872476354241371],
  "multi_label": false }
{ "sequence": "The pope works out every two days",
  "labels": ["authority", "work", "sport", "hiking", "travel", "utilities", "dancing", "cooking"],
  "scores": [0.9307130575180054, 0.9188652038574219, 0.7416408658027649, 0.033093079924583435, 0.0051829395815730095, 0.0014411408919841051, 0.00011244666529819369, 7.980306691024452e-05],
  "multi_label": true }
{ "sequence": "The pope works out every two days",
  "labels": ["work", "authority", "sport", "travel", "utilities", "hiking", "cooking", "dancing"],
  "scores": [0.46334323287010193, 0.2907411456108093, 0.19170965254306793, 0.01879042014479637, 0.018452158197760582, 0.008880219422280788, 0.004273386672139168, 0.003809793619439006],
  "multi_label": false }
xenova commented 1 year ago

Hi! Just an update: So, I got it working, but the bart model you provided seems to give very poor performance when quantized. The results are identical when using the unquantized version, but the model is quite large, so, it wouldn't work in a browser environment (it will probably work in Node, if that is your intended environment).

I've verified the quantized outputs (JS library vs python library) and although they are the same, they differ quite a lot from the unquantized version.

Although a drop in performance is expected for quantization (due to the ~4x reduction in model size), the provided bart model seems to produce very poor results. Do you perhaps have any other (possibly smaller) models you think it would work with?

xenova commented 1 year ago

I've pushed those changes (https://github.com/xenova/transformers.js/commit/230984e59c3d2aaa87aa353aaf274a5ffb01f610) in case you want to get started early.

I will add example usage and documented support tomorrow. I'll close the issue when that is done :)

trenta3 commented 1 year ago

Thank you a lot for all the work. I will look into alternative and smaller models that hopefully won't have such a performance problem.

xenova commented 1 year ago

I ran different (quantized) models on your example inputs (using this code)


let classifier = await pipeline('zero-shot-classification', 'typeform/distilbert-base-uncased-mnli');
let sequences = [
    "Devo ancora pagare la bolletta del gas",
    "I still have to pay the gas bill",
    "Ich werde morgen klettern. Kommst du mit?",
    "I will go tomorrow to hike. Do you want to come?",
    "Je suis cusinier dans un restaurant",
    "I am a cook in a restaurant",
    "El Papa entrena cada dos días",
    "The pope works out every two days",
];
let candidate_labels = ["travel", "hiking", "sport", "dancing", "cooking", "authority", "utilities", "work"];

for (let sequence of sequences) {
    for (let multi_label of [true, false]) {
        let result = await classifier(sequence, candidate_labels, { multi_label });
        result["multi_label"] = multi_label;
        console.log(JSON.stringify(result, null, 2));
    }
}

and here is what I got:

typeform/distilbert-base-uncased-mnli ```json { "sequence": "Devo ancora pagare la bolletta del gas", "labels": [ "cooking", "utilities", "work", "authority", "sport", "travel", "hiking", "dancing" ], "scores": [ 0.9902453059985754, 0.9563363829237796, 0.4522189297113513, 0.4281303955204536, 0.22027366524464617, 0.1316629613972952, 0.00010894777797181787, 0.00006533848359244857 ], "multi_label": true } { "sequence": "Devo ancora pagare la bolletta del gas", "labels": [ "cooking", "utilities", "travel", "authority", "sport", "work", "hiking", "dancing" ], "scores": [ 0.6649761364970503, 0.1616119817179301, 0.08960168696703175, 0.04536425182635001, 0.017936605526676794, 0.01097139326723069, 0.005045922667469906, 0.004492021530260465 ], "multi_label": false } { "sequence": "I still have to pay the gas bill", "labels": [ "utilities", "work", "authority", "sport", "travel", "cooking", "hiking", "dancing" ], "scores": [ 0.42193755228524366, 0.38818018002138766, 0.1602798781668676, 0.10891098283795397, 0.006039909172395629, 0.0002960543346836515, 0.00020549018244267968, 0.00009872990917916817 ], "multi_label": true } { "sequence": "I still have to pay the gas bill", "labels": [ "utilities", "authority", "work", "sport", "travel", "hiking", "dancing", "cooking" ], "scores": [ 0.19396713827854078, 0.1742412659934899, 0.16034920327954294, 0.12946319700919814, 0.0970512032570434, 0.08836132274771191, 0.08541943619300568, 0.07114723324146731 ], "multi_label": false } { "sequence": "Ich werde morgen klettern. Kommst du mit?", "labels": [ "work", "authority", "hiking", "sport", "travel", "cooking", "utilities", "dancing" ], "scores": [ 0.41685593143067323, 0.3937618949361884, 0.29131196575389157, 0.1804471715750621, 0.09100493616579526, 0.027679396975243797, 0.025381374124319858, 0.0009363064579849874 ], "multi_label": true } { "sequence": "Ich werde morgen klettern. Kommst du mit?", "labels": [ "utilities", "travel", "work", "authority", "cooking", "hiking", "sport", "dancing" ], "scores": [ 0.21887279341663843, 0.2106224450843664, 0.1578014351650923, 0.12347119435239792, 0.09910890870610263, 0.08293636992457251, 0.06631678414244331, 0.04087006920838647 ], "multi_label": false } { "sequence": "I will go tomorrow to hike. Do you want to come?", "labels": [ "hiking", "travel", "sport", "cooking", "utilities", "authority", "work", "dancing" ], "scores": [ 0.9984065346665293, 0.9972906156470893, 0.4603649438402447, 0.28764894020964654, 0.2803761250805987, 0.2646544184250676, 0.21482148546650504, 0.1630787380286466 ], "multi_label": true } { "sequence": "I will go tomorrow to hike. Do you want to come?", "labels": [ "hiking", "travel", "utilities", "sport", "authority", "cooking", "work", "dancing" ], "scores": [ 0.664828428621914, 0.3246874870160568, 0.002171831867169705, 0.001965016246687522, 0.0019502264721319715, 0.0017803237353753957, 0.0014759282521490393, 0.0011407577885156632 ], "multi_label": false } { "sequence": "Je suis cusinier dans un restaurant", "labels": [ "cooking", "work", "authority", "sport", "dancing", "utilities", "travel", "hiking" ], "scores": [ 0.9956123762281037, 0.478103108941383, 0.4207594367852989, 0.18193985156913464, 0.045180465299414, 0.0008467889600649423, 0.00037130303839168156, 0.00006049898972782384 ], "multi_label": true } { "sequence": "Je suis cusinier dans un restaurant", "labels": [ "cooking", "sport", "authority", "work", "utilities", "dancing", "travel", "hiking" ], "scores": [ 0.9391143275123579, 0.016840541110355458, 0.012020620458769917, 0.009115834294396928, 0.008996372784974404, 0.006265614020036664, 0.005457150970082771, 0.002189538849026144 ], "multi_label": false } { "sequence": "I am a cook in a restaurant", "labels": [ "cooking", "work", "authority", "sport", "utilities", "travel", "hiking", "dancing" ], "scores": [ 0.9995328419859524, 0.39921810475175995, 0.1945895945665453, 0.0003910289731084995, 0.00027231248643501356, 0.00009901114909847959, 0.0000916095417321447, 0.00006936410066976118 ], "multi_label": true } { "sequence": "I am a cook in a restaurant", "labels": [ "cooking", "work", "authority", "hiking", "utilities", "dancing", "sport", "travel" ], "scores": [ 0.9948385718772081, 0.0011793525655110395, 0.0011372176353453651, 0.0008421281832212454, 0.0007130099166275016, 0.00045050474860628916, 0.00042230733921758026, 0.0004169077342628422 ], "multi_label": false } { "sequence": "El Papa entrena cada dos días", "labels": [ "authority", "sport", "cooking", "work", "travel", "utilities", "dancing", "hiking" ], "scores": [ 0.9145043088409288, 0.756763959781443, 0.7014293469274572, 0.47927114066693194, 0.22782565544870392, 0.16016606578480835, 0.0915675053401547, 0.0002921966871994991 ], "multi_label": true } { "sequence": "El Papa entrena cada dos días", "labels": [ "authority", "utilities", "cooking", "sport", "travel", "work", "dancing", "hiking" ], "scores": [ 0.33507879558173936, 0.19074281226818035, 0.13094437807045461, 0.11570613538414823, 0.09135360042129907, 0.08628948368242631, 0.033323754736108165, 0.016561039855644086 ], "multi_label": false } { "sequence": "The pope works out every two days", "labels": [ "work", "travel", "authority", "sport", "cooking", "utilities", "dancing", "hiking" ], "scores": [ 0.9899650067251105, 0.8545753680534756, 0.78098618051582, 0.6232725303733159, 0.4581839761284343, 0.21118628907057188, 0.20971563083320202, 0.19321620133536638 ], "multi_label": true } { "sequence": "The pope works out every two days", "labels": [ "work", "travel", "authority", "cooking", "sport", "utilities", "hiking", "dancing" ], "scores": [ 0.7423679919847118, 0.08145120488254134, 0.05786096528064109, 0.03370080881720909, 0.029539020736324948, 0.02127888266820652, 0.017068911964278986, 0.016732213666086123 ], "multi_label": false } ```
typeform/mobilebert-uncased-mnli ```json { "sequence": "Devo ancora pagare la bolletta del gas", "labels": [ "authority", "work", "cooking", "utilities", "travel", "sport", "dancing", "hiking" ], "scores": [ 0.7530056097313892, 0.49468390560040854, 0.2796989365494588, 0.20441230363075086, 0.18825231730692513, 0.14981234563328905, 0.11934813015400553, 0.03078633593622286 ], "multi_label": true } { "sequence": "Devo ancora pagare la bolletta del gas", "labels": [ "authority", "work", "cooking", "travel", "utilities", "sport", "dancing", "hiking" ], "scores": [ 0.28248073377588767, 0.2254350119184116, 0.13216558794823785, 0.10166848918615448, 0.08011222349762893, 0.07618704124319758, 0.07356125957623917, 0.02838965285424273 ], "multi_label": false } { "sequence": "I still have to pay the gas bill", "labels": [ "authority", "work", "utilities", "travel", "hiking", "sport", "cooking", "dancing" ], "scores": [ 0.6551644548382497, 0.6129691133588665, 0.47548560956661695, 0.314576943079704, 0.2919446116677919, 0.17885902995630698, 0.13064967154665214, 0.10963733337488733 ], "multi_label": true } { "sequence": "I still have to pay the gas bill", "labels": [ "authority", "work", "utilities", "travel", "hiking", "dancing", "sport", "cooking" ], "scores": [ 0.2602615928899545, 0.23601034228373352, 0.15110126520533054, 0.1067366986226086, 0.096970812101255, 0.052682296987482016, 0.049993497965351096, 0.04624349394428466 ], "multi_label": false } { "sequence": "Ich werde morgen klettern. Kommst du mit?", "labels": [ "work", "authority", "travel", "utilities", "sport", "dancing", "hiking", "cooking" ], "scores": [ 0.8110844484051041, 0.771906848180183, 0.45701728225143257, 0.32360896882693774, 0.21494707286874998, 0.1566069883404282, 0.13259627687109837, 0.11509211421352519 ], "multi_label": true } { "sequence": "Ich werde morgen klettern. Kommst du mit?", "labels": [ "work", "authority", "travel", "utilities", "sport", "dancing", "hiking", "cooking" ], "scores": [ 0.32199472732015755, 0.2314746989781787, 0.1466783688682585, 0.09121098142509405, 0.06761422316630494, 0.04978381149956141, 0.04813851654289773, 0.04310467219954711 ], "multi_label": false } { "sequence": "I will go tomorrow to hike. Do you want to come?", "labels": [ "hiking", "travel", "authority", "dancing", "work", "cooking", "sport", "utilities" ], "scores": [ 0.7465699592671287, 0.5503920721065462, 0.41977054695820487, 0.351660807823447, 0.34328417528896926, 0.31891211864087254, 0.2880479339174353, 0.21143029645498143 ], "multi_label": true } { "sequence": "I will go tomorrow to hike. Do you want to come?", "labels": [ "hiking", "travel", "authority", "work", "sport", "cooking", "dancing", "utilities" ], "scores": [ 0.3156145528376795, 0.22568159496990625, 0.11441383699886969, 0.10424194197519329, 0.06647398030552303, 0.06548766161347103, 0.05443163413718205, 0.05365479716217501 ], "multi_label": false } { "sequence": "Je suis cusinier dans un restaurant", "labels": [ "authority", "work", "cooking", "dancing", "travel", "sport", "utilities", "hiking" ], "scores": [ 0.7026705325618069, 0.4937450833467401, 0.36027176471702677, 0.21167154618199074, 0.1719054680208508, 0.06656106351887439, 0.04758529500225259, 0.03614105193360182 ], "multi_label": true } { "sequence": "Je suis cusinier dans un restaurant", "labels": [ "authority", "work", "cooking", "dancing", "travel", "sport", "utilities", "hiking" ], "scores": [ 0.3067705593429678, 0.2347080999498034, 0.1428124573221425, 0.10103139359788958, 0.08880494318968142, 0.051015219316085596, 0.03990314401169101, 0.034954183269738634 ], "multi_label": false } { "sequence": "I am a cook in a restaurant", "labels": [ "cooking", "authority", "work", "dancing", "travel", "utilities", "sport", "hiking" ], "scores": [ 0.8751668829179938, 0.4603785075634197, 0.2536750088727179, 0.22859254988798253, 0.1353032228150551, 0.07706698605012774, 0.05247147216552127, 0.008766334656286392 ], "multi_label": true } { "sequence": "I am a cook in a restaurant", "labels": [ "cooking", "authority", "work", "travel", "dancing", "utilities", "sport", "hiking" ], "scores": [ 0.5863532548109591, 0.12065697570510582, 0.09966332185362288, 0.059138342478574385, 0.04727746555563931, 0.03825599600299708, 0.033305696480254526, 0.015348947112846938 ], "multi_label": false } { "sequence": "El Papa entrena cada dos días", "labels": [ "work", "authority", "dancing", "travel", "utilities", "cooking", "sport", "hiking" ], "scores": [ 0.7971092230009061, 0.7618511067926934, 0.20833726813607753, 0.14954083187080852, 0.13342233606172724, 0.08255992017255499, 0.06197599664345723, 0.050301524296339016 ], "multi_label": true } { "sequence": "El Papa entrena cada dos días", "labels": [ "work", "authority", "dancing", "travel", "utilities", "cooking", "sport", "hiking" ], "scores": [ 0.33946729261628444, 0.3224387397909557, 0.08074730321692582, 0.07560090748686925, 0.05761278700360373, 0.05498164587504124, 0.040803911595365665, 0.028347412414953966 ], "multi_label": false } { "sequence": "The pope works out every two days", "labels": [ "work", "authority", "travel", "dancing", "utilities", "sport", "cooking", "hiking" ], "scores": [ 0.750297426458442, 0.7216455861618669, 0.16700669433328402, 0.1218820974640524, 0.08317990720745941, 0.07122700466902462, 0.050406668446562566, 0.021577743115519337 ], "multi_label": true } { "sequence": "The pope works out every two days", "labels": [ "work", "authority", "travel", "sport", "utilities", "dancing", "cooking", "hiking" ], "scores": [ 0.49327575164732235, 0.2618316531378385, 0.07252403564249621, 0.04691509357714434, 0.04385029289163932, 0.03865986472470574, 0.028867924765201232, 0.014075383613652225 ], "multi_label": false } ```
squeezebert/squeezebert-mnli ```json { "sequence": "Devo ancora pagare la bolletta del gas", "labels": [ "hiking", "utilities", "dancing", "sport", "cooking", "work", "authority", "travel" ], "scores": [ 0.7095718241201571, 0.5601871952076724, 0.5029455995275317, 0.5016108909764665, 0.47877657142352764, 0.466289186612325, 0.46152888313363627, 0.4227685433370887 ], "multi_label": true } { "sequence": "Devo ancora pagare la bolletta del gas", "labels": [ "hiking", "utilities", "dancing", "sport", "cooking", "authority", "work", "travel" ], "scores": [ 0.19784173017316714, 0.13444191124299118, 0.12777935076716204, 0.11601583124253262, 0.11234377201246298, 0.10910830446213993, 0.10138671885557272, 0.10108238124397136 ], "multi_label": false } { "sequence": "I still have to pay the gas bill", "labels": [ "utilities", "authority", "dancing", "work", "sport", "hiking", "travel", "cooking" ], "scores": [ 0.6920918877042259, 0.6807455479988296, 0.6773820914051899, 0.6749958797786557, 0.6679339987207117, 0.6564164308761861, 0.6138683257667512, 0.6051044294743433 ], "multi_label": true } { "sequence": "I still have to pay the gas bill", "labels": [ "dancing", "utilities", "work", "hiking", "sport", "authority", "travel", "cooking" ], "scores": [ 0.13303994872237276, 0.13143114056477564, 0.13129310916039197, 0.1297354403487904, 0.1277798481824145, 0.12209754942655308, 0.11249525484554315, 0.11212770874915856 ], "multi_label": false } { "sequence": "Ich werde morgen klettern. Kommst du mit?", "labels": [ "hiking", "work", "dancing", "sport", "cooking", "authority", "utilities", "travel" ], "scores": [ 0.5414073832583677, 0.4815155791563841, 0.474639502778432, 0.44772975273639887, 0.44601043590342176, 0.41879345763800785, 0.4117502230277659, 0.3839596742798006 ], "multi_label": true } { "sequence": "Ich werde morgen klettern. Kommst du mit?", "labels": [ "hiking", "dancing", "cooking", "work", "sport", "utilities", "authority", "travel" ], "scores": [ 0.15509447022234335, 0.1331437910828215, 0.1260127687377355, 0.12553822443172383, 0.12360629724663506, 0.11355988313315972, 0.11174757322173508, 0.11129699192384598 ], "multi_label": false } { "sequence": "I will go tomorrow to hike. Do you want to come?", "labels": [ "hiking", "dancing", "travel", "work", "sport", "cooking", "authority", "utilities" ], "scores": [ 0.6966266968248899, 0.5949871884281633, 0.551912266281817, 0.5471921352908906, 0.5164952261415365, 0.486871005270248, 0.4746645262678212, 0.37451048975103046 ], "multi_label": true } { "sequence": "I will go tomorrow to hike. Do you want to come?", "labels": [ "hiking", "dancing", "work", "travel", "sport", "cooking", "authority", "utilities" ], "scores": [ 0.17519310143931277, 0.14570628517597722, 0.1313725017854946, 0.1261424704419556, 0.11895243803032615, 0.11293546519045419, 0.10415287886934424, 0.08554485906713531 ], "multi_label": false } { "sequence": "Je suis cusinier dans un restaurant", "labels": [ "work", "dancing", "hiking", "cooking", "authority", "sport", "utilities", "travel" ], "scores": [ 0.685594391825924, 0.6804463347513114, 0.6689860462873715, 0.6626885075629517, 0.597337889847802, 0.5610321969773422, 0.4575257845997273, 0.43213171067234163 ], "multi_label": true } { "sequence": "Je suis cusinier dans un restaurant", "labels": [ "hiking", "dancing", "cooking", "work", "sport", "authority", "utilities", "travel" ], "scores": [ 0.14505840817807447, 0.14505786347133903, 0.14314307299660514, 0.1374805880731423, 0.12406838698950177, 0.12225560812889871, 0.09158454848510765, 0.0913515236773308 ], "multi_label": false } { "sequence": "I am a cook in a restaurant", "labels": [ "dancing", "work", "cooking", "hiking", "authority", "sport", "travel", "utilities" ], "scores": [ 0.8146369234016859, 0.8114369947413465, 0.8074190726996751, 0.7902977740782051, 0.7691464006229612, 0.7626229488815847, 0.7547425900638316, 0.678656690734859 ], "multi_label": true } { "sequence": "I am a cook in a restaurant", "labels": [ "dancing", "work", "hiking", "cooking", "sport", "travel", "authority", "utilities" ], "scores": [ 0.15007059069796364, 0.13365853492427293, 0.1333828700910458, 0.13146129681735813, 0.1230439362715313, 0.11937660679029917, 0.11576616332114621, 0.09324000108638282 ], "multi_label": false } { "sequence": "El Papa entrena cada dos días", "labels": [ "hiking", "dancing", "sport", "cooking", "utilities", "work", "authority", "travel" ], "scores": [ 0.786116480884578, 0.6746898562888978, 0.6450723665436988, 0.5924688251920639, 0.5545077364899046, 0.5538634536283711, 0.5221465125685089, 0.49050093290404545 ], "multi_label": true } { "sequence": "El Papa entrena cada dos días", "labels": [ "hiking", "dancing", "sport", "cooking", "utilities", "work", "authority", "travel" ], "scores": [ 0.20894781703907211, 0.15229133364129108, 0.12301868455336636, 0.11993730760126618, 0.10478335004834179, 0.09900150834732152, 0.09793132945650165, 0.09408866931283935 ], "multi_label": false } { "sequence": "The pope works out every two days", "labels": [ "work", "hiking", "dancing", "cooking", "sport", "authority", "travel", "utilities" ], "scores": [ 0.8504547714313634, 0.8413993156895064, 0.8368079573842164, 0.8304142543884816, 0.8144448430683495, 0.7821717893650811, 0.7575793416429795, 0.6923766363614529 ], "multi_label": true } { "sequence": "The pope works out every two days", "labels": [ "dancing", "hiking", "cooking", "work", "sport", "authority", "travel", "utilities" ], "scores": [ 0.14771903119158325, 0.14387317867279667, 0.13948038252432723, 0.1388655483899186, 0.1296542844861241, 0.1096925141262391, 0.10508174819619938, 0.0856333124128118 ], "multi_label": false } ```
facebook/bart-large-mnli ```json { "sequence": "Devo ancora pagare la bolletta del gas", "labels": [ "travel", "authority", "cooking", "utilities", "hiking", "work", "sport", "dancing" ], "scores": [ 0.7382692940985826, 0.7257935356834506, 0.6896937413618119, 0.6896229327169038, 0.6434514857443152, 0.6005172227819816, 0.5961676721475843, 0.5862954481668546 ], "multi_label": true } { "sequence": "Devo ancora pagare la bolletta del gas", "labels": [ "authority", "utilities", "travel", "cooking", "work", "hiking", "sport", "dancing" ], "scores": [ 0.1436111586634048, 0.1347001250302126, 0.1327983268794599, 0.1293599616413353, 0.12561314981480382, 0.1255922175515031, 0.1054393162340102, 0.10288574418527034 ], "multi_label": false } { "sequence": "I still have to pay the gas bill", "labels": [ "work", "travel", "cooking", "sport", "dancing", "authority", "hiking", "utilities" ], "scores": [ 0.7101498799656039, 0.6907973085494992, 0.6824733749746831, 0.6692983344829856, 0.6289913049610285, 0.6272394942929628, 0.5928325990723533, 0.5658989698676924 ], "multi_label": true } { "sequence": "I still have to pay the gas bill", "labels": [ "cooking", "work", "travel", "hiking", "sport", "authority", "dancing", "utilities" ], "scores": [ 0.16981317423654943, 0.15624907132459742, 0.1249033686446265, 0.12393417365989402, 0.12383221151264265, 0.10883705981616731, 0.10165531567017473, 0.09077562513534791 ], "multi_label": false } { "sequence": "Ich werde morgen klettern. Kommst du mit?", "labels": [ "authority", "sport", "travel", "dancing", "hiking", "work", "cooking", "utilities" ], "scores": [ 0.7823771299114956, 0.7243631659348523, 0.7137097252123383, 0.6828288541587093, 0.6315453732761332, 0.6205735063518703, 0.5209283966422481, 0.4900709481268492 ], "multi_label": true } { "sequence": "Ich werde morgen klettern. Kommst du mit?", "labels": [ "authority", "sport", "travel", "dancing", "hiking", "work", "cooking", "utilities" ], "scores": [ 0.17284391511428718, 0.15303401688978494, 0.13878554371228327, 0.1336537158492981, 0.121363861650043, 0.11151744356665257, 0.09592566383948589, 0.07287583937816487 ], "multi_label": false } { "sequence": "I will go tomorrow to hike. Do you want to come?", "labels": [ "sport", "travel", "hiking", "authority", "work", "utilities", "dancing", "cooking" ], "scores": [ 0.8420013949019144, 0.7967057397093049, 0.7777279153292183, 0.741339682280214, 0.7290380139843836, 0.6934892227880977, 0.6871216984874124, 0.6825875719764865 ], "multi_label": true } { "sequence": "I will go tomorrow to hike. Do you want to come?", "labels": [ "sport", "work", "hiking", "authority", "travel", "cooking", "dancing", "utilities" ], "scores": [ 0.1762039509682679, 0.14273278355764127, 0.14134739763792603, 0.12500683394491544, 0.12210418315243765, 0.11048059361426775, 0.10429631661329461, 0.07782794051124954 ], "multi_label": false } { "sequence": "Je suis cusinier dans un restaurant", "labels": [ "travel", "utilities", "cooking", "hiking", "sport", "dancing", "work", "authority" ], "scores": [ 0.8452055538438696, 0.8071082273036786, 0.7920366382727202, 0.7669067309993126, 0.7461172961195923, 0.6906315029025424, 0.5951141758428791, 0.5611158030277402 ], "multi_label": true } { "sequence": "Je suis cusinier dans un restaurant", "labels": [ "sport", "cooking", "utilities", "travel", "dancing", "hiking", "work", "authority" ], "scores": [ 0.1528842918157485, 0.1442089759404554, 0.14332008665151208, 0.14149801669012035, 0.127174712914871, 0.11461946949251174, 0.10313741607954093, 0.07315703041524005 ], "multi_label": false } { "sequence": "I am a cook in a restaurant", "labels": [ "cooking", "authority", "work", "travel", "sport", "dancing", "utilities", "hiking" ], "scores": [ 0.8920636616157175, 0.8594615544936064, 0.8299212745204784, 0.7888765418962205, 0.7522871006370978, 0.7397451631570987, 0.6735660390448619, 0.6282058061941442 ], "multi_label": true } { "sequence": "I am a cook in a restaurant", "labels": [ "cooking", "work", "authority", "travel", "sport", "dancing", "hiking", "utilities" ], "scores": [ 0.1963758746919129, 0.14778533450017423, 0.13872478532922866, 0.11434372665857924, 0.11388318471901368, 0.11013095165232042, 0.09846329145375717, 0.08029285099501376 ], "multi_label": false } { "sequence": "El Papa entrena cada dos días", "labels": [ "authority", "dancing", "sport", "travel", "hiking", "work", "cooking", "utilities" ], "scores": [ 0.7971630748724884, 0.785117738657495, 0.7840981242459101, 0.7718731961664825, 0.723225553593394, 0.7022453904673059, 0.5923213830201877, 0.5768271235742223 ], "multi_label": true } { "sequence": "El Papa entrena cada dos días", "labels": [ "sport", "authority", "travel", "hiking", "dancing", "work", "cooking", "utilities" ], "scores": [ 0.15957012255596326, 0.15701076297987046, 0.14528290485808226, 0.13122357196862725, 0.12482856121831548, 0.11703058620046981, 0.09242591134443931, 0.07262757887423209 ], "multi_label": false } { "sequence": "The pope works out every two days", "labels": [ "work", "authority", "travel", "utilities", "sport", "hiking", "dancing", "cooking" ], "scores": [ 0.8302606288169339, 0.8132347139125635, 0.7953501933849112, 0.7856106710848418, 0.7606516211897575, 0.7512878198850903, 0.7077362692445592, 0.6191230008506056 ], "multi_label": true } { "sequence": "The pope works out every two days", "labels": [ "work", "authority", "sport", "travel", "utilities", "hiking", "cooking", "dancing" ], "scores": [ 0.1609009526772765, 0.13715180476285874, 0.1286404518674437, 0.12848703678374276, 0.12674661727136777, 0.11190808810271427, 0.10530177255789669, 0.10086327597669972 ], "multi_label": false } ```

As mentioned before, there is quite a drop in performance when using the quantized models. Not 100% sure why this is, but there are some warning messages when exporting, so, that might have to do with it.

typeform/distilbert-base-uncased-mnli seems to perform well on your examples (particularly for the English ones). The others are much less confident with their outputs.

If possible, I would recommend that you use unquantized versions of the models (you would have to export these yourself, see README for instructions).

xenova commented 1 year ago

I'm just finishing up the demo, then will publish the update :)

Edit: GitHub actions/pages are down right now (https://www.githubstatus.com/incidents/z3c6q056q332)... will publish when resolved :')

xenova commented 1 year ago

Here it is! 🥳 v1.3.5 is out (https://www.npmjs.com/package/@xenova/transformers) and zero-shot classification is now supported.

https://user-images.githubusercontent.com/26504141/228693835-3459695b-474d-4cc0-8f2d-541fa14a05da.mp4


Example code:

// npm i @xenova/transformers
import { pipeline } from '@xenova/transformers';

// Allocate pipeline
// (uses 'typeform/distilbert-base-uncased-mnli' by default)
let classifier = await pipeline('zero-shot-classification');

// Specify text and classes
let text = 'I have a problem with my iphone that needs to be resolved asap!';
let classes = ['urgent', 'not urgent', 'phone', 'tablet', 'microwave'];

let output = await classifier(text, classes, { multi_label: true });
// {
//     sequence: "I have a problem with my iphone that needs to be resolved asap!",
//     labels: ["phone", "urgent", "not urgent", "tablet", "microwave"],
//     scores: [0.783870881, 0.451184637, 0.009843352, 0.001410044, 0.000039902]
// }

Let me know if you have any other questions 👍

trenta3 commented 1 year ago

Thank you, this was really fast! I haven't had the time to search for another model, but will report back on it.