From 1e1af1d80b04de14f870a757bda0968842ab801d Mon Sep 17 00:00:00 2001 From: AbdelrhmanNile Date: Mon, 5 Sep 2022 11:56:09 +0200 Subject: [PATCH] classification2 using roberta --- freenlpc/freenlpc.py | 31 +++++++++++++++++++++++++++++++ setup.py | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/freenlpc/freenlpc.py b/freenlpc/freenlpc.py index cf10184..99d1853 100644 --- a/freenlpc/freenlpc.py +++ b/freenlpc/freenlpc.py @@ -10,6 +10,7 @@ def __init__(self, api_keys: list, gpu: bool=False, lang: str="en") -> None: self.__lang = lang self.__task_model = { "classification": "bart-large-mnli-yahoo-answers", + "classification2": "xlm-roberta-large-xnli", "dialog_sum": "bart-large-samsum", "headline_gen": "t5-base-en-generate-headline", "entities_extraction": "en_core_web_lg", @@ -66,6 +67,7 @@ def __init_api(self): api_key = self.__api_keys[self.activated_apikey] self.__models = {"classification": nlpcloud.Client(self.which_model("classification"), api_key, gpu=self.__gpu, lang=self.__lang), + "classification2": nlpcloud.Client(self.which_model("classification2"), api_key, gpu=self.__gpu, lang=self.__lang), "dialog_sum": nlpcloud.Client(self.which_model("dialog_sum"), api_key, gpu=self.__gpu, lang=self.__lang), "headline_gen":nlpcloud.Client(self.which_model("headline_gen"), api_key, gpu=self.__gpu, lang=self.__lang), "entities_extraction": nlpcloud.Client(self.which_model("entities_extraction"), api_key, gpu=self.__gpu, lang=self.__lang), @@ -108,6 +110,35 @@ def classification(self, text: str, lables: list, multiclass: bool =True): return {'scored_labels': ordered} except requests.exceptions.HTTPError: self.__init_api() + + def classification2(self, text: str): + """perform classification on a piece of text. + + Args: + text (str): The block of text you want to analyze. 2,500 tokens maximum. + lables (list): A list of labels you want to use to classify your text. 25 labels maximum. + multiclass (bool, optional): Whether multiple labels should be applied to your text, + meaning that the model will calculate an independent score for each label. Defaults to True. + + + Returns: + dict: scored labels. + """ + while True: + try: + sleep(1) + response = self.__models[self.classification2.__name__].classification(text) + result = [] + for i in range(len(response['labels'])): + info = {} + info['label'] = response['labels'][i] + info['score'] = response['scores'][i] + result.append(info) + + ordered = sorted(result, key=itemgetter('score'), reverse=True) + return {'scored_labels': ordered} + except requests.exceptions.HTTPError: + self.__init_api() def dialog_sum(self, dialog: str): diff --git a/setup.py b/setup.py index 511ee83..60ecdd0 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup, find_packages -VERSION = '0.1.3' +VERSION = '0.1.4' DESCRIPTION = 'A wrapper for nlpcloud free-tier services with no requests per minute limits.' LONG_DESCRIPTION = """# freenlpc a wrapper for nlpcloud free-tier.