Spaces:
Sleeping
Sleeping
File size: 11,313 Bytes
5fdb69e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
{
"cells": [
{
"cell_type": "markdown",
"id": "5787d599-798e-4161-a473-970e8d948db3",
"metadata": {},
"source": [
"# Community contribution\n",
"\n",
"Generating a sports brochure - and then in Spanish! Many thanks for the contribution."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4ed9b1c0-50dc-48ea-b1b6-6be3264255b6",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import requests\n",
"import json\n",
"from typing import List\n",
"from dotenv import load_dotenv\n",
"from bs4 import BeautifulSoup\n",
"from IPython.display import Markdown, display, update_display\n",
"from openai import OpenAI"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "23cc579a-43eb-44b5-8105-8ec3d46ec029",
"metadata": {},
"outputs": [],
"source": [
"# Initialize and constants\n",
"\n",
"load_dotenv()\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"MODEL = 'gpt-4o-mini'\n",
"openai = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0206cf1d-00c5-401d-8aa0-88a83aeb8c83",
"metadata": {},
"outputs": [],
"source": [
"# A class to represent a Webpage\n",
"\n",
"class Website:\n",
" url: str\n",
" title: str\n",
" body: str\n",
" links: List[str]\n",
"\n",
" def __init__(self, url):\n",
" self.url = url\n",
" response = requests.get(url)\n",
" self.body = response.content\n",
" soup = BeautifulSoup(self.body, 'html.parser')\n",
" self.title = soup.title.string if soup.title else \"No title found\"\n",
" if soup.body:\n",
" for irrelevant in soup.body([\"script\", \"style\", \"img\", \"input\"]):\n",
" irrelevant.decompose()\n",
" self.text = soup.body.get_text(separator=\"\\n\", strip=True)\n",
" else:\n",
" self.text = \"\"\n",
" links = [link.get('href') for link in soup.find_all('a')]\n",
" self.links = [link for link in links if link]\n",
"\n",
" def get_contents(self):\n",
" return f\"Webpage Title:\\n{self.title}\\nWebpage Contents:\\n{self.text}\\n\\n\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e1ab939b-0a81-4301-8820-abdc1b740a86",
"metadata": {},
"outputs": [],
"source": [
"caps = Website(\"https://www.nhl.com/capitals/\")\n",
"print(caps.get_contents())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6bc9763c-e8cb-47f4-a5f0-fccbdb34f5f9",
"metadata": {},
"outputs": [],
"source": [
"print(caps.links)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "11a407e0-6d23-4199-bb58-344352178978",
"metadata": {},
"outputs": [],
"source": [
"link_system_prompt = \"You are provided with a list of links found on a webpage. \\\n",
"You are able to decide which of the links would be most relevant to include in a brochure about the team, \\\n",
"such as links to an About page, Team, News, Schedule, History, Stats pages.\\n\"\n",
"link_system_prompt += \"You should respond in JSON as in this example:\"\n",
"link_system_prompt += \"\"\"\n",
"{\n",
" \"links\": [\n",
" {\"type\": \"about page\", \"url\": \"https://full.url/goes/here/about\"},\n",
" {\"type\": \"team page\", \"url\": \"https://full.url/goes/here/team\"},\n",
" {\"type\": \"news page\": \"url\": \"https://another.full.url/news\"},\n",
" {\"type\": \"schedule page\": \"url\": \"https://another.full.url/schedule\"},\n",
" {\"type\": \"history page\": \"url\": \"https://another.full.url/history\"},\n",
" {\"type\": \"stats page\": \"url\": \"https://another.full.url/stats\"},\n",
" {\"type\": \"standings page\": \"url\": \"https://another.full.url/standings\"},\n",
" ]\n",
"}\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b45c01f5-e27d-47d6-b9e7-7500940da3c0",
"metadata": {},
"outputs": [],
"source": [
"def get_links_user_prompt(website):\n",
" user_prompt = f\"Here is the list of links on the website of {website.url} - \"\n",
" user_prompt += \"please decide which of these are relevant web links for a brochure about the team, respond with the full https URL in JSON format. \\\n",
"Do not include Terms of Service, Privacy, Tickets, Video, Listen, Community, Fans, Youth Hockey, Shop, League, email links.\\n\"\n",
" user_prompt += \"Links (some might be relative links):\\n\"\n",
" user_prompt += \"\\n\".join(website.links)\n",
" return user_prompt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9dbfc365-b3c7-45d2-bd6d-79efb2372f43",
"metadata": {},
"outputs": [],
"source": [
"print(get_links_user_prompt(caps))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "410c113b-a5b2-4639-aace-2203a8631dbc",
"metadata": {},
"outputs": [],
"source": [
"def get_links(url):\n",
" website = Website(url)\n",
" completion = openai.chat.completions.create(\n",
" model=MODEL,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": link_system_prompt},\n",
" {\"role\": \"user\", \"content\": get_links_user_prompt(website)}\n",
" ],\n",
" response_format={\"type\": \"json_object\"}\n",
" )\n",
" result = completion.choices[0].message.content\n",
" return json.loads(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b8a5d809-c85e-4a97-9a81-61114b635027",
"metadata": {},
"outputs": [],
"source": [
"get_links(\"https://www.nhl.com/capitals/\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ffb4dbdf-758c-4180-aa4f-3c18899493eb",
"metadata": {},
"outputs": [],
"source": [
"def get_all_details(url):\n",
" result = \"Landing page:\\n\"\n",
" result += Website(url).get_contents()\n",
" links = get_links(url)\n",
" print(\"Found links:\", links)\n",
" for link in links[\"links\"]:\n",
" result += f\"\\n\\n{link['type']}\\n\"\n",
" result += Website(link[\"url\"]).get_contents()\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7546e12-c819-4092-a783-7be23d4d7b8c",
"metadata": {},
"outputs": [],
"source": [
"print(get_all_details(\"https://www.nhl.com/capitals/\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3d9aa466-e68d-4119-95e4-10893d774ebf",
"metadata": {},
"outputs": [],
"source": [
"system_prompt = \"You are a sports marketing analyst that analyzes the contents of several relevant pages from a sports team website \\\n",
"and creates a short brochure about the team for prospective fans and players to recruit. Respond in markdown.\\\n",
"Include details of team history, team culture, team news, and team stats if you have the information.\"\n",
"\n",
"# Or uncomment the lines below for a more humorous brochure - this demonstrates how easy it is to incorporate 'tone':\n",
"\n",
"# system_prompt = \"You are an assistant that analyzes the contents of several relevant pages from a company website \\\n",
"# and creates a short humorous, entertaining, jokey brochure about the company for prospective customers, investors and recruits. Respond in markdown.\\\n",
"# Include details of company culture, customers and careers/jobs if you have the information.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "07097603-5196-47cc-9e61-b412c14b62c6",
"metadata": {},
"outputs": [],
"source": [
"def get_brochure_user_prompt(company_name, url):\n",
" user_prompt = f\"You are looking at a company called: {company_name}\\n\"\n",
" user_prompt += f\"Here are the contents of its landing page and other relevant pages; use this information to build a short brochure of the team in markdown.\\n\"\n",
" user_prompt += get_all_details(url)\n",
" user_prompt = user_prompt[:40_000] # Truncate if more than 40,000 characters\n",
" return user_prompt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "36d42b59-0a96-41b7-b9fc-02ae37659f7a",
"metadata": {},
"outputs": [],
"source": [
"def create_brochure(company_name, url):\n",
" response = openai.chat.completions.create(\n",
" model=MODEL,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": get_brochure_user_prompt(company_name, url)}\n",
" ],\n",
" )\n",
" result = response.choices[0].message.content\n",
" display(Markdown(result))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6e642228-092a-4bf5-a3c2-1ab78822453a",
"metadata": {},
"outputs": [],
"source": [
"create_brochure(\"Washington Capitals\", \"https://www.nhl.com/capitals\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1ccee810-3a11-48ec-8a69-d12efcbeb1cd",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "d392b8a2-9d1a-40bb-a8c1-5b88ad7e463d",
"metadata": {},
"source": [
"# Translate to Spanish"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7bc08bb5-c02e-4685-bbd1-5afa084b7f18",
"metadata": {},
"outputs": [],
"source": [
"def get_brochure_user_prompt(company_name, url):\n",
" user_prompt = f\"You are looking at a company called: {company_name}\\n\"\n",
" user_prompt += f\"Here are the contents of its landing page and other relevant pages; use this information to build a short brochure in spanish of the team in markdown.\\n\"\n",
" user_prompt += get_all_details(url)\n",
" user_prompt = user_prompt[:40_000] # Truncate if more than 40,000 characters\n",
" return user_prompt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a40a2437-499a-4665-8d45-8241705477d6",
"metadata": {},
"outputs": [],
"source": [
"create_brochure(\"Washington Capitals\", \"https://www.nhl.com/capitals\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "36f5d6d7-952c-4d50-894c-9b01e29cebc8",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|