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

Commit

Permalink
refactor: using def and constants for core.py
Browse files Browse the repository at this point in the history
  • Loading branch information
dsdanielpark committed Sep 22, 2023
1 parent 8f5f5e8 commit 18836b3
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 12 deletions.
26 changes: 24 additions & 2 deletions bardapi/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
TEXT_GENERATION_WEB_SERVER_PARAM = "boq_assistant-bard-web-server_20230912.07_p1"




SESSION_HEADERS = {
"Host": "bard.google.com",
"X-Same-Domain": "1",
Expand All @@ -32,3 +30,27 @@
"x-goog-upload-protocol": "resumable",
"x-tenant-id": "bard-storage",
}

REPLIT_SUPPORT_PROGRAM_LANGUAGES = {
"python": "main.py",
"javascript": "index.js",
"go": "main.go",
"java": "Main.java",
"kotlin": "Main.kt",
"php": "index.php",
"c#": "main.cs",
"swift": "main.swift",
"r": "main.r",
"ruby": "main.rb",
"c": "main.c",
"c++": "main.cpp",
"matlab": "main.m",
"typescript": "main.ts",
"scala": "main.scala",
"sql": "main.sql",
"html": "index.html",
"css": "style.css",
"nosql": "main.nosql",
"rust": "main.rs",
"perl": "main.pl",
}
28 changes: 21 additions & 7 deletions bardapi/core_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,22 @@
from httpx import AsyncClient
from deep_translator import GoogleTranslator
from google.cloud import translate_v2 as translate
from bardapi.constants import ALLOWED_LANGUAGES, SESSION_HEADERS
from bardapi.utils import extract_links, upload_image, extract_bard_cookie
from bardapi.constants import (
ALLOWED_LANGUAGES,
SESSION_HEADERS,
TEXT_GENERATION_WEB_SERVER_PARAM,
)
from bardapi.utils import (
extract_links,
build_input_replit_data_struct,
build_image_bard_answer,
build_export_data_structure,
build_bard_answer,
upload_image,
extract_bard_cookie,
build_input_text_struct,
build_input_image_struct,
)


class BardAsync:
Expand Down Expand Up @@ -153,7 +167,7 @@ async def get_answer(self, input_text: str) -> dict:
if not isinstance(self.SNlM0e, str):
self.SNlM0e = await self.SNlM0e
params = {
"bl": "boq_assistant-bard-web-server_20230419.00_p1",
"bl": TEXT_GENERATION_WEB_SERVER_PARAM,
"_reqid": str(self._reqid),
"rt": "c",
}
Expand Down Expand Up @@ -327,7 +341,7 @@ async def speech(self, input_text: str, lang: str = "en-US") -> dict:
}
"""
params = {
"bl": "boq_assistant-bard-web-server_20230419.00_p1",
"bl": TEXT_GENERATION_WEB_SERVER_PARAM,
"_reqid": str(self._reqid),
"rt": "c",
}
Expand Down Expand Up @@ -395,7 +409,7 @@ async def export_conversation(self, bard_answer, title: str = "") -> dict:
params = {
"rpcids": "fuVx7",
"source-path": "/",
"bl": "boq_assistant-bard-web-server_20230912.07_p1",
"bl": TEXT_GENERATION_WEB_SERVER_PARAM,
# '_reqid': str(self._reqid),
"rt": "c",
}
Expand Down Expand Up @@ -484,7 +498,7 @@ async def export_replit(
params = {
"rpcids": "qACoKe",
"source-path": kwargs.get("source_path", "/"),
"bl": "boq_assistant-bard-web-server_20230718.13_p2",
"bl": TEXT_GENERATION_WEB_SERVER_PARAM,
"_reqid": str(self._reqid),
"rt": "c",
}
Expand Down Expand Up @@ -644,7 +658,7 @@ async def ask_about_image(
],
]
params = {
"bl": "boq_assistant-bard-web-server_20230716.16_p2",
"bl": TEXT_GENERATION_WEB_SERVER_PARAM,
"_reqid": str(self._reqid),
"rt": "c",
}
Expand Down
123 changes: 120 additions & 3 deletions bardapi/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Util Functions for Async and Sync Core Cookie Modes
import uuid
import json
import requests
import browser_cookie3
from bardapi.constants import IMG_UPLOAD_HEADERS
Expand Down Expand Up @@ -169,7 +171,9 @@ def max_sentence(text: str, n: int):
print("".join(sentences).strip())


def create_input_text_struct(input_text, conversation_id, response_id, choice_id):
def build_input_text_struct(
input_text: str, conversation_id: int, response_id: int, choice_id: int
) -> dict:
return [
[input_text, 0, None, [], None, None, 0],
["en"],
Expand All @@ -182,5 +186,118 @@ def create_input_text_struct(input_text, conversation_id, response_id, choice_id
[],
[],
1,
1
]
1,
]


def build_input_image_struct(
transl_text: str, image_url: int, lang: str = None, default_language: str = "en"
) -> dict:
return [
None,
[
[transl_text, 0, None, [[[image_url, 1], "uploaded_photo.jpg"]]],
[lang if lang is not None else default_language],
["", "", ""],
"", # Unknown random string value (1000 characters +) - If needed, can replace with a random string generator
uuid.uuid4().hex, # Random uuidv4 (32 characters)
None,
[1],
0,
[],
[],
],
]


def build_input_replit_data_struct(instructions: str, code: str, filename: str) -> dict:
"""
Creates and returns the input_image_data_struct based on provided parameters.
:param instructions: The instruction text.
:param code: The code.
:param filename: The filename.
:return: The input_image_data_struct.
"""
return [
[
[
"qACoKe",
json.dumps([instructions, 5, code, [[filename, code]]]),
None,
"generic",
]
]
]


def build_bard_answer(
parsed_answer: list, images: str, program_lang: str, code: str, resp: dict
) -> dict:
return {
"content": parsed_answer[4][0][1][0],
"conversation_id": parsed_answer[1][0],
"response_id": parsed_answer[1][1],
"factuality_queries": parsed_answer[3],
"text_query": parsed_answer[2][0] if parsed_answer[2] else "",
"choices": [{"id": x[0], "content": x[1]} for x in parsed_answer[4]],
"links": extract_links(parsed_answer[4]),
"images": images,
"program_lang": program_lang,
"code": code,
"status_code": resp.status_code,
}


def build_export_data_structure(
conv_id: int, resp_id: int, choice_id: int, title: str
) -> dict:
return [
[
[
"fuVx7",
json.dumps(
[
[
None,
[
[
[conv_id, resp_id],
None,
None,
[[], [], [], choice_id, []],
]
],
[0, title],
]
]
),
None,
"generic",
]
]
]


def build_image_bard_answer(
translated_content: str, parsed_answer: list, resp: dict
) -> dict:
"""
For the image bard, unlike build_bard_answer which translates all choices,
this function is simplified since it only translates a single sentence.
"""

return {
"content": translated_content,
"conversation_id": parsed_answer[1][0],
"response_id": parsed_answer[1][1],
"factuality_queries": parsed_answer[3],
"text_query": parsed_answer[2][0] if parsed_answer[2] else "",
"choices": [{"id": x[0], "content": x[1]} for x in parsed_answer[4]],
"links": extract_links(parsed_answer[4]),
"images": [""],
"program_lang": "",
"code": "",
"status_code": resp.status_code,
}

0 comments on commit 18836b3

Please sign in to comment.