From 8faa97d86fff72e7ea17b6fd9ac1ad4d33e6aedc Mon Sep 17 00:00:00 2001 From: CBoYXD Date: Wed, 9 Aug 2023 12:55:14 +0300 Subject: [PATCH 1/3] Add BardAsyncCookies to __init__.py --- bardapi/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bardapi/__init__.py b/bardapi/__init__.py index af3b03f21..e9587962c 100644 --- a/bardapi/__init__.py +++ b/bardapi/__init__.py @@ -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, @@ -29,6 +29,7 @@ "ChatBard", "BardAsync", "BardCookies", + "BardAsyncCookies", "SESSION_HEADERS", "ALLOWED_LANGUAGES", "DEFAULT_LANGUAGE", From b52d64d996463b53abfdddfa41ef21ae98e4d788 Mon Sep 17 00:00:00 2001 From: CBoYXD Date: Wed, 9 Aug 2023 14:01:26 +0300 Subject: [PATCH 2/3] Add status_code, fix doc strings --- bardapi/core.py | 50 ++++++++++++++++++++++++------ bardapi/core_async.py | 64 ++++++++++++++++++++++++++++----------- bardapi/core_cookies.py | 67 ++++++++++++++++++++++++++++++----------- 3 files changed, 135 insertions(+), 46 deletions(-) diff --git a/bardapi/core.py b/bardapi/core.py index b83736ead..5f9cb07ef 100644 --- a/bardapi/core.py +++ b/bardapi/core.py @@ -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 = { @@ -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"], @@ -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", @@ -288,7 +294,10 @@ 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 = ""): """ @@ -299,12 +308,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"] @@ -357,7 +371,11 @@ 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: """ @@ -385,7 +403,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: @@ -512,7 +532,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"], @@ -533,6 +555,7 @@ 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 @@ -540,7 +563,11 @@ 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", @@ -612,7 +639,10 @@ def export_replit( # increment request ID self._reqid += 100000 - return url + return { + "url": url, + "status_code": resp.status_code + } def _get_snim0e(self) -> str: """ diff --git a/bardapi/core_async.py b/bardapi/core_async.py index 6041e2163..12a8c497f 100644 --- a/bardapi/core_async.py +++ b/bardapi/core_async.py @@ -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']) @@ -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() @@ -218,7 +221,7 @@ async def get_answer(self, input_text: str) -> dict: "images": images, "langCode": langcode, "code": code, - "status_code": resp.status_code, + "status_code": resp.status_code } self.conversation_id, self.response_id, self.choice_id = ( @@ -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()) @@ -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", @@ -302,7 +308,10 @@ 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): """ @@ -341,11 +350,11 @@ 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()) @@ -353,7 +362,11 @@ async def export_conversation(self, bard_answer, title: str = "") -> str: 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"] @@ -406,7 +419,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 @@ -418,11 +434,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()) @@ -432,7 +448,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", @@ -502,7 +523,10 @@ 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 @@ -513,7 +537,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) @@ -537,7 +561,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() @@ -667,7 +693,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"], diff --git a/bardapi/core_cookies.py b/bardapi/core_cookies.py index c938d425a..9842eeb2f 100644 --- a/bardapi/core_cookies.py +++ b/bardapi/core_cookies.py @@ -85,7 +85,9 @@ def get_answer(self, input_text: str) -> dict: "choices": list, "links": list, "images": set, - "code": str + "langCode": str, + "code": str, + "status_code": int } """ return super().get_answer(input_text) @@ -102,15 +104,18 @@ def speech(self, input_text: str, lang="en-US") -> dict: >>> bard = BardCookies(cookie_dict=cookies) >>> 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 + } """ return super().speech(input_text, lang) @@ -142,7 +147,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 } """ return super().ask_about_image(input_text, image, lang) @@ -159,13 +166,17 @@ def export_conversation(self, bard_answer, title: str = ""): >>> bard = BardCookies(cookie_dict=cookies) >>> bard_answer = bard.get_answer("hello!") >>> url = bard.export_conversation(bard_answer, title="Export Conversation") - >>> print(url) + >>> 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 + } """ return super().export_conversation(bard_answer, title) @@ -183,7 +194,7 @@ def export_replit( >>> bard = BardCookies(cookie_dict=cookies) >>> bard_answer = bard.get_answer("code python to print hello world") >>> url = bard.export_replit(bard_answer['code'], bard_answer['langCode']) - >>> print(url) + >>> print(url['url']) Args: code (str): source code @@ -191,7 +202,11 @@ 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 + } """ return super().export_replit(code, langcode, filename, **kwargs) @@ -295,7 +310,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 } """ return await super().get_answer(input_text) @@ -315,7 +333,7 @@ async def speech(self, input_text: str, lang: str = "en-US") -> dict: >>> bard = BardAsyncCookies(cookie_dict=cookies) >>> audio = await bard.speech("Hello") >>> with open("bard.ogg", "wb") as f: - >>> f.write(bytes(audio)) + >>> f.write(bytes(audio['audio'])) >>> >>> asyncio.run(main()) @@ -324,8 +342,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 + } """ return await super().speech(input_text, lang) @@ -367,7 +388,9 @@ async def ask_about_image( "choices": list, "links": list, "images": set, - "code": str + "langCode": str, + "code": str, + "status_code": int } """ return await super().ask_about_image(input_text, image, lang) @@ -387,7 +410,7 @@ async def export_conversation(self, bard_answer, title: str = "") -> str: >>> bard = BardAsyncCookies(cookie_dict=cookies) >>> 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()) @@ -395,7 +418,11 @@ async def export_conversation(self, bard_answer, title: str = "") -> str: 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 + } """ return await super().export_conversation(bard_answer, title) @@ -416,7 +443,7 @@ async def export_replit( >>> bard = BardAsyncCookies(cookie_dict=cookies) >>> 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()) @@ -426,7 +453,11 @@ 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 + } """ return await super().export_replit(code, langcode, filename, **kwargs) From 065a6c0251db40b61b198c51555e9c22a1327592 Mon Sep 17 00:00:00 2001 From: CBoYXD Date: Wed, 9 Aug 2023 14:05:14 +0300 Subject: [PATCH 3/3] Style black --- bardapi/core.py | 20 +++++--------------- bardapi/core_async.py | 18 ++++++------------ 2 files changed, 11 insertions(+), 27 deletions(-) diff --git a/bardapi/core.py b/bardapi/core.py index 5f9cb07ef..264e78652 100644 --- a/bardapi/core.py +++ b/bardapi/core.py @@ -218,7 +218,7 @@ def get_answer(self, input_text: str) -> dict: "images": images, "langCode": langcode, "code": code, - "status_code": resp.status_code + "status_code": resp.status_code, } self.conversation_id, self.response_id, self.choice_id = ( bard_answer["conversation_id"], @@ -294,10 +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": audio_bytes, - "status_code": resp.status_code - } + return {"audio": audio_bytes, "status_code": resp.status_code} def export_conversation(self, bard_answer, title: str = ""): """ @@ -371,11 +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": url, - "status_code": resp.status_code - } - + return {"url": url, "status_code": resp.status_code} def ask_about_image(self, input_text: str, image: bytes, lang: str = None) -> dict: """ @@ -534,7 +527,7 @@ def ask_about_image(self, input_text: str, image: bytes, lang: str = None) -> di "images": [""], "langCode": "", "code": "", - "status_code": resp.status_code + "status_code": resp.status_code, } self.conversation_id, self.response_id, self.choice_id = ( bard_answer["conversation_id"], @@ -639,10 +632,7 @@ def export_replit( # increment request ID self._reqid += 100000 - return { - "url": url, - "status_code": resp.status_code - } + return {"url": url, "status_code": resp.status_code} def _get_snim0e(self) -> str: """ diff --git a/bardapi/core_async.py b/bardapi/core_async.py index 12a8c497f..cf85e1d36 100644 --- a/bardapi/core_async.py +++ b/bardapi/core_async.py @@ -96,7 +96,7 @@ async def get_answer(self, input_text: str) -> dict: "links": list "images": set, "langCode": str, - "code": str, + "code": str, "status_code": int } """ @@ -221,7 +221,7 @@ async def get_answer(self, input_text: str) -> dict: "images": images, "langCode": langcode, "code": code, - "status_code": resp.status_code + "status_code": resp.status_code, } self.conversation_id, self.response_id, self.choice_id = ( @@ -308,10 +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": audio_bytes, - "status_code": resp.status_code - } + return {"audio": audio_bytes, "status_code": resp.status_code} async def _get_snim0e(self): """ @@ -453,7 +450,7 @@ async def export_replit( "url": str, "status_code": int } - + """ params = { "rpcids": "qACoKe", @@ -523,10 +520,7 @@ async def export_replit( # increment request ID self._reqid += 100000 - return { - "url": url, - "status_code": resp.status_code - } + return {"url": url, "status_code": resp.status_code} async def ask_about_image( self, input_text: str, image: bytes, lang: str = None @@ -695,7 +689,7 @@ async def ask_about_image( "images": [""], "langCode": "", "code": "", - "status_code": resp.status_code + "status_code": resp.status_code, } self.conversation_id, self.response_id, self.choice_id = ( bard_answer["conversation_id"],