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

Add BardAsyncCookies to init, add status_code, fix doc strings #159

Merged
merged 4 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bardapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
IMG_UPLOAD_HEADERS,
)
from bardapi.core_async import BardAsync
from bardapi.core_cookies import BardCookies
from bardapi.core_cookies import BardCookies, BardAsyncCookies
from bardapi.utils import (
extract_links,
upload_image,
Expand All @@ -29,6 +29,7 @@
"ChatBard",
"BardAsync",
"BardCookies",
"BardAsyncCookies",
"SESSION_HEADERS",
"ALLOWED_LANGUAGES",
"DEFAULT_LANGUAGE",
Expand Down
40 changes: 30 additions & 10 deletions bardapi/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ def get_answer(self, input_text: str) -> dict:
"choices": list,
"links": list,
"images": set,
"code": str
"langCode": str,
"code": str,
"status_code": int
}
"""
params = {
Expand Down Expand Up @@ -216,6 +218,7 @@ def get_answer(self, input_text: str) -> dict:
"images": images,
"langCode": langcode,
"code": code,
"status_code": resp.status_code,
}
self.conversation_id, self.response_id, self.choice_id = (
bard_answer["conversation_id"],
Expand Down Expand Up @@ -243,15 +246,18 @@ def speech(self, input_text: str, lang="en-US") -> dict:
>>> bard = Bard(token=token)
>>> audio = bard.speech("hello!")
>>> with open("bard.ogg", "wb") as f:
>>> f.write(bytes(audio))
>>> f.write(bytes(audio['audio']))

Args:
input_text (str): Input text for the query.
lang (str): Input language for the query.

Returns:
bytes: audio in bytes format
with format of audio/ogg
dict: Answer from the Bard API in the following format:
{
"audio": bytes,
"status_code": int
}
"""
params = {
"bl": "boq_assistant-bard-web-server_20230713.13_p0",
Expand Down Expand Up @@ -288,7 +294,7 @@ def speech(self, input_text: str, lang="en-US") -> dict:
resp_json = json.loads(resp_dict)
audio_b64 = resp_json[0]
audio_bytes = base64.b64decode(audio_b64)
return audio_bytes
return {"audio": audio_bytes, "status_code": resp.status_code}

def export_conversation(self, bard_answer, title: str = ""):
"""
Expand All @@ -299,12 +305,17 @@ def export_conversation(self, bard_answer, title: str = ""):
>>> bard = Bard(token=token)
>>> bard_answer = bard.get_answer("hello!")
>>> url = bard.export_conversation(bard_answer, title="Export Conversation")
>>> print(url['url'])

Args:
bard_answer (dict): bard_answer returned from get_answer
title (str): Title for URL
Returns:
string: public URL you can share
dict: Answer from the Bard API in the following format:
{
"url": str,
"status_code": int
}
"""
conv_id = bard_answer["conversation_id"]
resp_id = bard_answer["response_id"]
Expand Down Expand Up @@ -357,7 +368,7 @@ def export_conversation(self, bard_answer, title: str = ""):
url = f"https://g.co/bard/share/{url_id}"
# Increment request ID
self._reqid += 100000
return url
return {"url": url, "status_code": resp.status_code}

def ask_about_image(self, input_text: str, image: bytes, lang: str = None) -> dict:
"""
Expand Down Expand Up @@ -385,7 +396,9 @@ def ask_about_image(self, input_text: str, image: bytes, lang: str = None) -> di
"choices": list,
"links": list,
"images": set,
"code": str
"langCode": str,
"code": str,
"status_code": int
}
"""
if self.google_translator_api_key is not None:
Expand Down Expand Up @@ -512,7 +525,9 @@ def ask_about_image(self, input_text: str, image: bytes, lang: str = None) -> di
"choices": [{"id": x[0], "content": x[1]} for x in parsed_answer[4]],
"links": extract_links(parsed_answer[4]),
"images": [""],
"langCode": "",
"code": "",
"status_code": resp.status_code,
}
self.conversation_id, self.response_id, self.choice_id = (
bard_answer["conversation_id"],
Expand All @@ -533,14 +548,19 @@ def export_replit(
>>> bard = Bard(token=token)
>>> bard_answer = bard.get_answer("code python to print hello world")
>>> url = bard.export_replit(bard_answer['code'], bard_answer['langCode'])
>>> print(url['url'])

Args:
code (str): source code
langcode (str): code language
filename (str): filename for code language
**kwargs: instructions, source_path
Returns:
string: export URL to create repl
dict: Answer from the Bard API in the following format:
{
"url": str,
"status_code": int
}
"""
params = {
"rpcids": "qACoKe",
Expand Down Expand Up @@ -612,7 +632,7 @@ def export_replit(
# increment request ID
self._reqid += 100000

return url
return {"url": url, "status_code": resp.status_code}

def _get_snim0e(self) -> str:
"""
Expand Down
56 changes: 39 additions & 17 deletions bardapi/core_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async def get_answer(self, input_text: str) -> dict:
>>> import asyncio
>>>
>>> async def main():
>>> token = 'xxxxxxxxxx'
>>> token = 'xxxxxx'
>>> bard = BardAsync(token=token)
>>> response = await bard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")
>>> print(response['content'])
Expand All @@ -94,7 +94,10 @@ async def get_answer(self, input_text: str) -> dict:
"textQuery": str,
"choices": list,
"links": list
"images": set
"images": set,
"langCode": str,
"code": str,
"status_code": int
}
"""
self.SNlM0e = await self._get_snim0e()
Expand Down Expand Up @@ -252,11 +255,11 @@ async def speech(self, input_text: str, lang: str = "en-US") -> dict:
>>> import asyncio
>>>
>>> async def main():
>>> token = 'xxxxxxxxxx'
>>> token = 'xxxxxx'
>>> bard = BardAsync(token=token)
>>> audio = await bard.speech("Hello")
>>> with open("bard.ogg", "wb") as f:
>>> f.write(bytes(audio))
>>> f.write(bytes(audio['audio']))
>>>
>>> asyncio.run(main())

Expand All @@ -265,8 +268,11 @@ async def speech(self, input_text: str, lang: str = "en-US") -> dict:
lang (str): Input language for the query

Returns:
bytes: audio in bytes format
with format of audio/ogg
dict: Answer from the Bard API in the following format:
{
"audio": bytes,
"status_code": int
}
"""
params = {
"bl": "boq_assistant-bard-web-server_20230419.00_p1",
Expand Down Expand Up @@ -302,7 +308,7 @@ async def speech(self, input_text: str, lang: str = "en-US") -> dict:
resp_json = json.loads(resp_dict)
audio_b64 = resp_json[0]
audio_bytes = base64.b64decode(audio_b64)
return audio_bytes
return {"audio": audio_bytes, "status_code": resp.status_code}

async def _get_snim0e(self):
"""
Expand Down Expand Up @@ -341,19 +347,23 @@ async def export_conversation(self, bard_answer, title: str = "") -> str:
>>> import asyncio
>>>
>>> async def main():
>>> token = 'xxxxxxxxxx'
>>> token = 'xxxxxx'
>>> bard = BardAsync(token=token)
>>> bard_answer = await bard.get_answer("hello!")
>>> url = await bard.export_conversation(bard_answer, title="Export Conversation")
>>> print(url)
>>> print(url['url'])
>>>
>>> asyncio.run(main())

Args:
bard_answer (dict): bard_answer returned from get_answer
title (str): Title for URL
Returns:
string: public URL you can share
dict: Answer from the Bard API in the following format:
{
"url": str,
"status_code": int
}
"""
conv_id = bard_answer["conversation_id"]
resp_id = bard_answer["response_id"]
Expand Down Expand Up @@ -406,7 +416,10 @@ async def export_conversation(self, bard_answer, title: str = "") -> str:
url = f"https://g.co/bard/share/{url_id}"
# Increment request ID
self._reqid += 100000
return url
return {
"url": url,
"status_code": resp.status_code,
}

async def export_replit(
self, code: str, langcode: str = None, filename: str = None, **kwargs
Expand All @@ -418,11 +431,11 @@ async def export_replit(
>>> import asyncio
>>>
>>> async def main():
>>> token = 'xxxxxxxxxx'
>>> token = 'xxxxxx'
>>> bard = BardAsync(token=token)
>>> bard_answer = await bard.get_answer("code python to print hello world")
>>> url = await bard.export_replit(bard_answer['code'], bard_answer['langCode'])
>>> print(url)
>>> print(url['url'])
>>>
>>> asyncio.run(main())

Expand All @@ -432,7 +445,12 @@ async def export_replit(
filename (str): filename for code language
**kwargs: instructions, source_path
Returns:
string: export URL to create repl
dict: Answer from the Bard API in the following format:
{
"url": str,
"status_code": int
}

"""
params = {
"rpcids": "qACoKe",
Expand Down Expand Up @@ -502,7 +520,7 @@ async def export_replit(
# increment request ID
self._reqid += 100000

return url
return {"url": url, "status_code": resp.status_code}

async def ask_about_image(
self, input_text: str, image: bytes, lang: str = None
Expand All @@ -513,7 +531,7 @@ async def ask_about_image(
>>> import asyncio
>>>
>>> async def main():
>>> token = 'xxxxxxxxxx'
>>> token = 'xxxxxx'
>>> bard = BardAsync(token=token)
>>> image = open('image.jpg', 'rb').read()
>>> bard_answer = await bard.ask_about_image("what is in the image?", image)
Expand All @@ -537,7 +555,9 @@ async def ask_about_image(
"choices": list,
"links": list,
"images": set,
"code": str
"langCode": str,
"code": str,
"status_code": int
}
"""
self.SNlM0e = await self._get_snim0e()
Expand Down Expand Up @@ -667,7 +687,9 @@ async def ask_about_image(
"choices": [{"id": x[0], "content": x[1]} for x in parsed_answer[4]],
"links": extract_links(parsed_answer[4]),
"images": [""],
"langCode": "",
"code": "",
"status_code": resp.status_code,
}
self.conversation_id, self.response_id, self.choice_id = (
bard_answer["conversation_id"],
Expand Down
Loading