|
121 | 121 | "outputs": [], |
122 | 122 | "source": [ |
123 | 123 | "import os\n", |
124 | | - "import urllib.request\n", |
| 124 | + "import requests\n", |
125 | 125 | "\n", |
126 | 126 | "file_path = \"the-verdict.txt\"\n", |
127 | 127 | "url = \"https://raw.githubusercontent.com/rasbt/LLMs-from-scratch/main/ch02/01_main-chapter-code/the-verdict.txt\"\n", |
128 | 128 | "\n", |
129 | 129 | "if not os.path.exists(file_path):\n", |
| 130 | + " response = requests.get(url, timeout=30)\n", |
| 131 | + " response.raise_for_status()\n", |
| 132 | + " text_data = response.text\n", |
| 133 | + " with open(file_path, \"w\", encoding=\"utf-8\") as file:\n", |
| 134 | + " file.write(text_data)\n", |
| 135 | + "else:\n", |
| 136 | + " with open(file_path, \"r\", encoding=\"utf-8\") as file:\n", |
| 137 | + " text_data = file.read()\n", |
| 138 | + "\n", |
| 139 | + "# The book originally used the following code below\n", |
| 140 | + "# However, urllib uses older protocol settings that\n", |
| 141 | + "# can cause problems for some readers using a VPN.\n", |
| 142 | + "# The `requests` version above is more robust\n", |
| 143 | + "# in that regard.\n", |
| 144 | + "\n", |
| 145 | + "\"\"\"\n", |
| 146 | + "import os\n", |
| 147 | + "import urllib.request\n", |
| 148 | + "\n", |
| 149 | + "if not os.path.exists(file_path):\n", |
130 | 150 | " with urllib.request.urlopen(url) as response:\n", |
131 | 151 | " text_data = response.read().decode('utf-8')\n", |
132 | 152 | " with open(file_path, \"w\", encoding=\"utf-8\") as file:\n", |
133 | 153 | " file.write(text_data)\n", |
134 | 154 | "else:\n", |
135 | 155 | " with open(file_path, \"r\", encoding=\"utf-8\") as file:\n", |
136 | | - " text_data = file.read()" |
| 156 | + " text_data = file.read()\n", |
| 157 | + "\"\"\"" |
137 | 158 | ] |
138 | 159 | }, |
139 | 160 | { |
|
0 commit comments