generated from github-education-resources/codespaces-teaching-template-py
-
Notifications
You must be signed in to change notification settings - Fork 144
/
zero_shot_classification.py
executable file
·46 lines (34 loc) · 1.46 KB
/
zero_shot_classification.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
"""Create Zero-shot classification command-line tool using Hugging Face's transformers library."""
from transformers import pipeline
import click
# Create a function that reads a file
def read_file(filename):
with open(filename, encoding="utf-8") as myfile:
return myfile.read()
# create a function that grabs candidate labels from a file
def read_labels(kw_file):
return read_file(kw_file).splitlines()
# create a function that reads a file performs zero-shot classification
def classify(text, labels, model="MoritzLaurer/mDeBERTa-v3-base-mnli-xnli"):
classifier = pipeline("zero-shot-classification", model=model)
results = classifier(text, labels, multi_label=False)
return results
# create click group
@click.group()
def cli():
"""A cli for zero-shot classification"""
# create a click command that performs zero-shot classification
@cli.command("classify")
@click.argument("filename", default="four-score.m4a.txt")
@click.argument("kw_file", default="keywords.txt")
def classifycli(filename, kw_file):
"""Classify text using zero-shot classification"""
text = read_file(filename)
labels = read_labels(kw_file) # needs to be a sequence
results = classify(text, labels)
# print out each label and its score in a tabular format with colors
for label, score in zip(results["labels"], results["scores"]):
click.secho(f"{label}\t{score:.2f}", fg="green")
if __name__ == "__main__":
cli()