Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
fix: session to client in async
Browse files Browse the repository at this point in the history
  • Loading branch information
dsdanielpark committed Dec 28, 2023
1 parent ad6ae55 commit a38f26f
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions bardapi/core_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 (
Expand All @@ -670,15 +671,15 @@ 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)

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 +)
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit a38f26f

Please sign in to comment.