From ed6e05ae5e753fde645c17d5d70b62f1139de87e Mon Sep 17 00:00:00 2001 From: parkminwoo Date: Wed, 28 Jun 2023 03:13:39 +0900 Subject: [PATCH] chore: style black --- bardapi/core.py | 57 +++++++++++++++++++++++++++++++++---------- bardapi/core_async.py | 49 ++++++++++++++++++++++++++++++------- setup.py | 8 +++++- 3 files changed, 91 insertions(+), 23 deletions(-) diff --git a/bardapi/core.py b/bardapi/core.py index 9e3bde784..37fe2538a 100644 --- a/bardapi/core.py +++ b/bardapi/core.py @@ -43,7 +43,7 @@ def __init__( self.conversation_id = "" self.response_id = "" self.choice_id = "" - if conversation_id != None: + if conversation_id is not None: self.conversation_id = conversation_id # Set session if session is None: @@ -89,15 +89,27 @@ def get_answer(self, input_text: str) -> dict: "rt": "c", } if self.google_translator_api_key is not None: - google_official_translator = translate.Client(api_key=self.google_translator_api_key) + google_official_translator = translate.Client( + api_key=self.google_translator_api_key + ) # Set language (optional) - if self.language is not None and self.language not in ALLOWED_LANGUAGES and self.google_translator_api_key is None: + if ( + self.language is not None + and self.language not in ALLOWED_LANGUAGES + and self.google_translator_api_key is None + ): translator_to_eng = GoogleTranslator(source="auto", target="en") input_text = translator_to_eng.translate(input_text) - elif self.language is not None and self.language not in ALLOWED_LANGUAGES and self.google_translator_api_key is not None: - input_text = google_official_translator.translate(input_text, target_language='en') - + elif ( + self.language is not None + and self.language not in ALLOWED_LANGUAGES + and self.google_translator_api_key is not None + ): + input_text = google_official_translator.translate( + input_text, target_language="en" + ) + # Make post data structure and insert prompt input_text_struct = [ [input_text], @@ -139,13 +151,32 @@ def get_answer(self, input_text: str) -> dict: parsed_answer = json.loads(resp_dict) # Translated by Google Translator (optional) - ## Unofficial for testing - if self.language is not None and self.language not in ALLOWED_LANGUAGES and self.google_translator_api_key is None: + # Unofficial for testing + if ( + self.language is not None + and self.language not in ALLOWED_LANGUAGES + and self.google_translator_api_key is None + ): translator_to_lang = GoogleTranslator(source="auto", target=self.language) - parsed_answer[4] = [[x[0], [translator_to_lang.translate(x[1][0])]+x[1][1:], x[2]] for x in parsed_answer[4]] - ## Official Google Cloud Translation API - elif self.language is not None and self.language not in ALLOWED_LANGUAGES and self.google_translator_api_key is not None: - parsed_answer[4] = [[x[0], [google_official_translator(x[1][0], target_language=self.language)]+x[1][1:], x[2]] for x in parsed_answer[4]] + parsed_answer[4] = [ + [x[0], [translator_to_lang.translate(x[1][0])] + x[1][1:], x[2]] + for x in parsed_answer[4] + ] + # Official Google Cloud Translation API + elif ( + self.language is not None + and self.language not in ALLOWED_LANGUAGES + and self.google_translator_api_key is not None + ): + parsed_answer[4] = [ + [ + x[0], + [google_official_translator(x[1][0], target_language=self.language)] + + x[1][1:], + x[2], + ] + for x in parsed_answer[4] + ] # Get code try: @@ -177,7 +208,7 @@ def get_answer(self, input_text: str) -> dict: try: print(bard_answer["code"]) exec(bard_answer["code"]) - except Exception: + except: pass return bard_answer diff --git a/bardapi/core_async.py b/bardapi/core_async.py index a31c3516f..9ec9c0bbd 100644 --- a/bardapi/core_async.py +++ b/bardapi/core_async.py @@ -86,15 +86,27 @@ async def get_answer(self, input_text: str) -> dict: "rt": "c", } if self.google_translator_api_key is not None: - google_official_translator = translate.Client(api_key=self.google_translator_api_key) + google_official_translator = translate.Client( + api_key=self.google_translator_api_key + ) # Set language (optional) - if self.language is not None and self.language not in ALLOWED_LANGUAGES and self.google_translator_api_key is None: + if ( + self.language is not None + and self.language not in ALLOWED_LANGUAGES + and self.google_translator_api_key is None + ): translator_to_eng = GoogleTranslator(source="auto", target="en") input_text = translator_to_eng.translate(input_text) - elif self.language is not None and self.language not in ALLOWED_LANGUAGES and self.google_translator_api_key is not None: - input_text = google_official_translator.translate(input_text, target_language='en') - + elif ( + self.language is not None + and self.language not in ALLOWED_LANGUAGES + and self.google_translator_api_key is not None + ): + input_text = google_official_translator.translate( + input_text, target_language="en" + ) + # Make post data structure and insert prompt input_text_struct = [ [input_text], @@ -141,12 +153,31 @@ async def get_answer(self, input_text: str) -> dict: # Translated by Google Translator (optional) ## Unofficial for testing - if self.language is not None and self.language not in ALLOWED_LANGUAGES and self.google_translator_api_key is None: + if ( + self.language is not None + and self.language not in ALLOWED_LANGUAGES + and self.google_translator_api_key is None + ): translator_to_lang = GoogleTranslator(source="auto", target=self.language) - parsed_answer[4] = [[x[0], [translator_to_lang.translate(x[1][0])]+x[1][1:], x[2]] for x in parsed_answer[4]] + parsed_answer[4] = [ + [x[0], [translator_to_lang.translate(x[1][0])] + x[1][1:], x[2]] + for x in parsed_answer[4] + ] ## Official Google Cloud Translation API - elif self.language is not None and self.language not in ALLOWED_LANGUAGES and self.google_translator_api_key is not None: - parsed_answer[4] = [[x[0], [google_official_translator(x[1][0], target_language=self.language)]+x[1][1:], x[2]] for x in parsed_answer[4]] + elif ( + self.language is not None + and self.language not in ALLOWED_LANGUAGES + and self.google_translator_api_key is not None + ): + parsed_answer[4] = [ + [ + x[0], + [google_official_translator(x[1][0], target_language=self.language)] + + x[1][1:], + x[2], + ] + for x in parsed_answer[4] + ] # Get code try: diff --git a/setup.py b/setup.py index 88e213f38..b9ffa64b6 100644 --- a/setup.py +++ b/setup.py @@ -34,7 +34,13 @@ def get_long_description(): url="https://github.com/dsdanielpark/Bard-API", packages=find_packages(exclude=[]), python_requires=">=3.6", - install_requires=["requests", "deep_translator", "colorama", "httpx[http2]","google-cloud-translate==2.0.1"], + install_requires=[ + "requests", + "deep_translator", + "colorama", + "httpx[http2]", + "google-cloud-translate==2.0.1", + ], keywords="Python, API, Bard, Google Bard, Large Language Model, Chatbot API, Google API, Chatbot", classifiers=[ "Development Status :: 5 - Production/Stable",