-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding BardAPI to give names for lessons by images
- Loading branch information
1 parent
7279c0d
commit 8763213
Showing
2 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import os\n", | ||
"from notion_client import Client\n", | ||
"\n", | ||
"def get_image_urls(page_id):\n", | ||
" try:\n", | ||
" # Initialize Notion client\n", | ||
" notion = Client(auth=os.environ.get(\"NOTION_TOKEN\"))\n", | ||
"\n", | ||
" # Retrieve the page\n", | ||
" page = notion.pages.retrieve(page_id)\n", | ||
"\n", | ||
" # Get list of image URLs\n", | ||
" image_urls = []\n", | ||
" children = notion.blocks.children.list(block_id=page_id).get(\"results\")\n", | ||
" for child in children:\n", | ||
" if child[\"type\"] == \"image\":\n", | ||
" if child[\"image\"].get(\"type\") == \"external\":\n", | ||
" image_urls.append(child[\"image\"][\"external\"][\"url\"])\n", | ||
" elif child[\"image\"].get(\"type\") == \"file\":\n", | ||
" image_urls.append(child[\"image\"][\"file\"][\"url\"])\n", | ||
" elif child['type'] == \"embed\":\n", | ||
" image_urls.append(child[\"embed\"]['url'])\n", | ||
" return image_urls\n", | ||
"\n", | ||
" except Exception as e:\n", | ||
" # Handle API limit or other exceptions here\n", | ||
" print(f\"An error occurred: {str(e)}\")\n", | ||
" return []\n", | ||
"\n", | ||
"def update_ai_name(self, page_id, new_name):\n", | ||
" logging.info(f\"Updating 'AiName' for page {page_id}...\")\n", | ||
" update_properties = {\n", | ||
" \"AiName\": {\n", | ||
" \"title\": [\n", | ||
" {\n", | ||
" \"text\": {\n", | ||
" \"content\": new_name\n", | ||
" }\n", | ||
" }\n", | ||
" ]\n", | ||
" }\n", | ||
" }\n", | ||
" response = self.notion.pages.update(page_id, properties=update_properties)\n", | ||
" if response:\n", | ||
" logging.info(f\"Successfully updated 'AiName' for page {page_id}!\")\n", | ||
" else:\n", | ||
" logging.info(f\"Failed to update 'AiName' for page {page_id}.\")\n", | ||
"\n", | ||
"import os\n", | ||
"import requests\n", | ||
"import logging\n", | ||
"from PIL import Image\n", | ||
"from io import BytesIO\n", | ||
"from bardapi import Bard\n", | ||
"\n", | ||
"# Set up logging configuration\n", | ||
"logging.basicConfig(\n", | ||
" level=logging.INFO, format=\"%(asctime)s - %(levelname)s - %(message)s\"\n", | ||
")\n", | ||
"logger = logging.getLogger(__name__)\n", | ||
"\n", | ||
"\n", | ||
"def process_images(images_urls):\n", | ||
" # Configure API key\n", | ||
" api_key = \"ZAiB9lhD1HnhRZFS3ieaM_QCtIwC-BRO4B4HcRGakEKl1tEo3sW9Ir4o8ZL0YK81OQWsRQ.\" # Replace with your actual API key\n", | ||
" os.environ[\"_BARD_API_KEY\"] = api_key\n", | ||
"\n", | ||
" # Initialize the Bard API and session\n", | ||
" session = requests.Session()\n", | ||
" session.headers = {\n", | ||
" \"Host\": \"bard.google.com\",\n", | ||
" \"X-Same-Domain\": \"1\",\n", | ||
" \"User-Agent\": \"Mozilla/5.0 (Windows NT 11.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36\",\n", | ||
" \"Content-Type\": \"application/x-www-form-urlencoded;charset=UTF-8\",\n", | ||
" \"Origin\": \"https://bard.google.com\",\n", | ||
" \"Referer\": \"https://bard.google.com/\",\n", | ||
" }\n", | ||
" session.cookies.set(\"__Secure-1PSID\", os.getenv(\"_BARD_API_KEY\"))\n", | ||
"\n", | ||
" bard = Bard(token=api_key, session=session, timeout=30)\n", | ||
" for index, url in enumerate(images_urls):\n", | ||
" response = requests.get(url)\n", | ||
" with open(f\"images/temp_{index}.jpg\", \"wb\") as f:\n", | ||
" f.write(response.content)\n", | ||
" image_path = f\"images/temp_{index}.jpg\"\n", | ||
" logger.info(f\"Downloaded image: {image_path}\")\n", | ||
"\n", | ||
" with open(image_path, \"rb\") as image_file:\n", | ||
" img = image_file.read()\n", | ||
" response = bard.ask_about_image(\n", | ||
" f\"Screenshot {index} Rule 1: max sentences = 3 Rule 2: Output must contain only the name of the topic Rule 3: Output must be in Ukrainian. Rule 4: Topic must be bold.\",\n", | ||
" img,\n", | ||
" )[\"content\"]\n", | ||
" logger.info(f\"Bard API response for image {index}: {response}\")\n", | ||
"\n", | ||
" # Now that we have processed all images, we can use the Bard API to get the short description\n", | ||
" response = bard.get_answer(\n", | ||
" \"So can you give a short description about this lesson? Rule 1: max sentences = 1 Rule 2: Output must contain only the name of the topic Rule 3: Output must be in Ukrainian. Rule 4: Topic must be bold.\"\n", | ||
" )[\"content\"]\n", | ||
" response = response[response.index(\"**\") + 2 : response.rindex(\"**\")]\n", | ||
" logger.info(f\"Short description generated: {response}\")\n", | ||
" return response\n" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.3" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import os | ||
import requests | ||
import logging | ||
from PIL import Image | ||
from io import BytesIO | ||
from notion_client import Client | ||
from bardapi import Bard | ||
from ProcessNotion import * | ||
|
||
class NotionImageProcessor: | ||
def __init__(self, notion_token, bard_api_key, database_id): | ||
logging.info("Initializing NotionImageProcessor...") | ||
self.notion = Client(auth=notion_token) | ||
self.bard_api_key = bard_api_key | ||
self.bard = Bard( | ||
token=self.bard_api_key, session=self._init_bard_session(), timeout=30 | ||
) | ||
self.database_id = database_id | ||
self.image_dir = os.path.join(os.getcwd(), 'images') | ||
|
||
def load_database(self): | ||
# logging.info("Downloading database...") | ||
# DW = NotionDatabaseDW(self.notion, self.database_id) | ||
# results = DW.query_database() | ||
# df = DW.extract_data_and_export_to_csv(results) | ||
# df = DW.preprocess_df(df) | ||
# logging.info("Database downloaded and processed.") | ||
df = pd.read_csv('resources/local_database.csv') | ||
return df | ||
|
||
def _init_bard_session(self): | ||
logging.info("Initializing Bard session...") | ||
session = requests.Session() | ||
session.headers = { | ||
"Host": "bard.google.com", | ||
"X-Same-Domain": "1", | ||
"User-Agent": "Mozilla/5.0 (Windows NT 11.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36", | ||
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", | ||
"Origin": "https://bard.google.com", | ||
"Referer": "https://bard.google.com/", | ||
} | ||
session.cookies.set("__Secure-1PSID", self.bard_api_key) | ||
logging.info("Bard session initialized.") | ||
return session | ||
|
||
def get_image_urls(self, page_id): | ||
logging.info(f"Retrieving image URLs for page {page_id}...") | ||
try: | ||
page = self.notion.pages.retrieve(page_id) | ||
image_urls = [] | ||
children = self.notion.blocks.children.list(block_id=page_id).get("results") | ||
for child in children: | ||
if child["type"] == "image": | ||
if child["image"].get("type") == "external": | ||
image_urls.append(child["image"]["external"]["url"]) | ||
elif child["image"].get("type") == "file": | ||
image_urls.append(child["image"]["file"]["url"]) | ||
elif child["type"] == "embed": | ||
image_urls.append(child["embed"]["url"]) | ||
logging.info(f"Retrieved {len(image_urls)} image URLs.") | ||
return image_urls | ||
except Exception as e: | ||
logging.error(f"An error occurred while retrieving image URLs: {str(e)}") | ||
return [] | ||
|
||
def update_ai_name(self, page_id, new_name): | ||
logging.info(f"Updating 'AiName' for page {page_id}...") | ||
update_properties = {"AiName": {"rich_text": [{"text": {"content": new_name}}]}} | ||
response = self.notion.pages.update(page_id, properties=update_properties) | ||
if response: | ||
logging.info(f"Successfully updated 'AiName' for page {page_id}!") | ||
else: | ||
logging.error(f"Failed to update 'AiName' for page {page_id}.") | ||
|
||
def download_image(self, url, index): | ||
try: | ||
response = requests.get(url) | ||
response.raise_for_status() | ||
except requests.exceptions.HTTPError as errh: | ||
logging.error(f"HTTP Error occurred: {errh}") | ||
return None | ||
except requests.exceptions.ConnectionError as errc: | ||
logging.error(f"Error Connecting: {errc}") | ||
return None | ||
except requests.exceptions.Timeout as errt: | ||
logging.error(f"Timeout Error: {errt}") | ||
return None | ||
except requests.exceptions.RequestException as err: | ||
logging.error(f"Something went wrong: {err}") | ||
return None | ||
|
||
image_path = os.path.join(self.image_dir, f"temp_{index}.jpg") | ||
with open(image_path, "wb") as f: | ||
f.write(response.content) | ||
logging.info(f"Downloaded image: {image_path}") | ||
return image_path | ||
|
||
def process_image(self, image_path, index): | ||
with open(image_path, "rb") as image_file: | ||
img = image_file.read() | ||
response = self.bard.ask_about_image( | ||
f"Screenshot {index} Rule 1: max sentences = 3 Rule 2: Output must contain only the name of the topic Rule 3: Output must be in Ukrainian. Rule 4: Topic must be bold.", | ||
img, | ||
)["content"] | ||
logging.info(f"Bard API response for image {index}") | ||
return response | ||
|
||
def process_images(self, images_urls, topic ): | ||
for index, url in enumerate(images_urls): | ||
logging.info(f"Processing image {index}...") | ||
image_path = self.download_image(url, index) | ||
if image_path: | ||
self.process_image(image_path, index) | ||
|
||
response = self.bard.get_answer( | ||
f"So can you give a short description about this lesson? Rule 1: max sentences = 10. Rule 2: Topic must be related or to big topic called {topic}. Rule 3: Output must be in Ukrainian. Rule 4: Topic must be bold like this '**[topic]**'. Rule 5: Topic name must be convinient with the all of the {len(images_urls)} images.", | ||
)["content"] | ||
if '**' in response: | ||
response = response[response.index("**") + 2 : ] | ||
response = response[:response.index("**")] | ||
logging.info(f"Short description generated: {response}") | ||
return response | ||
else: | ||
logging.info(f"No short description generated.") | ||
return response | ||
|
||
if __name__ == "__main__": | ||
logging.basicConfig(level=logging.INFO) | ||
notion_token = os.getenv('NOTION_TOKEN') | ||
bard_api_key = os.getenv('BARD_API_KEY') | ||
database_id = os.getenv('DATABASE_ID') | ||
|
||
NIP = NotionImageProcessor( | ||
notion_token, bard_api_key, database_id | ||
) | ||
|
||
df = NIP.load_database() | ||
ids = df['id'].tolist() | ||
topics = df['Тема'].tolist() | ||
for index, (id, topic) in enumerate(zip(ids, topics)): | ||
images_urls = NIP.get_image_urls(id) | ||
response = NIP.process_images(images_urls, topic) | ||
NIP.update_ai_name(id, response) |