From a38f26fab19b986ff90cf55ffe98eca0d37943bb Mon Sep 17 00:00:00 2001 From: dsdanielpark Date: Thu, 28 Dec 2023 12:01:26 +0900 Subject: [PATCH] fix: session to client in async --- bardapi/core_async.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/bardapi/core_async.py b/bardapi/core_async.py index d7373b148..7e17e721b 100644 --- a/bardapi/core_async.py +++ b/bardapi/core_async.py @@ -44,7 +44,7 @@ def __init__( token: Optional[str] = None, timeout: int = 20, proxies: Optional[dict] = None, - session: Optional[AsyncClient] = None, + client: Optional[AsyncClient] = None, conversation_id: Optional[str] = None, google_translator_api_key: Optional[str] = None, language: Optional[str] = None, @@ -72,9 +72,7 @@ def __init__( self.conversation_id = conversation_id or "" self.response_id = "" self.choice_id = "" - self.client = self._get_client( - session - ) # Creating an httpx async client for asynchronous core code + self.client = self._get_client(client) # Creating an httpx async client for asynchronous core code self.language = language self.cookie_dict = {"__Secure-1PSID": self.token} self.run_code = run_code or False @@ -159,17 +157,18 @@ async def _get_client(self, session: Optional[AsyncClient]) -> AsyncClient: """ if session is None: async_client = AsyncClient( - http2=True, - headers=SESSION_HEADERS, - cookies={"__Secure-1PSID": self.token}, - timeout=self.timeout, - proxies=self.proxies, + http2=True, + headers=SESSION_HEADERS, + cookies={"__Secure-1PSID": self.token}, + timeout=self.timeout, + proxies=self.proxies, ) return async_client else: - assert type(session) == AsyncClient + assert type(session)==AsyncClient return session + async def get_answer(self, input_text: str) -> dict: """ Get an answer from the Bard API for the given input text. @@ -649,19 +648,21 @@ async def ask_about_image( translator_to_eng = GoogleTranslator(source="auto", target="en") # [Optional] Set language - if ( + if self.language is None or lang is None: + translated_input_text = input_text + elif ( (self.language is not None or lang 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") - transl_text = translator_to_eng.translate(input_text) + translated_input_text = translator_to_eng.translate(input_text) elif ( (self.language is not None or lang is not None) and self.language not in ALLOWED_LANGUAGES and self.google_translator_api_key is not None ): - transl_text = google_official_translator.translate( + translated_input_text = google_official_translator.translate( input_text, target_language="en" ) elif ( @@ -670,7 +671,7 @@ async def ask_about_image( and self.google_translator_api_key is None ): translator_to_eng = GoogleTranslator(source="auto", target="en") - transl_text = translator_to_eng.translate(input_text) + translated_input_text = translator_to_eng.translate(input_text) # Supported format: jpeg, png, webp image_url = upload_image(image) @@ -678,7 +679,7 @@ async def ask_about_image( input_data_struct = [ None, [ - [transl_text, 0, None, [[[image_url, 1], "uploaded_photo.jpg"]]], + [translated_input_text, 0, None, [[[image_url, 1], "uploaded_photo.jpg"]]], [lang if lang is not None else self.language], ["", "", ""], "", # Unknown random string value (1000 characters +) @@ -718,7 +719,9 @@ async def ask_about_image( parsed_answer = json.loads(resp_dict) content = parsed_answer[4][0][1][0] try: - if self.language is not None and self.google_translator_api_key is None: + if self.language is None and self.google_translator_api_key is None: + translated_content = content + elif self.language is not None and self.google_translator_api_key is None: translator = GoogleTranslator(source="en", target=self.language) translated_content = translator.translate(content)