Skip to content

Commit

Permalink
tokens & sentence dependencies api support
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelrhmanNile committed Sep 1, 2022
1 parent d6e6f2d commit afe9308
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 27 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ a wrapper for nlpcloud free-tier.
- embeddings
- translation
- language detection
- tokenization and lemmatization
- sentence dependencies

# INSTALLATION
```
Expand Down
98 changes: 72 additions & 26 deletions freenlpc/freenlpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def __init__(self, api_keys: list, gpu: bool=False, lang: str="en") -> None:
"summarization": "bart-large-cnn",
"embeddings": "paraphrase-multilingual-mpnet-base-v2",
"translation": "nllb-200-3-3b",
"lang_detection": "python-langdetect"
"lang_detection": "python-langdetect",
"tokenize": "en_core_web_lg",
"sentence_dependencies": "en_core_web_lg"
}
self.activated_apikey = None
self.__check_keys()
Expand Down Expand Up @@ -73,7 +75,9 @@ def __init_api(self):
"sentiment_emotions": nlpcloud.Client(self.which_model("sentiment_emotions"), api_key, gpu=self.__gpu, lang=self.__lang),
"summarization": nlpcloud.Client(self.which_model("summarization"), api_key, gpu=self.__gpu, lang=self.__lang),
"translation": nlpcloud.Client(self.which_model("translation"), api_key, gpu=self.__gpu),
"lang_detection": nlpcloud.Client(self.which_model("lang_detection"), api_key)
"lang_detection": nlpcloud.Client(self.which_model("lang_detection"), api_key),
"tokenize": nlpcloud.Client(self.which_model("tokenize"), api_key),
"sentence_dependencies": nlpcloud.Client(self.which_model("sentence_dependencies"), api_key)
}


Expand All @@ -91,8 +95,8 @@ def classification(self, text: str, lables: list, multiclass: bool =True):
"""
while True:
try:
sleep(2)
response = self.__models["classification"].classification(text, lables, multiclass)
sleep(1)
response = self.__models[self.classification.__name__].classification(text, lables, multiclass)
result = []
for i in range(len(response['labels'])):
info = {}
Expand All @@ -117,8 +121,8 @@ def dialog_sum(self, dialog: str):
"""
while True:
try:
sleep(2)
return self.__models["dialog_sum"].summarization(dialog)
sleep(1)
return self.__models[self.dialog_sum.__name__].summarization(dialog)
except requests.exceptions.HTTPError:
self.__init_api()

Expand All @@ -133,8 +137,8 @@ def headline_gen(self, text: str):
"""
while True:
try:
sleep(2)
return self.__models["headline_gen"].summarization(text)
sleep(1)
return self.__models[self.headline_gen.__name__].summarization(text)
except requests.exceptions.HTTPError:
self.__init_api()

Expand All @@ -149,8 +153,8 @@ def entities_extraction(self, text: str):
"""
while True:
try:
sleep(2)
return self.__models["entities_extraction"].entities(text)
sleep(1)
return self.__models[self.entities_extraction.__name__].entities(text)
except requests.exceptions.HTTPError:
self.__init_api()

Expand All @@ -167,8 +171,8 @@ def qa(self, question: str, context: str):
"""
while True:
try:
sleep(2)
return self.__models["qa"].question(question=question, context=context)
sleep(1)
return self.__models[self.qa.__name__].question(question=question, context=context)
except requests.exceptions.HTTPError:
self.__init_api()

Expand All @@ -185,8 +189,8 @@ def semantic_similarity(self, texts: list):
"""
while True:
try:
sleep(2)
return self.__models["semantic_similarity"].semantic_similarity(texts)
sleep(1)
return self.__models[self.semantic_similarity.__name__].semantic_similarity(texts)
except requests.exceptions.HTTPError:
self.__init_api()

Expand All @@ -201,8 +205,8 @@ def sentiment_pos_neg(self, text: str):
"""
while True:
try:
sleep(2)
return self.__models["sentiment_pos_neg"].sentiment(text)
sleep(1)
return self.__models[self.sentiment_pos_neg.__name__].sentiment(text)
except requests.exceptions.HTTPError:
self.__init_api()

Expand All @@ -217,8 +221,8 @@ def sentiment_emotions(self, text: str):
"""
while True:
try:
sleep(2)
response = self.__models["sentiment_emotions"].sentiment(text)["scored_labels"]
sleep(1)
response = self.__models[self.sentiment_emotions.__name__].sentiment(text)["scored_labels"]
ordered = sorted(response, key=itemgetter('score'), reverse=True)
return {'scored_labels': ordered}
except requests.exceptions.HTTPError:
Expand All @@ -235,15 +239,24 @@ def summarization(self, text: str):
"""
while True:
try:
sleep(2)
return self.__models["summarization"].summarization(text)
sleep(1)
return self.__models[self.summarization.__name__].summarization(text)
except requests.exceptions.HTTPError:
self.__init_api()

def embeddings(self, texts: list):
"""calculate word embeddings.
Args:
texts (list): The pieces of text you want to analyze.
The list can contain 50 elements maximum. Each element should contain 128 tokens maximum.
Returns:
dict: embeddings
"""
while True:
try:
sleep(2)
sleep(1)
return self.__models["semantic_similarity"].embeddings(texts)
except requests.exceptions.HTTPError:
self.__init_api()
Expand All @@ -266,8 +279,8 @@ def translation(self, text: str, source_lang: str, target_lang: str):
"""
while True:
try:
sleep(2)
return self.__models["translation"].translation(text, source_lang, target_lang)
sleep(1)
return self.__models[self.translation.__name__].translation(text, source_lang, target_lang)
except requests.exceptions.HTTPError:
self.__init_api()

Expand All @@ -282,16 +295,49 @@ def lang_detection(self, text: str):
"""
while True:
try:
sleep(2)
response = self.__models["lang_detection"].langdetection(text)["languages"]
sleep(1)
response = self.__models[self.lang_detection.__name__].langdetection(text)["languages"]
result = []
for i in response:
lang = list(i.keys())[0]
score = i[lang]
result.append({'language': lang, 'score': score})
return {'scored_languages': result}
except requests.exceptions.HTTPError:
self.__init_api()
self.__init_api()

def tokenize(self, text: str):
"""tokenize and lemmatize a text
Args:
text (str): The sentence containing the tokens to extract. 350 tokens maximum.
Returns:
dict: tokens
"""
while True:
try:
sleep(1)
return self.__models[self.tokenize.__name__].tokens(text)
except requests.exceptions.HTTPError:
self.__init_api()

def sentence_dependencies(self, text: str):
"""perform Part-of-Speech (POS) tagging.
Args:
text (str): The sentences containing parts of speech to extract. 350 tokens maximum.
Returns:
dict: sentence dependencies.
"""
while True:
try:
sleep(1)
return self.__models[self.sentence_dependencies.__name__].sentence_dependencies(text)
except requests.exceptions.HTTPError:
self.__init_api()


def __repr__(self) -> str:
tasks = list(self.__task_model.keys())
Expand Down
4 changes: 3 additions & 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.2'
VERSION = '0.1.3'
DESCRIPTION = 'A wrapper for nlpcloud free-tier services with no requests per minute limits.'
LONG_DESCRIPTION = """# freenlpc
a wrapper for nlpcloud free-tier.
Expand All @@ -21,6 +21,8 @@
- embeddings
- translation
- language detection
- tokenization and lemmatization
- sentence dependencies
# INSTALLATION
```
Expand Down

0 comments on commit afe9308

Please sign in to comment.