Browse Source

Use llm to generate funny tweet on image alt-text

pull/210/head
SyedNaqi Hussain 3 months ago
parent
commit
606b11682c
  1. 101
      week1/day1.ipynb

101
week1/day1.ipynb

@ -159,8 +159,8 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"openai = OpenAI()\n", "import httpx\n",
"\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 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" "# 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", " Create this Website object from the given url using the BeautifulSoup library\n",
" \"\"\"\n", " \"\"\"\n",
" self.url = url\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", " soup = BeautifulSoup(response.content, 'html.parser')\n",
" self.title = soup.title.string if soup.title else \"No title found\"\n", " self.title = soup.title.string if soup.title else \"No title found\"\n",
" for irrelevant in soup.body([\"script\", \"style\", \"img\", \"input\"]):\n", " for irrelevant in soup.body([\"script\", \"style\", \"img\", \"input\"]):\n",
@ -233,8 +234,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"# Let's try one out. Change the website and add print statements to follow along.\n", "# Let's try one out. Change the website and add print statements to follow along.\n",
"\n", "ed = Website(\"http://edwarddonner.com\")\n",
"ed = Website(\"https://edwarddonner.com\")\n",
"print(ed.title)\n", "print(ed.title)\n",
"print(ed.text)" "print(ed.text)"
] ]
@ -434,12 +434,24 @@
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "3018853a-445f-41ff-9560-d925d1774b2f", "id": "3018853a-445f-41ff-9560-d925d1774b2f",
"metadata": {}, "metadata": {
"scrolled": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"display_summary(\"https://edwarddonner.com\")" "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", "cell_type": "markdown",
"id": "b3bcf6f4-adce-45e9-97ad-d9a5d7a3a624", "id": "b3bcf6f4-adce-45e9-97ad-d9a5d7a3a624",
@ -470,7 +482,9 @@
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "75e9fd40-b354-4341-991e-863ef2e59db7", "id": "75e9fd40-b354-4341-991e-863ef2e59db7",
"metadata": {}, "metadata": {
"scrolled": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"display_summary(\"https://anthropic.com\")" "display_summary(\"https://anthropic.com\")"
@ -510,30 +524,77 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 25,
"id": "00743dac-0e70-45b7-879a-d7293a6f68a6", "id": "00743dac-0e70-45b7-879a-d7293a6f68a6",
"metadata": {}, "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",
"| ![Vintage Motorcycle](https://images.pexels.com/photos/30770767/pexels-photo-30770767/free-photo-of-classic-motorcycle-in-kerala-countryside.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500) | \"When you want to feel like a rebel, but your bike is still in the shop. 🏍😂\" |\n",
"| ![Flock of Birds](https://images.pexels.com/photos/30810205/pexels-photo-30810205/free-photo-of-flock-of-birds-in-flight-against-clear-sky.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500) | \"When the squad is finally ready to leave the party but you can't find your keys. 🕊🤣\" |\n",
"| ![Playful Seals](https://images.pexels.com/photos/30824250/pexels-photo-30824250/free-photo-of-playful-seals-on-rocky-san-diego-shore.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500) | \"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": [ "source": [
"# A small exercise to feed the llm with image alt text and return a funny tweet.\n",
"\n",
"# Step 1: Create your prompts\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",
"system_prompt = \"something here\"\n",
"user_prompt = \"\"\"\n",
" Lots of text\n",
" Can be pasted here\n",
"\"\"\"\n",
"\n", "\n",
"# Step 2: Make the messages list\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", "\n",
"messages = [] # fill this in\n",
"\n", "\n",
"# Step 3: Call OpenAI\n", "# Step 2: Make the messages list\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", "\n",
"response =\n", "# Step 3: Call OpenAI\n",
"response = openai.chat.completions.create(\n",
" model = \"gpt-4o-mini\",\n",
" messages = messages\n",
" )\n",
"\n", "\n",
"# Step 4: print the result\n", "# Step 4: print the result\n",
"\n", "display(Markdown((response.choices[0].message.content)))"
"print("
] ]
}, },
{ {

Loading…
Cancel
Save