Skip to content

Commit

Permalink
fix requests take too long by adding a timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelrhmanNile committed Sep 22, 2022
1 parent 512ed61 commit 0300c62
Show file tree
Hide file tree
Showing 3 changed files with 468 additions and 6 deletions.
10 changes: 6 additions & 4 deletions freenlpc/freenlpc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from operator import itemgetter
import nlpcloud
import nlpcloudd as nlpcloud
import requests
from time import sleep

Expand Down Expand Up @@ -41,7 +41,7 @@ def __check_keys(self):
nlpcloud.Client(self.which_model("sentiment_pos_neg"),
self.__api_keys[i], lang="en").sentiment("this pizze is good")
except requests.exceptions.HTTPError as e:
if str(e).find("Unauthorized") != -1:
if str(e).find("Unauthorized") != -1 or str(e).find("Forbidden") != -1:
raise Exception(
f"NLPCLOUD API Token at index {i} is not valid.")

Expand Down Expand Up @@ -258,7 +258,8 @@ def sentiment_emotions(self, text: str):
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:
except requests.exceptions.HTTPError as e:
print(e)
self.__init_api()

def summarization(self, text: str):
Expand Down Expand Up @@ -290,7 +291,8 @@ def embeddings(self, texts: list):
while True:
try:
sleep(1)
return self.__models["semantic_similarity"].embeddings(texts)
response = self.__models["semantic_similarity"].embeddings(texts)
return response
except requests.exceptions.HTTPError:
self.__init_api()

Expand Down
Loading

0 comments on commit 0300c62

Please sign in to comment.