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

Commit

Permalink
chore: style black
Browse files Browse the repository at this point in the history
  • Loading branch information
dsdanielpark committed Jun 27, 2023
1 parent 8445aa2 commit ed6e05a
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 23 deletions.
57 changes: 44 additions & 13 deletions bardapi/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
49 changes: 40 additions & 9 deletions bardapi/core_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit ed6e05a

Please sign in to comment.