Use llm to generate funny tweet on image alt-text
This commit is contained in:
103
week1/day1.ipynb
103
week1/day1.ipynb
@@ -159,8 +159,8 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"openai = OpenAI()\n",
|
||||
"\n",
|
||||
"import httpx\n",
|
||||
"openai = OpenAI(http_client=httpx.Client(verify=False))\n",
|
||||
"# If this doesn't work, try Kernel menu >> Restart Kernel and Clear Outputs Of All Cells, then run the cells from the top of this notebook down.\n",
|
||||
"# If it STILL doesn't work (horrors!) then please see the Troubleshooting notebook in this folder for full instructions"
|
||||
]
|
||||
@@ -217,7 +217,8 @@
|
||||
" Create this Website object from the given url using the BeautifulSoup library\n",
|
||||
" \"\"\"\n",
|
||||
" self.url = url\n",
|
||||
" response = requests.get(url, headers=headers)\n",
|
||||
" requests.packages.urllib3.disable_warnings()\n",
|
||||
" response = requests.get(url, headers=headers, verify=False)\n",
|
||||
" soup = BeautifulSoup(response.content, 'html.parser')\n",
|
||||
" self.title = soup.title.string if soup.title else \"No title found\"\n",
|
||||
" for irrelevant in soup.body([\"script\", \"style\", \"img\", \"input\"]):\n",
|
||||
@@ -233,8 +234,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Let's try one out. Change the website and add print statements to follow along.\n",
|
||||
"\n",
|
||||
"ed = Website(\"https://edwarddonner.com\")\n",
|
||||
"ed = Website(\"http://edwarddonner.com\")\n",
|
||||
"print(ed.title)\n",
|
||||
"print(ed.text)"
|
||||
]
|
||||
@@ -434,12 +434,24 @@
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3018853a-445f-41ff-9560-d925d1774b2f",
|
||||
"metadata": {},
|
||||
"metadata": {
|
||||
"scrolled": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"display_summary(\"https://edwarddonner.com\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f8a34db6-9c2f-4f5e-95b4-62090d7b591b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"display_summary(\"https://openai.com\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b3bcf6f4-adce-45e9-97ad-d9a5d7a3a624",
|
||||
@@ -470,7 +482,9 @@
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "75e9fd40-b354-4341-991e-863ef2e59db7",
|
||||
"metadata": {},
|
||||
"metadata": {
|
||||
"scrolled": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"display_summary(\"https://anthropic.com\")"
|
||||
@@ -510,30 +524,77 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 25,
|
||||
"id": "00743dac-0e70-45b7-879a-d7293a6f68a6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"Here's a markdown layout featuring tables for each image with a funny tweet alongside it:\n",
|
||||
"\n",
|
||||
"```markdown\n",
|
||||
"| Image | Funny Tweet |\n",
|
||||
"|------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------|\n",
|
||||
"|  | \"When you want to feel like a rebel, but your bike is still in the shop. 🏍️😂\" |\n",
|
||||
"|  | \"When the squad is finally ready to leave the party but you can't find your keys. 🕊️🤣\" |\n",
|
||||
"|  | \"When you’re trying to chill at the beach, but your buddy won’t stop splashing you. 🦭💦\" |\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"Feel free to use or modify the layout and the tweets as you see fit!"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Step 1: Create your prompts\n",
|
||||
"# A small exercise to feed the llm with image alt text and return a funny tweet.\n",
|
||||
"\n",
|
||||
"# Step 1: Create your prompts\n",
|
||||
"import json\n",
|
||||
"system_prompt = \"You are a meme lord. You like tweeting funny and hilarious comments on images. To understand the image you would be given alt text on the image.\"\n",
|
||||
"class website:\n",
|
||||
" def __init__(self,url):\n",
|
||||
" self.url = url\n",
|
||||
" requests.packages.urllib3.disable_warnings()\n",
|
||||
" response = requests.get(url, headers=headers, verify=False)\n",
|
||||
" html_content = response.content\n",
|
||||
" soup = BeautifulSoup(html_content, 'html.parser')\n",
|
||||
" image_tags = soup.find_all('img')\n",
|
||||
" self.image_urls = [img['src'] for img in image_tags if img.get('src')]\n",
|
||||
" self.image_alt = [img['alt'] if img.get('alt') else \"\" for img in image_tags]\n",
|
||||
"\n",
|
||||
" # Restricting to 3 images only.\n",
|
||||
" if self.image_urls:\n",
|
||||
" self.images = {self.image_urls[i]:self.image_alt[i] for i in range(4)}\n",
|
||||
" else:\n",
|
||||
" self.images = {}\n",
|
||||
" \n",
|
||||
"\n",
|
||||
"def user_prompt_for(website):\n",
|
||||
" user_prompt = f\"Following are images with their alt-text:\"\n",
|
||||
" user_prompt += json.dumps(website.images)\n",
|
||||
" user_prompt += \"\\n Give me a markdown layout with tables for each image where each image is given its own row, with the image itself on the left and funny tweet on the right.\"\n",
|
||||
" return user_prompt\n",
|
||||
"\n",
|
||||
"system_prompt = \"something here\"\n",
|
||||
"user_prompt = \"\"\"\n",
|
||||
" Lots of text\n",
|
||||
" Can be pasted here\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"# Step 2: Make the messages list\n",
|
||||
"\n",
|
||||
"messages = [] # fill this in\n",
|
||||
"page = website(\"https://www.pexels.com/\")\n",
|
||||
"user_prompt = user_prompt_for(page)\n",
|
||||
"messages = [{\"role\":\"system\",\"content\":system_prompt},{\"role\":\"user\", \"content\":user_prompt}] # fill this in\n",
|
||||
"\n",
|
||||
"# Step 3: Call OpenAI\n",
|
||||
"\n",
|
||||
"response =\n",
|
||||
"response = openai.chat.completions.create(\n",
|
||||
" model = \"gpt-4o-mini\",\n",
|
||||
" messages = messages\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"# Step 4: print the result\n",
|
||||
"\n",
|
||||
"print("
|
||||
"display(Markdown((response.choices[0].message.content)))"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user