From aee44843657a8e1de388740a1e428ad1f85126a2 Mon Sep 17 00:00:00 2001 From: proguy914629 Date: Wed, 5 Jan 2022 21:35:29 +0700 Subject: [PATCH] Upgrade version and add support for the new AI endpoints Upgrade version to 0.4.0 and have support for text generation, sentiment and summarization endpoints --- openrobot/api_wrapper/__init__.py | 2 +- openrobot/api_wrapper/_async.py | 176 ++++++++++++++++++++++++++++++ openrobot/api_wrapper/_sync.py | 170 +++++++++++++++++++++++++++++ openrobot/api_wrapper/results.py | 115 +++++++++++++++++++ 4 files changed, 462 insertions(+), 1 deletion(-) diff --git a/openrobot/api_wrapper/__init__.py b/openrobot/api_wrapper/__init__.py index 3d56c99..0d56983 100644 --- a/openrobot/api_wrapper/__init__.py +++ b/openrobot/api_wrapper/__init__.py @@ -7,4 +7,4 @@ from . import _async, _sync, translate, results, error, speech -__version__ = '0.3.0.6' +__version__ = '0.4.0' diff --git a/openrobot/api_wrapper/_async.py b/openrobot/api_wrapper/_async.py index 6384547..5c21e61 100644 --- a/openrobot/api_wrapper/_async.py +++ b/openrobot/api_wrapper/_async.py @@ -185,6 +185,182 @@ async def _request(self, method: str, url: str, **kwargs) -> typing.Union[dict, # Methods to query to API: + async def text_generation(self, text: str, *, max_length: typing.Optional[int] = None, num_return: typing.Optional[int] = 1) -> TextGenerationResult: + """|coro| + + Text Generation/Completion. This uses the /api/text-generation endpoint. + + Parameters + ---------- + text: :class:`str` + The text to be completed/generated. + max_length: Optional[:class:`int`] + The maximum length of the generated text. Defaults to ``None``. + num_return: Optional[:class:`int`] + The number of generated texts to return. Defaults to 1. + + Raises + ------ + :exc:`Forbidden` + API Returned a 403 HTTP Status Code. + :exc:`BadRequest` + API Returned a 400 HTTP Status Code. + :exc:`InternalServerError` + API Returned a 500 HTTP Status Code. + + Returns + ------- + :class:`TextGenerationResult` + The text generation result returned by the API. + """ + + js = await self._request('POST', '/api/text-generation', data={'text': text, 'max_length': max_length, 'num_return': num_return}) + return TextGenerationResult(js) + + async def text_generation_get(self, task_id: str) -> TextGenerationResult: + """|coro| + + Gets the status of a text generation task. This uses the /api/text-generation/{task_id} endpoint. + + Parameters + ---------- + task_id: :class:`str` + The task ID of the text generation task. + + Raises + ------ + :exc:`Forbidden` + API Returned a 403 HTTP Status Code. + :exc:`BadRequest` + API Returned a 400 HTTP Status Code. + :exc:`InternalServerError` + API Returned a 500 HTTP Status Code. + + Returns + ------- + :class:`TextGenerationResult` + The text generation result returned by the API. + """ + + js = await self._request('GET', f'/api/text-generation/{quote(task_id)}') + return TextGenerationResult(js) + + async def sentiment(self, text: str) -> SentimentResult: + """|coro| + + Performs a Sentiment check on a text. + + Parameters + ---------- + text: :class:`str` + The text to be checked. + + Raises + ------ + :exc:`Forbidden` + API Returned a 403 HTTP Status Code. + :exc:`BadRequest` + API Returned a 400 HTTP Status Code. + :exc:`InternalServerError` + API Returned a 500 HTTP Status Code. + + Returns + ------- + :class:`SentimentResult` + The sentiment result returned by the API. + """ + + js = await self._request('POST', '/api/sentiment', data={'text': text}) + return SentimentResult(js) + + async def sentiment_get(self, task_id: str) -> SentimentResult: + """|coro| + + Gets the status of a sentiment task. This uses the /api/sentiment/{task_id} endpoint. + + Parameters + ---------- + task_id: :class:`str` + The task ID of the sentiment task. + + Raises + ------ + :exc:`Forbidden` + API Returned a 403 HTTP Status Code. + :exc:`BadRequest` + API Returned a 400 HTTP Status Code. + :exc:`InternalServerError` + API Returned a 500 HTTP Status Code. + + Returns + ------- + :class:`SentimentResult` + The sentiment result returned by the API. + """ + + js = await self._request('GET', f'/api/sentiment/{quote(task_id)}') + return SentimentResult(js) + + async def summarization(self, text: str, *, max_length: typing.Optional[int] = None, min_length: typing.Optional[int] = 1) -> SummarizationResult: + """|coro| + + Summarizes a text. This uses the /api/summarization endpoint. + + Parameters + ---------- + text: :class:`str` + The text to be summarized. + max_length: Optional[:class:`int`] + The maximum length of the summary. Defaults to ``None``. + min_length: Optional[:class:`int`] + The minimum length of the summary. Defaults to ``None``. + + Raises + ------ + :exc:`Forbidden` + API Returned a 403 HTTP Status Code. + :exc:`BadRequest` + API Returned a 400 HTTP Status Code. + :exc:`InternalServerError` + API Returned a 500 HTTP Status Code. + + Returns + ------- + :class:`SummarizationResult` + The summarization result returned by the API. + """ + + js = await self._request('POST', '/api/summarization', data={'text': text, 'max_length': max_length, 'min_length': min_length}) + return SummarizationResult(js) + + async def summarization_get(self, task_id: str) -> SummarizationResult: + """|coro| + + Gets the status of a summarization task. This uses the /api/summarization/{task_id} endpoint. + + Parameters + ---------- + task_id: :class:`str` + The task ID of the summarization task. + + Raises + ------ + :exc:`Forbidden` + API Returned a 403 HTTP Status Code. + :exc:`BadRequest` + API Returned a 400 HTTP Status Code. + :exc:`InternalServerError` + API Returned a 500 HTTP Status Code. + + Returns + ------- + :class:`SummarizationResult` + The summarization result returned by the API. + """ + + js = await self._request('GET', f'/api/summarization/{quote(task_id)}') + return SummarizationResult(js) + async def lyrics(self, query: str) -> LyricResult: """|coro| diff --git a/openrobot/api_wrapper/_sync.py b/openrobot/api_wrapper/_sync.py index 7e322c1..e38e520 100644 --- a/openrobot/api_wrapper/_sync.py +++ b/openrobot/api_wrapper/_sync.py @@ -136,6 +136,176 @@ def _request(self, method: str, url: str, **kwargs): # Methods to query to API: + def text_generation(self, text: str, *, max_length: typing.Optional[int] = None, num_return: typing.Optional[int] = 1) -> TextGenerationResult: + """ + Text Generation/Completion. This uses the /api/text-generation endpoint. + + Parameters + ---------- + text: :class:`str` + The text to be completed/generated. + max_length: Optional[:class:`int`] + The maximum length of the generated text. Defaults to ``None``. + num_return: Optional[:class:`int`] + The number of generated texts to return. Defaults to 1. + + Raises + ------ + :exc:`Forbidden` + API Returned a 403 HTTP Status Code. + :exc:`BadRequest` + API Returned a 400 HTTP Status Code. + :exc:`InternalServerError` + API Returned a 500 HTTP Status Code. + + Returns + ------- + :class:`TextGenerationResult` + The text generation result returned by the API. + """ + + js = self._request('POST', '/api/text-generation', data={'text': text, 'max_length': max_length, 'num_return': num_return}) + return TextGenerationResult(js) + + def text_generation_get(self, task_id: str) -> TextGenerationResult: + """ + Gets the status of a text generation task. This uses the /api/text-generation/{task_id} endpoint. + + Parameters + ---------- + task_id: :class:`str` + The task ID of the text generation task. + + Raises + ------ + :exc:`Forbidden` + API Returned a 403 HTTP Status Code. + :exc:`BadRequest` + API Returned a 400 HTTP Status Code. + :exc:`InternalServerError` + API Returned a 500 HTTP Status Code. + + Returns + ------- + :class:`TextGenerationResult` + The text generation result returned by the API. + """ + + js = self._request('GET', f'/api/text-generation/{quote(task_id)}') + return TextGenerationResult(js) + + def sentiment(self, text: str) -> SentimentResult: + """ + Performs a Sentiment check on a text. + + Parameters + ---------- + text: :class:`str` + The text to be checked. + + Raises + ------ + :exc:`Forbidden` + API Returned a 403 HTTP Status Code. + :exc:`BadRequest` + API Returned a 400 HTTP Status Code. + :exc:`InternalServerError` + API Returned a 500 HTTP Status Code. + + Returns + ------- + :class:`SentimentResult` + The sentiment result returned by the API. + """ + + js = self._request('POST', '/api/sentiment', data={'text': text}) + return SentimentResult(js) + + def sentiment_get(self, task_id: str) -> SentimentResult: + """ + Gets the status of a sentiment task. This uses the /api/sentiment/{task_id} endpoint. + + Parameters + ---------- + task_id: :class:`str` + The task ID of the sentiment task. + + Raises + ------ + :exc:`Forbidden` + API Returned a 403 HTTP Status Code. + :exc:`BadRequest` + API Returned a 400 HTTP Status Code. + :exc:`InternalServerError` + API Returned a 500 HTTP Status Code. + + Returns + ------- + :class:`SentimentResult` + The sentiment result returned by the API. + """ + + js = self._request('GET', f'/api/sentiment/{quote(task_id)}') + return SentimentResult(js) + + def summarization(self, text: str, *, max_length: typing.Optional[int] = None, min_length: typing.Optional[int] = 1) -> SummarizationResult: + """ + Summarizes a text. This uses the /api/summarization endpoint. + + Parameters + ---------- + text: :class:`str` + The text to be summarized. + max_length: Optional[:class:`int`] + The maximum length of the summary. Defaults to ``None``. + min_length: Optional[:class:`int`] + The minimum length of the summary. Defaults to ``None``. + + Raises + ------ + :exc:`Forbidden` + API Returned a 403 HTTP Status Code. + :exc:`BadRequest` + API Returned a 400 HTTP Status Code. + :exc:`InternalServerError` + API Returned a 500 HTTP Status Code. + + Returns + ------- + :class:`SummarizationResult` + The summarization result returned by the API. + """ + + js = self._request('POST', '/api/summarization', data={'text': text, 'max_length': max_length, 'min_length': min_length}) + return SummarizationResult(js) + + def summarization_get(self, task_id: str) -> SummarizationResult: + """ + Gets the status of a summarization task. This uses the /api/summarization/{task_id} endpoint. + + Parameters + ---------- + task_id: :class:`str` + The task ID of the summarization task. + + Raises + ------ + :exc:`Forbidden` + API Returned a 403 HTTP Status Code. + :exc:`BadRequest` + API Returned a 400 HTTP Status Code. + :exc:`InternalServerError` + API Returned a 500 HTTP Status Code. + + Returns + ------- + :class:`SummarizationResult` + The summarization result returned by the API. + """ + + js = self._request('GET', f'/api/summarization/{quote(task_id)}') + return SummarizationResult(js) + def lyrics(self, query: str) -> LyricResult: """ Gets the lyrics from the API. diff --git a/openrobot/api_wrapper/results.py b/openrobot/api_wrapper/results.py index 2561149..2f43c85 100644 --- a/openrobot/api_wrapper/results.py +++ b/openrobot/api_wrapper/results.py @@ -13,6 +13,121 @@ class OpenRobotAPIBaseResult: def __init__(self, js): self.raw = js +class TextGenerationResult(OpenRobotAPIBaseResult): + """ + The result of the /api/text-generation endpoint. + + Attributes + ---------- + task_id: :class:`str` + The Task ID of the request. This is used to re-get + the result later. + text: :class:`str` + The original text passed. + max_length: :class:`int` + The maximum length of the generated text. + num_return: :class:`str` + The number of generated texts to return. + status: Literal["STARTED", "PENDING", "COMPLETED", "FAILED"] + The status of the request. + result: Optional[:class:`str`] + The generated text. This may be ``None`` if status is not + ``COMPLETED``. + timestamp: :class:`float` + The timestamp at which the request was made. + """ + + def __init__(self, js): + super().__init__(js) + + self.task_id: str = js["task_id"] + self.text: str = js["text"] + self.max_length: int = js["max_length"] + self.num_return: int = js["num_return"] + self.status: str = js["status"] + self.result: typing.Optional[str] = js["result"][0]["generated_text"] if js["result"] else None + self.timestamp: float = js["timestamp"] + +class SentimentResultReturned: + """ + The sentiment result. + + Attributes + ---------- + label: Literal["POSITIVE", "NEGATIVE"] + The label of the sentiment. + score: :class:`float` + The score of the sentiment. + """ + + def __init__(self, js): + self.label: str = js["label"] + self.score: float = js["score"] + +class SentimentResult(OpenRobotAPIBaseResult): + """ + The resullt of the /api/sentiment endpoint. + + Attributes + ---------- + task_id: :class:`str` + The Task ID of the request. This is used to re-get + the result later. + text: :class:`str` + The original text passed. + status: Literal["STARTED", "PENDING", "COMPLETED", "FAILED"] + The status of the request. + result: List[:class:`SentimentResultReturned`] + The sentiment result. This may be an empty list if status is not + ``COMPLETED``. + timestamp: :class:`float` + The timestamp at which the request was made. + """ + + def __init__(self, js): + super().__init__(js) + + self.task_id: str = js["task_id"] + self.text: str = js["text"] + self.status: str = js["status"] + self.result: typing.List[SentimentResultReturned] = [SentimentResultReturned(x) for x in js["result"]] + self.timestamp: float = js["timestamp"] + +class SummarizationResult(OpenRobotAPIBaseResult): + """ + The result of the /api/summarization endpoint. + + Attributes + ---------- + task_id: :class:`str` + The Task ID of the request. This is used to re-get + the result later. + text: :class:`str` + The original text passed. + max_length: :class:`int` + The maximum length of the generated text. + min_length: :class:`str` + The number of generated texts to return. + status: Literal["STARTED", "PENDING", "COMPLETED", "FAILED"] + The status of the request. + result: Optional[:class:`str`] + The generated text. This may be ``None`` if status is not + ``COMPLETED``. + timestamp: :class:`float` + The timestamp at which the request was made. + """ + + def __init__(self, js): + super().__init__(js) + + self.task_id: str = js["task_id"] + self.text: str = js["text"] + self.max_length: int = js["max_length"] + self.num_return: int = js["min_length"] + self.status: str = js["status"] + self.result: typing.Optional[str] = js["result"][0]["summary_text"] if js["result"] else None + self.timestamp: float = js["timestamp"] + class LyricImages: """ The Lyric's Track Images.