Skip to content

Commit

Permalink
classification2 using roberta
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelrhmanNile committed Sep 5, 2022
1 parent 2e69cce commit 1e1af1d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
31 changes: 31 additions & 0 deletions freenlpc/freenlpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit 1e1af1d

Please sign in to comment.