Files
hack/tools/scripts/llm-key-hunter/results/_cache/content/aashique1915005_web-scraping.json
T

1 line
3.0 KiB
JSON

{"repo": "aashique1915005/web-scraping", "files": {"web.py": {"552d0a044c14f61a7dae9dacc2818bb9fe434bd1": {"t": 1785674452.9303193, "n": 2715, "h": "1063561f5e960feb", "c": "import os\r\nimport requests\r\nfrom dotenv import load_dotenv\r\nfrom bs4 import BeautifulSoup\r\nfrom IPython.display import Markdown, display\r\nfrom openai import OpenAI\r\nload_dotenv(override=True)\r\napi_key = os.getenv('OPENAI_API_KEY')\r\n\r\n# Check the key\r\n\r\nif not api_key:\r\n print(\"No API key was found !\")\r\nelif not api_key.startswith(\"sk-proj-\"):\r\n print(\"An API key was found, but it doesn't start sk-proj-; \")\r\nelif api_key.strip() != api_key:\r\n print(\"An API key was found, but it looks like it might have space or tab characters at the start or end \")\r\nelse:\r\n print(\"API key found and looks good so far!\")\r\n\r\nopenai = OpenAI()\r\n\r\nmessage = \"Hello, GPT! This is my first ever message to you! Hi!\"\r\nresponse = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=[{\"role\":\"user\", \"content\":message}])\r\nprint(response.choices[0].message.content)\r\n\r\nheaders = {\r\n \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36\"\r\n}\r\n\r\nclass Website:\r\n\r\n def __init__(self, url):\r\n \"\"\"\r\n Create this Website object from the given url using the BeautifulSoup library\r\n \"\"\"\r\n self.url = url\r\n response = requests.get(url, headers=headers)\r\n soup = BeautifulSoup(response.content, 'html.parser')\r\n self.title = soup.title.string if soup.title else \"No title found\"\r\n for irrelevant in soup.body([\"script\", \"style\", \"img\", \"input\"]):\r\n irrelevant.decompose()\r\n self.text = soup.body.get_text(separator=\"\\n\", strip=True)\r\n\r\nsystem_prompt = \"You are an assistant that analyzes the contents of a website \\\r\nand provides a short summary, ignoring text that might be navigation related. \\\r\nRespond in markdown.\"\r\n\r\ndef user_prompt_for(website):\r\n user_prompt = f\"You are looking at a website titled {website.title}\"\r\n user_prompt += \"\\nThe contents of this website is as follows; \\\r\nplease provide a short summary of this website in markdown. \\\r\nTotal employee count ? \\\r\nIf it includes news or announcements, then summarize these too.\\n\\n\"\r\n user_prompt += website.text\r\n return user_prompt\r\n\r\ndef messages_for(website):\r\n return [\r\n {\"role\": \"system\", \"content\": system_prompt},\r\n {\"role\": \"user\", \"content\": user_prompt_for(website)}\r\n ]\r\n\r\ndef summarize(url):\r\n website = Website(url)\r\n response = openai.chat.completions.create(\r\n model = \"gpt-4o-mini\",\r\n messages = messages_for(website)\r\n )\r\n return response.choices[0].message.content\r\n\r\ndef display_summary(url):\r\n summary = summarize(url)\r\n display(Markdown(summary))\r\n\r\ndisplay_summary(\"https://www.infosys.com\")\r\n\r\nprint(display_summary)"}}}}