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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {},
|
6 |
+
"source": [
|
7 |
+
"# π TensorFlow β PyTorch Conversion\n",
|
8 |
+
"\n",
|
9 |
+
"This section guides you through converting the PatentBERT model from TensorFlow to PyTorch and uploading it to Hugging Face Hub.\n",
|
10 |
+
"\n",
|
11 |
+
"## π Conversion Plan:\n",
|
12 |
+
"\n",
|
13 |
+
"1. **TensorFlow Model Download** (previous cells)\n",
|
14 |
+
"2. **Weight Extraction** - Extract parameters from TensorFlow checkpoint\n",
|
15 |
+
"3. **PyTorch Conversion** - Create equivalent PyTorch model\n",
|
16 |
+
"4. **Model Testing** - Verify that the conversion works\n",
|
17 |
+
"5. **Hugging Face Upload** - Publish to Hub for public use\n",
|
18 |
+
"\n",
|
19 |
+
"## β οΈ Prerequisites:\n",
|
20 |
+
"- PatentBERT model downloaded (run previous cells first)\n",
|
21 |
+
"- Python 3.7+ with TensorFlow 1.15\n",
|
22 |
+
"- Separate environment with PyTorch to avoid conflicts"
|
23 |
+
]
|
24 |
+
},
|
25 |
+
{
|
26 |
+
"cell_type": "code",
|
27 |
+
"execution_count": 1,
|
28 |
+
"metadata": {},
|
29 |
+
"outputs": [
|
30 |
+
{
|
31 |
+
"name": "stdout",
|
32 |
+
"output_type": "stream",
|
33 |
+
"text": [
|
34 |
+
"π Environment verification...\n",
|
35 |
+
"Python: 3.7.16 (default, Jan 17 2023, 22:20:44) \n",
|
36 |
+
"[GCC 11.2.0]\n",
|
37 |
+
"TensorFlow: 1.15.0\n",
|
38 |
+
"NumPy: 1.21.5\n",
|
39 |
+
"\n",
|
40 |
+
"π Checking model files in ./:\n",
|
41 |
+
"β
model.ckpt-181172.data-00000-of-00001\n",
|
42 |
+
"β
model.ckpt-181172.index\n",
|
43 |
+
"β
model.ckpt-181172.meta\n",
|
44 |
+
"β
bert_config.json\n",
|
45 |
+
"β
vocab.txt\n",
|
46 |
+
"\n",
|
47 |
+
"β
All model files are present!\n",
|
48 |
+
"π Created: /tmp/patentbert_conversion\n",
|
49 |
+
"π Created: /tmp/patentbert_conversion/tf_weights\n",
|
50 |
+
"π Created: /tmp/patentbert_conversion/pytorch_model\n",
|
51 |
+
"\n",
|
52 |
+
"π― Ready for conversion!\n",
|
53 |
+
"π Working directories configured\n"
|
54 |
+
]
|
55 |
+
}
|
56 |
+
],
|
57 |
+
"source": [
|
58 |
+
"# Step 1: Environment verification and preparation\n",
|
59 |
+
"\n",
|
60 |
+
"import os\n",
|
61 |
+
"import sys\n",
|
62 |
+
"import json\n",
|
63 |
+
"import numpy as np\n",
|
64 |
+
"import tensorflow as tf\n",
|
65 |
+
"\n",
|
66 |
+
"print(\"π Environment verification...\")\n",
|
67 |
+
"print(f\"Python: {sys.version}\")\n",
|
68 |
+
"print(f\"TensorFlow: {tf.__version__}\")\n",
|
69 |
+
"print(f\"NumPy: {np.__version__}\")\n",
|
70 |
+
"\n",
|
71 |
+
"# Verify that PatentBERT model has been downloaded\n",
|
72 |
+
"model_folder = './'\n",
|
73 |
+
"required_files = [\n",
|
74 |
+
" 'model.ckpt-181172.data-00000-of-00001',\n",
|
75 |
+
" 'model.ckpt-181172.index',\n",
|
76 |
+
" 'model.ckpt-181172.meta',\n",
|
77 |
+
" 'bert_config.json',\n",
|
78 |
+
" 'vocab.txt'\n",
|
79 |
+
"]\n",
|
80 |
+
"\n",
|
81 |
+
"print(f\"\\nπ Checking model files in {model_folder}:\")\n",
|
82 |
+
"missing_files = []\n",
|
83 |
+
"for file in required_files:\n",
|
84 |
+
" filepath = os.path.join(model_folder, file)\n",
|
85 |
+
" if os.path.exists(filepath):\n",
|
86 |
+
" print(f\"β
{file}\")\n",
|
87 |
+
" else:\n",
|
88 |
+
" print(f\"β {file} - MISSING\")\n",
|
89 |
+
" missing_files.append(file)\n",
|
90 |
+
"\n",
|
91 |
+
"if missing_files:\n",
|
92 |
+
" print(f\"\\nβ οΈ Missing files: {missing_files}\")\n",
|
93 |
+
" print(\"π‘ Please run the previous cells first to download the model\")\n",
|
94 |
+
"else:\n",
|
95 |
+
" print(\"\\nβ
All model files are present!\")\n",
|
96 |
+
"\n",
|
97 |
+
"# Create working directories for conversion\n",
|
98 |
+
"conversion_dir = \"/tmp/patentbert_conversion\"\n",
|
99 |
+
"tf_weights_dir = os.path.join(conversion_dir, \"tf_weights\")\n",
|
100 |
+
"pytorch_dir = os.path.join(conversion_dir, \"pytorch_model\")\n",
|
101 |
+
"\n",
|
102 |
+
"for dir_path in [conversion_dir, tf_weights_dir, pytorch_dir]:\n",
|
103 |
+
" os.makedirs(dir_path, exist_ok=True)\n",
|
104 |
+
" print(f\"π Created: {dir_path}\")\n",
|
105 |
+
"\n",
|
106 |
+
"print(f\"\\nπ― Ready for conversion!\")\n",
|
107 |
+
"print(f\"π Working directories configured\")"
|
108 |
+
]
|
109 |
+
},
|
110 |
+
{
|
111 |
+
"cell_type": "code",
|
112 |
+
"execution_count": 2,
|
113 |
+
"metadata": {},
|
114 |
+
"outputs": [
|
115 |
+
{
|
116 |
+
"name": "stdout",
|
117 |
+
"output_type": "stream",
|
118 |
+
"text": [
|
119 |
+
"π Extracting weights from TensorFlow PatentBERT model...\n",
|
120 |
+
"π Model configuration:\n",
|
121 |
+
" β’ Hidden size: 768\n",
|
122 |
+
" β’ Number of layers: 12\n",
|
123 |
+
" β’ Attention heads: 12\n",
|
124 |
+
" β’ Vocabulary size: 30522\n",
|
125 |
+
"π Found 604 variables in checkpoint\n",
|
126 |
+
"π 176 important variables to extract\n",
|
127 |
+
"π Extraction in progress...\n",
|
128 |
+
" Progress: 20/176 (11.4%)\n",
|
129 |
+
" Progress: 20/176 (11.4%)\n",
|
130 |
+
" Progress: 40/176 (22.7%)\n",
|
131 |
+
" Progress: 40/176 (22.7%)\n",
|
132 |
+
" Progress: 60/176 (34.1%)\n",
|
133 |
+
" Progress: 60/176 (34.1%)\n",
|
134 |
+
" Progress: 80/176 (45.5%)\n",
|
135 |
+
" Progress: 80/176 (45.5%)\n",
|
136 |
+
" Progress: 100/176 (56.8%)\n",
|
137 |
+
" Progress: 100/176 (56.8%)\n",
|
138 |
+
" Progress: 120/176 (68.2%)\n",
|
139 |
+
" Progress: 120/176 (68.2%)\n",
|
140 |
+
" Progress: 140/176 (79.5%)\n",
|
141 |
+
" Progress: 140/176 (79.5%)\n",
|
142 |
+
" Progress: 160/176 (90.9%)\n",
|
143 |
+
" Progress: 160/176 (90.9%)\n",
|
144 |
+
" Progress: 176/176 (100.0%)\n",
|
145 |
+
"β
Extraction completed!\n",
|
146 |
+
"π Weights saved in: /tmp/patentbert_conversion/tf_weights\n",
|
147 |
+
"π 176 weights extracted\n",
|
148 |
+
"πΎ Total size: 419.5 MB\n",
|
149 |
+
"\n",
|
150 |
+
"π Examples of created files:\n",
|
151 |
+
" β’ bert_config.json\n",
|
152 |
+
" β’ bert_embeddings_LayerNorm_gamma.npy\n",
|
153 |
+
" β’ bert_embeddings_position_embeddings.npy\n",
|
154 |
+
" β’ bert_embeddings_token_type_embeddings.npy\n",
|
155 |
+
" β’ bert_embeddings_word_embeddings.npy\n",
|
156 |
+
" ... and 174 other files\n",
|
157 |
+
"\n",
|
158 |
+
"π Extraction successful!\n",
|
159 |
+
" Progress: 176/176 (100.0%)\n",
|
160 |
+
"β
Extraction completed!\n",
|
161 |
+
"π Weights saved in: /tmp/patentbert_conversion/tf_weights\n",
|
162 |
+
"π 176 weights extracted\n",
|
163 |
+
"πΎ Total size: 419.5 MB\n",
|
164 |
+
"\n",
|
165 |
+
"π Examples of created files:\n",
|
166 |
+
" β’ bert_config.json\n",
|
167 |
+
" β’ bert_embeddings_LayerNorm_gamma.npy\n",
|
168 |
+
" β’ bert_embeddings_position_embeddings.npy\n",
|
169 |
+
" β’ bert_embeddings_token_type_embeddings.npy\n",
|
170 |
+
" β’ bert_embeddings_word_embeddings.npy\n",
|
171 |
+
" ... and 174 other files\n",
|
172 |
+
"\n",
|
173 |
+
"π Extraction successful!\n"
|
174 |
+
]
|
175 |
+
}
|
176 |
+
],
|
177 |
+
"source": [
|
178 |
+
"# Step 2: TensorFlow model weights extraction\n",
|
179 |
+
"\n",
|
180 |
+
"print(\"π Extracting weights from TensorFlow PatentBERT model...\")\n",
|
181 |
+
"\n",
|
182 |
+
"def extract_tf_weights():\n",
|
183 |
+
" \"\"\"Extract all weights from TensorFlow checkpoint\"\"\"\n",
|
184 |
+
" \n",
|
185 |
+
" # File paths\n",
|
186 |
+
" checkpoint_path = \"./model.ckpt-181172\"\n",
|
187 |
+
" config_path = \"./bert_config.json\"\n",
|
188 |
+
" vocab_path = \"./vocab.txt\"\n",
|
189 |
+
" \n",
|
190 |
+
" # Read BERT configuration\n",
|
191 |
+
" with open(config_path, 'r') as f:\n",
|
192 |
+
" config = json.load(f)\n",
|
193 |
+
" \n",
|
194 |
+
" print(f\"π Model configuration:\")\n",
|
195 |
+
" print(f\" β’ Hidden size: {config.get('hidden_size', 768)}\")\n",
|
196 |
+
" print(f\" β’ Number of layers: {config.get('num_hidden_layers', 12)}\")\n",
|
197 |
+
" print(f\" β’ Attention heads: {config.get('num_attention_heads', 12)}\")\n",
|
198 |
+
" print(f\" β’ Vocabulary size: {config.get('vocab_size', 30522)}\")\n",
|
199 |
+
" \n",
|
200 |
+
" # List all variables in checkpoint\n",
|
201 |
+
" var_list = tf.train.list_variables(checkpoint_path)\n",
|
202 |
+
" print(f\"π Found {len(var_list)} variables in checkpoint\")\n",
|
203 |
+
" \n",
|
204 |
+
" # Filter important variables (ignore optimization variables)\n",
|
205 |
+
" skip_patterns = ['adam', 'beta', 'global_step', 'learning_rate']\n",
|
206 |
+
" important_vars = []\n",
|
207 |
+
" \n",
|
208 |
+
" for name, shape in var_list:\n",
|
209 |
+
" if not any(pattern in name.lower() for pattern in skip_patterns):\n",
|
210 |
+
" important_vars.append((name, shape))\n",
|
211 |
+
" \n",
|
212 |
+
" print(f\"π {len(important_vars)} important variables to extract\")\n",
|
213 |
+
" \n",
|
214 |
+
" # Extract and save weights\n",
|
215 |
+
" weights_info = {}\n",
|
216 |
+
" total_size = 0\n",
|
217 |
+
" \n",
|
218 |
+
" print(\"π Extraction in progress...\")\n",
|
219 |
+
" for i, (name, shape) in enumerate(important_vars):\n",
|
220 |
+
" try:\n",
|
221 |
+
" # Load variable\n",
|
222 |
+
" weight = tf.train.load_variable(checkpoint_path, name)\n",
|
223 |
+
" \n",
|
224 |
+
" # Create safe filename\n",
|
225 |
+
" safe_name = name.replace('/', '_').replace(':', '_').replace(' ', '_')\n",
|
226 |
+
" filename = f\"{safe_name}.npy\"\n",
|
227 |
+
" \n",
|
228 |
+
" # Save in NumPy format\n",
|
229 |
+
" filepath = os.path.join(tf_weights_dir, filename)\n",
|
230 |
+
" np.save(filepath, weight)\n",
|
231 |
+
" \n",
|
232 |
+
" # Record metadata\n",
|
233 |
+
" weights_info[name] = {\n",
|
234 |
+
" 'filename': filename,\n",
|
235 |
+
" 'shape': list(shape),\n",
|
236 |
+
" 'dtype': str(weight.dtype),\n",
|
237 |
+
" 'size_mb': weight.nbytes / (1024 * 1024)\n",
|
238 |
+
" }\n",
|
239 |
+
" \n",
|
240 |
+
" total_size += weight.nbytes\n",
|
241 |
+
" \n",
|
242 |
+
" # Show progress\n",
|
243 |
+
" if (i + 1) % 20 == 0 or (i + 1) == len(important_vars):\n",
|
244 |
+
" print(f\" Progress: {i + 1}/{len(important_vars)} ({(i+1)/len(important_vars)*100:.1f}%)\")\n",
|
245 |
+
" \n",
|
246 |
+
" except Exception as e:\n",
|
247 |
+
" print(f\"β οΈ Error for {name}: {e}\")\n",
|
248 |
+
" continue\n",
|
249 |
+
" \n",
|
250 |
+
" # Create complete metadata\n",
|
251 |
+
" metadata = {\n",
|
252 |
+
" 'model_info': {\n",
|
253 |
+
" 'name': 'PatentBERT',\n",
|
254 |
+
" 'source': 'TensorFlow',\n",
|
255 |
+
" 'checkpoint_path': checkpoint_path,\n",
|
256 |
+
" 'extraction_date': '2025-07-20'\n",
|
257 |
+
" },\n",
|
258 |
+
" 'config': config,\n",
|
259 |
+
" 'weights_info': weights_info,\n",
|
260 |
+
" 'statistics': {\n",
|
261 |
+
" 'total_weights': len(weights_info),\n",
|
262 |
+
" 'total_size_mb': total_size / (1024 * 1024),\n",
|
263 |
+
" 'original_variables': len(var_list),\n",
|
264 |
+
" 'extracted_variables': len(weights_info)\n",
|
265 |
+
" }\n",
|
266 |
+
" }\n",
|
267 |
+
" \n",
|
268 |
+
" # Save metadata\n",
|
269 |
+
" metadata_path = os.path.join(tf_weights_dir, 'extraction_metadata.json')\n",
|
270 |
+
" with open(metadata_path, 'w') as f:\n",
|
271 |
+
" json.dump(metadata, f, indent=2)\n",
|
272 |
+
" \n",
|
273 |
+
" # Copy configuration files\n",
|
274 |
+
" import shutil\n",
|
275 |
+
" shutil.copy(config_path, os.path.join(tf_weights_dir, 'bert_config.json'))\n",
|
276 |
+
" shutil.copy(vocab_path, os.path.join(tf_weights_dir, 'vocab.txt'))\n",
|
277 |
+
" \n",
|
278 |
+
" print(f\"β
Extraction completed!\")\n",
|
279 |
+
" print(f\"π Weights saved in: {tf_weights_dir}\")\n",
|
280 |
+
" print(f\"π {len(weights_info)} weights extracted\")\n",
|
281 |
+
" print(f\"πΎ Total size: {total_size / (1024 * 1024):.1f} MB\")\n",
|
282 |
+
" \n",
|
283 |
+
" # Show some examples of extracted weights\n",
|
284 |
+
" print(f\"\\nπ Examples of created files:\")\n",
|
285 |
+
" files = sorted(os.listdir(tf_weights_dir))\n",
|
286 |
+
" for i, file in enumerate(files[:5]):\n",
|
287 |
+
" print(f\" β’ {file}\")\n",
|
288 |
+
" if len(files) > 5:\n",
|
289 |
+
" print(f\" ... and {len(files) - 5} other files\")\n",
|
290 |
+
" \n",
|
291 |
+
" return tf_weights_dir, metadata\n",
|
292 |
+
"\n",
|
293 |
+
"# Execute extraction\n",
|
294 |
+
"try:\n",
|
295 |
+
" weights_dir, metadata = extract_tf_weights()\n",
|
296 |
+
" print(\"\\nπ Extraction successful!\")\n",
|
297 |
+
" \n",
|
298 |
+
"except Exception as e:\n",
|
299 |
+
" print(f\"β Error during extraction: {e}\")\n",
|
300 |
+
" import traceback\n",
|
301 |
+
" traceback.print_exc()"
|
302 |
+
]
|
303 |
+
},
|
304 |
+
{
|
305 |
+
"cell_type": "code",
|
306 |
+
"execution_count": 1,
|
307 |
+
"metadata": {},
|
308 |
+
"outputs": [
|
309 |
+
{
|
310 |
+
"name": "stdout",
|
311 |
+
"output_type": "stream",
|
312 |
+
"text": [
|
313 |
+
"π― Converting TensorFlow weights to PyTorch format...\n",
|
314 |
+
"β
CORRECTED upload script created!\n",
|
315 |
+
"\n",
|
316 |
+
"π§ Key corrections:\n",
|
317 |
+
" β
Accepts BOTH model.safetensors AND pytorch_model.bin\n",
|
318 |
+
" β
Automatically detects model format\n",
|
319 |
+
" β
Improved error messages\n",
|
320 |
+
" β
Better commit message with format info\n",
|
321 |
+
" β
Proper torch import for testing\n",
|
322 |
+
"\n",
|
323 |
+
"π NOW RUN THIS CORRECTED COMMAND:\n",
|
324 |
+
" python /tmp/upload_to_hf.py patentbert_conversion/pytorch_model ZoeYou/patentbert-pytorch xxxxx\n",
|
325 |
+
"\n",
|
326 |
+
"π‘ Or use the new corrected script:\n",
|
327 |
+
" python /tmp/upload_to_hf_corrected.py patentbert_conversion/pytorch_model ZoeYou/patentbert-pytorch xxxxx\n"
|
328 |
+
]
|
329 |
+
}
|
330 |
+
],
|
331 |
+
"source": [
|
332 |
+
"# Step 3: Convert TensorFlow weights to PyTorch format\n",
|
333 |
+
"\n",
|
334 |
+
"print(\"π― Converting TensorFlow weights to PyTorch format...\")\n",
|
335 |
+
"\n",
|
336 |
+
"corrected_upload_script = \"\"\"#!/usr/bin/env python3\n",
|
337 |
+
"import os\n",
|
338 |
+
"import sys\n",
|
339 |
+
"from huggingface_hub import HfApi, create_repo, upload_folder\n",
|
340 |
+
"from transformers import BertForSequenceClassification, BertTokenizer\n",
|
341 |
+
"\n",
|
342 |
+
"def check_model_files(model_dir):\n",
|
343 |
+
" \\\"\\\"\\\"Check for required model files with support for both formats.\\\"\\\"\\\"\n",
|
344 |
+
" \n",
|
345 |
+
" # Required base files\n",
|
346 |
+
" required_base = ['config.json', 'vocab.txt', 'tokenizer_config.json']\n",
|
347 |
+
" \n",
|
348 |
+
" # Model files (at least one of these)\n",
|
349 |
+
" model_files = ['model.safetensors', 'pytorch_model.bin']\n",
|
350 |
+
" \n",
|
351 |
+
" missing_base = []\n",
|
352 |
+
" for file in required_base:\n",
|
353 |
+
" if not os.path.exists(os.path.join(model_dir, file)):\n",
|
354 |
+
" missing_base.append(file)\n",
|
355 |
+
" \n",
|
356 |
+
" # Check for at least one model file\n",
|
357 |
+
" found_model_files = []\n",
|
358 |
+
" for f in model_files:\n",
|
359 |
+
" if os.path.exists(os.path.join(model_dir, f)):\n",
|
360 |
+
" found_model_files.append(f)\n",
|
361 |
+
" \n",
|
362 |
+
" if missing_base:\n",
|
363 |
+
" print(f\"β Missing required files: {missing_base}\")\n",
|
364 |
+
" return False\n",
|
365 |
+
" \n",
|
366 |
+
" if not found_model_files:\n",
|
367 |
+
" print(f\"β No model file found. Expected one of: {model_files}\")\n",
|
368 |
+
" return False\n",
|
369 |
+
" \n",
|
370 |
+
" # Show found files\n",
|
371 |
+
" all_files = os.listdir(model_dir)\n",
|
372 |
+
" print(f\"β
Model files found: {all_files}\")\n",
|
373 |
+
" print(f\"β
Model weights format: {found_model_files[0]}\")\n",
|
374 |
+
" return True\n",
|
375 |
+
"\n",
|
376 |
+
"def test_model_loading(model_dir):\n",
|
377 |
+
" \\\"\\\"\\\"Test model loading to verify it works.\\\"\\\"\\\"\n",
|
378 |
+
" try:\n",
|
379 |
+
" print(\"π§ͺ Model loading test...\")\n",
|
380 |
+
" \n",
|
381 |
+
" # Load model and tokenizer\n",
|
382 |
+
" model = BertForSequenceClassification.from_pretrained(model_dir)\n",
|
383 |
+
" tokenizer = BertTokenizer.from_pretrained(model_dir)\n",
|
384 |
+
" \n",
|
385 |
+
" print(f\"β
Model loaded: {model.config.num_labels} classes, {model.config.hidden_size} hidden\")\n",
|
386 |
+
" print(f\"β
Tokenizer loaded: {len(tokenizer)} tokens\")\n",
|
387 |
+
" \n",
|
388 |
+
" # Quick inference test\n",
|
389 |
+
" text = \"A method for producing synthetic materials\"\n",
|
390 |
+
" inputs = tokenizer(text, return_tensors=\"pt\", max_length=512, truncation=True, padding=True)\n",
|
391 |
+
" \n",
|
392 |
+
" import torch\n",
|
393 |
+
" with torch.no_grad():\n",
|
394 |
+
" outputs = model(**inputs)\n",
|
395 |
+
" predictions = outputs.logits.softmax(dim=-1)\n",
|
396 |
+
" \n",
|
397 |
+
" print(f\"β
Inference test successful: shape {predictions.shape}\")\n",
|
398 |
+
" return True\n",
|
399 |
+
" \n",
|
400 |
+
" except Exception as e:\n",
|
401 |
+
" print(f\"β Test error: {e}\")\n",
|
402 |
+
" return False\n",
|
403 |
+
"\n",
|
404 |
+
"def upload_to_huggingface(model_dir, repo_name, token, private=False):\n",
|
405 |
+
" \\\"\\\"\\\"Upload model to Hugging Face Hub with support for all formats.\\\"\\\"\\\"\n",
|
406 |
+
" \n",
|
407 |
+
" print(\"π Upload to Hugging Face Hub\")\n",
|
408 |
+
" print(f\"π Model: {model_dir}\")\n",
|
409 |
+
" print(f\"π·οΈ Repository: {repo_name}\")\n",
|
410 |
+
" print(f\"π Private: {private}\")\n",
|
411 |
+
" \n",
|
412 |
+
" # File verification\n",
|
413 |
+
" if not check_model_files(model_dir):\n",
|
414 |
+
" return False\n",
|
415 |
+
" \n",
|
416 |
+
" # Loading test\n",
|
417 |
+
" if not test_model_loading(model_dir):\n",
|
418 |
+
" print(\"β οΈ Warning: Model doesn't load correctly, but continuing upload...\")\n",
|
419 |
+
" \n",
|
420 |
+
" try:\n",
|
421 |
+
" # Initialize API\n",
|
422 |
+
" api = HfApi(token=token)\n",
|
423 |
+
" \n",
|
424 |
+
" # Check connection\n",
|
425 |
+
" user_info = api.whoami()\n",
|
426 |
+
" print(f\"β
Connected as: {user_info['name']}\")\n",
|
427 |
+
" \n",
|
428 |
+
" # Create or verify repository\n",
|
429 |
+
" try:\n",
|
430 |
+
" create_repo(repo_name, token=token, private=private, exist_ok=True)\n",
|
431 |
+
" print(f\"β
Repository created/verified: https://huggingface.co/{repo_name}\")\n",
|
432 |
+
" except Exception as e:\n",
|
433 |
+
" print(f\"β οΈ Repository warning: {e}\")\n",
|
434 |
+
" \n",
|
435 |
+
" # Upload complete folder\n",
|
436 |
+
" print(\"π€ Uploading files...\")\n",
|
437 |
+
" \n",
|
438 |
+
" # Determine model format\n",
|
439 |
+
" model_format = \"SafeTensors\" if os.path.exists(os.path.join(model_dir, 'model.safetensors')) else \"PyTorch\"\n",
|
440 |
+
" \n",
|
441 |
+
" # Create informative commit message\n",
|
442 |
+
" commit_message = f\\\"\\\"\\\"Upload PatentBERT PyTorch model\n",
|
443 |
+
"\n",
|
444 |
+
"BERT model fine-tuned for patent classification, converted from TensorFlow to PyTorch.\n",
|
445 |
+
"\n",
|
446 |
+
"Specifications:\n",
|
447 |
+
"- Format: {model_format}\n",
|
448 |
+
"- Classes: Auto-detected from config.json \n",
|
449 |
+
"- Conversion: TensorFlow 1.15 β PyTorch via transformers\n",
|
450 |
+
"- CPC Labels: Real Cooperative Patent Classification labels included\n",
|
451 |
+
"\n",
|
452 |
+
"Included files:\n",
|
453 |
+
"{', '.join(sorted(os.listdir(model_dir)))}\n",
|
454 |
+
"\\\"\\\"\\\"\n",
|
455 |
+
" \n",
|
456 |
+
" upload_folder(\n",
|
457 |
+
" folder_path=model_dir,\n",
|
458 |
+
" repo_id=repo_name,\n",
|
459 |
+
" token=token,\n",
|
460 |
+
" commit_message=commit_message,\n",
|
461 |
+
" ignore_patterns=[\".git\", \".gitattributes\", \"*.tmp\"]\n",
|
462 |
+
" )\n",
|
463 |
+
" \n",
|
464 |
+
" print(\"π Upload completed successfully!\")\n",
|
465 |
+
" print(f\"π Model available at: https://huggingface.co/{repo_name}\")\n",
|
466 |
+
" \n",
|
467 |
+
" # Usage instructions\n",
|
468 |
+
" print(\"\\\\nπ Usage instructions:\")\n",
|
469 |
+
" print(f\"from transformers import BertForSequenceClassification, BertTokenizer\")\n",
|
470 |
+
" print(f\"model = BertForSequenceClassification.from_pretrained('{repo_name}')\")\n",
|
471 |
+
" print(f\"tokenizer = BertTokenizer.from_pretrained('{repo_name}')\")\n",
|
472 |
+
" \n",
|
473 |
+
" return True\n",
|
474 |
+
" \n",
|
475 |
+
" except Exception as e:\n",
|
476 |
+
" print(f\"β Upload error: {e}\")\n",
|
477 |
+
" import traceback\n",
|
478 |
+
" traceback.print_exc()\n",
|
479 |
+
" return False\n",
|
480 |
+
"\n",
|
481 |
+
"def main():\n",
|
482 |
+
" if len(sys.argv) != 4:\n",
|
483 |
+
" print(\"Usage: python upload_to_hf.py <model_dir> <repo_name> <hf_token>\")\n",
|
484 |
+
" print(\"Example: python upload_to_hf.py ./pytorch_model ZoeYou/patentbert-pytorch hf_xxx...\")\n",
|
485 |
+
" sys.exit(1)\n",
|
486 |
+
" \n",
|
487 |
+
" model_dir = sys.argv[1]\n",
|
488 |
+
" repo_name = sys.argv[2]\n",
|
489 |
+
" token = sys.argv[3]\n",
|
490 |
+
" \n",
|
491 |
+
" if not os.path.exists(model_dir):\n",
|
492 |
+
" print(f\"β Directory not found: {model_dir}\")\n",
|
493 |
+
" sys.exit(1)\n",
|
494 |
+
" \n",
|
495 |
+
" success = upload_to_huggingface(model_dir, repo_name, token, private=False)\n",
|
496 |
+
" \n",
|
497 |
+
" if success:\n",
|
498 |
+
" print(\"\\\\nβ
UPLOAD SUCCESSFUL!\")\n",
|
499 |
+
" else:\n",
|
500 |
+
" print(\"\\\\nβ UPLOAD FAILED!\")\n",
|
501 |
+
" sys.exit(1)\n",
|
502 |
+
"\n",
|
503 |
+
"if __name__ == \"__main__\":\n",
|
504 |
+
" # Import torch for loading test\n",
|
505 |
+
" try:\n",
|
506 |
+
" import torch\n",
|
507 |
+
" except ImportError:\n",
|
508 |
+
" print(\"β οΈ torch not available, loading test skipped\")\n",
|
509 |
+
" \n",
|
510 |
+
" main()\n",
|
511 |
+
"\"\"\"\n",
|
512 |
+
"\n",
|
513 |
+
"# Save the corrected upload script\n",
|
514 |
+
"with open('/tmp/upload_to_hf_corrected.py', 'w', encoding='utf-8') as f:\n",
|
515 |
+
" f.write(corrected_upload_script)\n",
|
516 |
+
"\n",
|
517 |
+
"# Also overwrite the original script\n",
|
518 |
+
"with open('/tmp/upload_to_hf.py', 'w', encoding='utf-8') as f:\n",
|
519 |
+
" f.write(corrected_upload_script)\n",
|
520 |
+
"\n",
|
521 |
+
"print(\"β
CORRECTED upload script created!\")\n",
|
522 |
+
"print(\"\\nπ§ Key corrections:\")\n",
|
523 |
+
"print(\" β
Accepts BOTH model.safetensors AND pytorch_model.bin\")\n",
|
524 |
+
"print(\" β
Automatically detects model format\")\n",
|
525 |
+
"print(\" β
Improved error messages\")\n",
|
526 |
+
"print(\" β
Better commit message with format info\")\n",
|
527 |
+
"print(\" β
Proper torch import for testing\")\n",
|
528 |
+
"\n",
|
529 |
+
"print(\"\\nπ NOW RUN THIS CORRECTED COMMAND:\")\n",
|
530 |
+
"print(\" python /tmp/upload_to_hf.py patentbert_conversion/pytorch_model ZoeYou/patentbert-pytorch xxxxx\")\n",
|
531 |
+
"\n",
|
532 |
+
"print(\"\\nπ‘ Or use the new corrected script:\")\n",
|
533 |
+
"print(\" python /tmp/upload_to_hf_corrected.py patentbert_conversion/pytorch_model ZoeYou/patentbert-pytorch xxxxx\")"
|
534 |
+
]
|
535 |
+
},
|
536 |
+
{
|
537 |
+
"cell_type": "code",
|
538 |
+
"execution_count": null,
|
539 |
+
"metadata": {},
|
540 |
+
"outputs": [],
|
541 |
+
"source": [
|
542 |
+
"# π UPLOAD SUCCESS! Let's test the uploaded model\n",
|
543 |
+
"\n",
|
544 |
+
"print(\"π Upload successful! Testing the uploaded model from Hugging Face...\")\n",
|
545 |
+
"\n",
|
546 |
+
"# Test the uploaded model\n",
|
547 |
+
"\n",
|
548 |
+
"from transformers import BertForSequenceClassification, BertTokenizer\n",
|
549 |
+
"import torch\n",
|
550 |
+
"\n",
|
551 |
+
"print(\"π Testing uploaded PatentBERT model from Hugging Face...\")\n",
|
552 |
+
"\n",
|
553 |
+
"try:\n",
|
554 |
+
" # Load model and tokenizer from Hugging Face Hub\n",
|
555 |
+
" model = BertForSequenceClassification.from_pretrained('ZoeYou/patentbert-pytorch')\n",
|
556 |
+
" tokenizer = BertTokenizer.from_pretrained('ZoeYou/patentbert-pytorch')\n",
|
557 |
+
" \n",
|
558 |
+
" print(f\"β
Model loaded: {model.config.num_labels} classes\")\n",
|
559 |
+
" print(f\"β
Tokenizer loaded: {len(tokenizer)} tokens\")\n",
|
560 |
+
" \n",
|
561 |
+
" # Test inference\n",
|
562 |
+
" text = \"A method for producing synthetic materials with enhanced properties\"\n",
|
563 |
+
" inputs = tokenizer(text, return_tensors=\"pt\", max_length=512, truncation=True, padding=True)\n",
|
564 |
+
" \n",
|
565 |
+
" with torch.no_grad():\n",
|
566 |
+
" outputs = model(**inputs)\n",
|
567 |
+
" predictions = outputs.logits.softmax(dim=-1)\n",
|
568 |
+
" \n",
|
569 |
+
" # Get top prediction\n",
|
570 |
+
" predicted_class_id = predictions.argmax().item()\n",
|
571 |
+
" confidence = predictions.max().item()\n",
|
572 |
+
" \n",
|
573 |
+
" # Use real CPC labels if available\n",
|
574 |
+
" if hasattr(model.config, 'id2label') and model.config.id2label:\n",
|
575 |
+
" predicted_label = model.config.id2label[predicted_class_id]\n",
|
576 |
+
" print(f\"β
Predicted CPC class: {predicted_label} (ID: {predicted_class_id})\")\n",
|
577 |
+
" else:\n",
|
578 |
+
" print(f\"β
Predicted class ID: {predicted_class_id}\")\n",
|
579 |
+
" \n",
|
580 |
+
" print(f\"β
Confidence: {confidence:.2%}\")\n",
|
581 |
+
" print(\"π Model works perfectly from Hugging Face!\")\n",
|
582 |
+
" \n",
|
583 |
+
"except Exception as e:\n",
|
584 |
+
" print(f\"β Error: {e}\")\n",
|
585 |
+
"\n",
|
586 |
+
"\n",
|
587 |
+
"print(\"π Model test code ready. Your model is now live at:\")\n",
|
588 |
+
"print(\"π https://huggingface.co/ZoeYou/patentbert-pytorch\")\n",
|
589 |
+
"\n",
|
590 |
+
"print(\"\\\\nπ Quick usage example:\")\n"
|
591 |
+
]
|
592 |
+
},
|
593 |
+
{
|
594 |
+
"cell_type": "code",
|
595 |
+
"execution_count": 2,
|
596 |
+
"metadata": {},
|
597 |
+
"outputs": [
|
598 |
+
{
|
599 |
+
"name": "stdout",
|
600 |
+
"output_type": "stream",
|
601 |
+
"text": [
|
602 |
+
"π CONVERSION SUCCESSFUL! Upload script correction...\n",
|
603 |
+
"β
CORRECTED upload script created!\n",
|
604 |
+
"\n",
|
605 |
+
"π§ Applied corrections:\n",
|
606 |
+
" β
Accepts model.safetensors AND pytorch_model.bin\n",
|
607 |
+
" β
Model loading test before upload\n",
|
608 |
+
" β
Robust file verification\n",
|
609 |
+
" β
Informative commit message\n",
|
610 |
+
" β
Usage instructions included\n",
|
611 |
+
"\n",
|
612 |
+
"π CORRECTED COMMAND:\n",
|
613 |
+
" python upload_to_hf.py patentbert_conversion/pytorch_model ZoeYou/patentbert-pytorch xxxxx\n"
|
614 |
+
]
|
615 |
+
}
|
616 |
+
],
|
617 |
+
"source": [
|
618 |
+
"# step 4: Provide usage example for the uploaded model\n",
|
619 |
+
"\n",
|
620 |
+
"# π CONVERSION SUCCESS! Upload script correction\n",
|
621 |
+
"\n",
|
622 |
+
"print(\"π CONVERSION SUCCESSFUL! Upload script correction...\")\n",
|
623 |
+
"\n",
|
624 |
+
"upload_script = \"\"\"#!/usr/bin/env python3\n",
|
625 |
+
"import os\n",
|
626 |
+
"import sys\n",
|
627 |
+
"from huggingface_hub import HfApi, create_repo, upload_folder\n",
|
628 |
+
"from transformers import BertForSequenceClassification, BertTokenizer\n",
|
629 |
+
"\n",
|
630 |
+
"def check_model_files(model_dir):\n",
|
631 |
+
" \\\"\\\"\\\"Check for required model files.\\\"\\\"\\\"\n",
|
632 |
+
" \n",
|
633 |
+
" # Required base files\n",
|
634 |
+
" required_base = ['config.json', 'vocab.txt', 'tokenizer_config.json']\n",
|
635 |
+
" \n",
|
636 |
+
" # Model files (at least one of these)\n",
|
637 |
+
" model_files = ['model.safetensors', 'pytorch_model.bin']\n",
|
638 |
+
" \n",
|
639 |
+
" missing_base = []\n",
|
640 |
+
" for file in required_base:\n",
|
641 |
+
" if not os.path.exists(os.path.join(model_dir, file)):\n",
|
642 |
+
" missing_base.append(file)\n",
|
643 |
+
" \n",
|
644 |
+
" # Check for at least one model file\n",
|
645 |
+
" has_model_file = any(os.path.exists(os.path.join(model_dir, f)) for f in model_files)\n",
|
646 |
+
" \n",
|
647 |
+
" if missing_base:\n",
|
648 |
+
" print(f\"β Missing required files: {missing_base}\")\n",
|
649 |
+
" return False\n",
|
650 |
+
" \n",
|
651 |
+
" if not has_model_file:\n",
|
652 |
+
" print(f\"β No model file found. Expected: {model_files}\")\n",
|
653 |
+
" return False\n",
|
654 |
+
" \n",
|
655 |
+
" # Show found files\n",
|
656 |
+
" found_files = []\n",
|
657 |
+
" for file in os.listdir(model_dir):\n",
|
658 |
+
" if os.path.isfile(os.path.join(model_dir, file)):\n",
|
659 |
+
" found_files.append(file)\n",
|
660 |
+
" \n",
|
661 |
+
" print(f\"β
Model files found: {found_files}\")\n",
|
662 |
+
" return True\n",
|
663 |
+
"\n",
|
664 |
+
"def test_model_loading(model_dir):\n",
|
665 |
+
" \\\"\\\"\\\"Test model loading to verify it works.\\\"\\\"\\\"\n",
|
666 |
+
" try:\n",
|
667 |
+
" print(\"π§ͺ Model loading test...\")\n",
|
668 |
+
" \n",
|
669 |
+
" # Load model and tokenizer\n",
|
670 |
+
" model = BertForSequenceClassification.from_pretrained(model_dir)\n",
|
671 |
+
" tokenizer = BertTokenizer.from_pretrained(model_dir)\n",
|
672 |
+
" \n",
|
673 |
+
" print(f\"β
Model loaded: {model.config.num_labels} classes, {model.config.hidden_size} hidden\")\n",
|
674 |
+
" print(f\"β
Tokenizer loaded: {len(tokenizer)} tokens\")\n",
|
675 |
+
" \n",
|
676 |
+
" # Quick inference test\n",
|
677 |
+
" text = \"A method for producing synthetic materials\"\n",
|
678 |
+
" inputs = tokenizer(text, return_tensors=\"pt\", max_length=512, truncation=True, padding=True)\n",
|
679 |
+
" \n",
|
680 |
+
" with torch.no_grad():\n",
|
681 |
+
" outputs = model(**inputs)\n",
|
682 |
+
" predictions = outputs.logits.softmax(dim=-1)\n",
|
683 |
+
" \n",
|
684 |
+
" print(f\"β
Inference test successful: shape {predictions.shape}\")\n",
|
685 |
+
" return True\n",
|
686 |
+
" \n",
|
687 |
+
" except Exception as e:\n",
|
688 |
+
" print(f\"β Test error: {e}\")\n",
|
689 |
+
" return False\n",
|
690 |
+
"\n",
|
691 |
+
"def upload_to_huggingface(model_dir, repo_name, token, private=False):\n",
|
692 |
+
" \\\"\\\"\\\"Upload model to Hugging Face Hub.\\\"\\\"\\\"\n",
|
693 |
+
" \n",
|
694 |
+
" print(\"π Upload to Hugging Face Hub\")\n",
|
695 |
+
" print(f\"π Model: {model_dir}\")\n",
|
696 |
+
" print(f\"π·οΈ Repository: {repo_name}\")\n",
|
697 |
+
" print(f\"π Private: {private}\")\n",
|
698 |
+
" \n",
|
699 |
+
" # File verification\n",
|
700 |
+
" if not check_model_files(model_dir):\n",
|
701 |
+
" return False\n",
|
702 |
+
" \n",
|
703 |
+
" # Loading test\n",
|
704 |
+
" if not test_model_loading(model_dir):\n",
|
705 |
+
" print(\"β οΈ Warning: Model doesn't load correctly, but continuing upload...\")\n",
|
706 |
+
" \n",
|
707 |
+
" try:\n",
|
708 |
+
" # Initialize API\n",
|
709 |
+
" api = HfApi(token=token)\n",
|
710 |
+
" \n",
|
711 |
+
" # Check connection\n",
|
712 |
+
" user_info = api.whoami()\n",
|
713 |
+
" print(f\"β
Connected as: {user_info['name']}\")\n",
|
714 |
+
" \n",
|
715 |
+
" # Create or verify repository\n",
|
716 |
+
" try:\n",
|
717 |
+
" create_repo(repo_name, token=token, private=private, exist_ok=True)\n",
|
718 |
+
" print(f\"β
Repository created/verified: https://huggingface.co/{repo_name}\")\n",
|
719 |
+
" except Exception as e:\n",
|
720 |
+
" print(f\"β οΈ Repository warning: {e}\")\n",
|
721 |
+
" \n",
|
722 |
+
" # Upload complete folder\n",
|
723 |
+
" print(\"π€ Uploading files...\")\n",
|
724 |
+
" \n",
|
725 |
+
" # Create informative commit message\n",
|
726 |
+
" commit_message = f\\\"\\\"\\\"Upload PatentBERT PyTorch model\n",
|
727 |
+
"\n",
|
728 |
+
"BERT model fine-tuned for patent classification, converted from TensorFlow to PyTorch.\n",
|
729 |
+
"\n",
|
730 |
+
"Specifications:\n",
|
731 |
+
"- Format: {'SafeTensors' if os.path.exists(os.path.join(model_dir, 'model.safetensors')) else 'PyTorch'}\n",
|
732 |
+
"- Classes: Auto-detected from config.json\n",
|
733 |
+
"- Conversion: TensorFlow 1.15 β PyTorch via transformers\n",
|
734 |
+
"\n",
|
735 |
+
"Included files:\n",
|
736 |
+
"{', '.join(os.listdir(model_dir))}\n",
|
737 |
+
"\\\"\\\"\\\"\n",
|
738 |
+
" \n",
|
739 |
+
" upload_folder(\n",
|
740 |
+
" folder_path=model_dir,\n",
|
741 |
+
" repo_id=repo_name,\n",
|
742 |
+
" token=token,\n",
|
743 |
+
" commit_message=commit_message,\n",
|
744 |
+
" ignore_patterns=[\".git\", \".gitattributes\", \"*.tmp\"]\n",
|
745 |
+
" )\n",
|
746 |
+
" \n",
|
747 |
+
" print(\"π Upload completed successfully!\")\n",
|
748 |
+
" print(f\"π Model available at: https://huggingface.co/{repo_name}\")\n",
|
749 |
+
" \n",
|
750 |
+
" # Usage instructions\n",
|
751 |
+
" print(\"\\\\nπ Usage instructions:\")\n",
|
752 |
+
" print(f\"from transformers import BertForSequenceClassification, BertTokenizer\")\n",
|
753 |
+
" print(f\"model = BertForSequenceClassification.from_pretrained('{repo_name}')\")\n",
|
754 |
+
" print(f\"tokenizer = BertTokenizer.from_pretrained('{repo_name}')\")\n",
|
755 |
+
" \n",
|
756 |
+
" return True\n",
|
757 |
+
" \n",
|
758 |
+
" except Exception as e:\n",
|
759 |
+
" print(f\"β Upload error: {e}\")\n",
|
760 |
+
" return False\n",
|
761 |
+
"\n",
|
762 |
+
"def main():\n",
|
763 |
+
" if len(sys.argv) != 4:\n",
|
764 |
+
" print(\"Usage: python upload_to_hf.py <model_dir> <repo_name> <hf_token>\")\n",
|
765 |
+
" print(\"Example: python upload_to_hf.py ./pytorch_model ZoeYou/patentbert-pytorch hf_xxx...\")\n",
|
766 |
+
" sys.exit(1)\n",
|
767 |
+
" \n",
|
768 |
+
" model_dir = sys.argv[1]\n",
|
769 |
+
" repo_name = sys.argv[2]\n",
|
770 |
+
" token = sys.argv[3]\n",
|
771 |
+
" \n",
|
772 |
+
" if not os.path.exists(model_dir):\n",
|
773 |
+
" print(f\"β Directory not found: {model_dir}\")\n",
|
774 |
+
" sys.exit(1)\n",
|
775 |
+
" \n",
|
776 |
+
" success = upload_to_huggingface(model_dir, repo_name, token, private=False)\n",
|
777 |
+
" \n",
|
778 |
+
" if success:\n",
|
779 |
+
" print(\"\\\\nβ
UPLOAD SUCCESSFUL!\")\n",
|
780 |
+
" else:\n",
|
781 |
+
" print(\"\\\\nβ UPLOAD FAILED!\")\n",
|
782 |
+
" sys.exit(1)\n",
|
783 |
+
"\n",
|
784 |
+
"if __name__ == \"__main__\":\n",
|
785 |
+
" # Import torch for loading test\n",
|
786 |
+
" try:\n",
|
787 |
+
" import torch\n",
|
788 |
+
" except ImportError:\n",
|
789 |
+
" print(\"β οΈ torch not available, loading test skipped\")\n",
|
790 |
+
" \n",
|
791 |
+
" main()\n",
|
792 |
+
"\"\"\"\n",
|
793 |
+
"\n",
|
794 |
+
"# Save corrected upload script\n",
|
795 |
+
"with open('/tmp/upload_to_hf.py', 'w', encoding='utf-8') as f:\n",
|
796 |
+
" f.write(upload_script)\n",
|
797 |
+
"\n",
|
798 |
+
"print(\"β
CORRECTED upload script created!\")\n",
|
799 |
+
"print(\"\\nπ§ Applied corrections:\")\n",
|
800 |
+
"print(\" β
Accepts model.safetensors AND pytorch_model.bin\")\n",
|
801 |
+
"print(\" β
Model loading test before upload\")\n",
|
802 |
+
"print(\" β
Robust file verification\")\n",
|
803 |
+
"print(\" β
Informative commit message\")\n",
|
804 |
+
"print(\" β
Usage instructions included\")\n",
|
805 |
+
"\n",
|
806 |
+
"print(\"\\nπ CORRECTED COMMAND:\")\n",
|
807 |
+
"print(\" python upload_to_hf.py patentbert_conversion/pytorch_model ZoeYou/patentbert-pytorch xxxxx\")"
|
808 |
+
]
|
809 |
+
},
|
810 |
+
{
|
811 |
+
"cell_type": "code",
|
812 |
+
"execution_count": null,
|
813 |
+
"metadata": {},
|
814 |
+
"outputs": [],
|
815 |
+
"source": []
|
816 |
+
},
|
817 |
+
{
|
818 |
+
"cell_type": "markdown",
|
819 |
+
"metadata": {},
|
820 |
+
"source": [
|
821 |
+
"π― COMPLETE TENSORFLOW β PYTORCH CONVERSION GUIDE\n",
|
822 |
+
"\n",
|
823 |
+
"π 4-step process:\n",
|
824 |
+
"\n",
|
825 |
+
"1οΈβ£ **DOWNLOAD** (in this notebook)\n",
|
826 |
+
" β’ Run previous cells to download PatentBERT\n",
|
827 |
+
" β’ Model will be in ./\n",
|
828 |
+
"\n",
|
829 |
+
"2οΈβ£ **EXTRACTION** (in this notebook)\n",
|
830 |
+
" β’ Run TensorFlow weight extraction cell\n",
|
831 |
+
" β’ Weights will be extracted to /tmp/patentbert_conversion/tf_weights/\n",
|
832 |
+
"\n",
|
833 |
+
"3οΈβ£ **CONVERSION** (Python 3.8+ environment)\n",
|
834 |
+
" ```\n",
|
835 |
+
" bash /tmp/install_pytorch_env.sh\n",
|
836 |
+
" source patentbert_pytorch/bin/activate\n",
|
837 |
+
" python /tmp/convert_patentbert.py /tmp/patentbert_conversion/tf_weights /tmp/patentbert_conversion/pytorch_model\n",
|
838 |
+
" ```\n",
|
839 |
+
"\n",
|
840 |
+
"4οΈβ£ **TEST AND UPLOAD**\n",
|
841 |
+
"\n",
|
842 |
+
" `python /tmp/test_patentbert.py /tmp/patentbert_conversion/pytorch_model`\n",
|
843 |
+
"\n",
|
844 |
+
" `python /tmp/upload_to_hf.py /tmp/patentbert_conversion/pytorch_model username/patentbert-pytorch your_hf_token`\n",
|
845 |
+
"\n",
|
846 |
+
"π RESULT:\n",
|
847 |
+
"β’ PyTorch model ready for production\n",
|
848 |
+
"β’ Compatible with Hugging Face Transformers\n",
|
849 |
+
"β’ Publicly available on Hub\n",
|
850 |
+
"β’ Documentation and examples included\n",
|
851 |
+
"\n",
|
852 |
+
"π‘ TIP:\n",
|
853 |
+
"First create an account at https://huggingface.co/ and get your access token\n",
|
854 |
+
"from https://huggingface.co/settings/tokens\n"
|
855 |
+
]
|
856 |
+
},
|
857 |
+
{
|
858 |
+
"cell_type": "code",
|
859 |
+
"execution_count": 4,
|
860 |
+
"metadata": {},
|
861 |
+
"outputs": [
|
862 |
+
{
|
863 |
+
"name": "stdout",
|
864 |
+
"output_type": "stream",
|
865 |
+
"text": [
|
866 |
+
"π·οΈ Creating and adding CPC class labels...\n",
|
867 |
+
"β
Loaded 656 real CPC labels from PatentBERT\n",
|
868 |
+
"π Example labels from the real data:\n",
|
869 |
+
" 0: A01B - SOIL WORKING IN AGRICULTURE OR FORESTRY; PARTS, DETAILS, OR ACCESSORIES OF AGRIC...\n",
|
870 |
+
" 50: A46B - BRUSHES ...\n",
|
871 |
+
" 100: B07B - SEPERATING SOLIDS FROM SOLIDS BY SIEVING, SCREENING, OR SIFTING OR BY USING GAS ...\n",
|
872 |
+
" 200: B60Q - ARRANGEMENT OF SIGNALLING OR LIGHTING DEVICES, THE MOUNTING OR SUPPORTING THEREO...\n",
|
873 |
+
" 300: C10F - DRYING OR WORKING-UP OF PEAT...\n",
|
874 |
+
" 400: E04G - SCAFFOLDING; FORMS; SHUTTERING; BUILDING IMPLEMENTS OR OTHER BUILDING AIDS, OR T...\n",
|
875 |
+
" 500: F28B - STEAM OR VAPOUR CONDENSERS ...\n",
|
876 |
+
" 600: H01H - ELECTRIC SWITCHES; RELAYS; SELECTORS...\n",
|
877 |
+
" 655: Y10T - TECHNICAL SUBJECTS COVERED BY FORMER US CLASSIFICATION...\n",
|
878 |
+
"\n",
|
879 |
+
"β
Real CPC system structure:\n",
|
880 |
+
" π Total classes: 656\n",
|
881 |
+
" π Distribution by section:\n",
|
882 |
+
" A: 84 classes\n",
|
883 |
+
" B: 171 classes\n",
|
884 |
+
" C: 88 classes\n",
|
885 |
+
" D: 40 classes\n",
|
886 |
+
" E: 31 classes\n",
|
887 |
+
" F: 101 classes\n",
|
888 |
+
" G: 81 classes\n",
|
889 |
+
" H: 51 classes\n",
|
890 |
+
" Y: 9 classes\n",
|
891 |
+
"β
Labels saved to: /tmp/patentbert_conversion/pytorch_model/labels.json\n",
|
892 |
+
"β
Configuration updated with real CPC labels\n",
|
893 |
+
"β
README updated with REAL CPC label documentation\n",
|
894 |
+
"\n",
|
895 |
+
"π Added/updated files:\n",
|
896 |
+
" β’ labels.json - Complete mapping of 656 REAL CPC labels\n",
|
897 |
+
" β’ config.json - Updated configuration with authentic id2label/label2id\n",
|
898 |
+
" β’ README.md - Complete documentation with real CPC distribution\n",
|
899 |
+
"\n",
|
900 |
+
"π― Model is now ready for upload with AUTHENTIC CPC labels!\n"
|
901 |
+
]
|
902 |
+
}
|
903 |
+
],
|
904 |
+
"source": [
|
905 |
+
"# π·οΈ ADDING CLASS LABELS - Essential for prediction interpretation\n",
|
906 |
+
"\n",
|
907 |
+
"print(\"π·οΈ Creating and adding CPC class labels...\")\n",
|
908 |
+
"\n",
|
909 |
+
"# Load the REAL CPC labels from the original PatentBERT label file\n",
|
910 |
+
"import pandas as pd\n",
|
911 |
+
"import json\n",
|
912 |
+
"import os\n",
|
913 |
+
"\n",
|
914 |
+
"# Load the real CPC labels\n",
|
915 |
+
"label_file_path = \"/home/yzuo/scratch/representation_learning/patentmapv1/PatentBert/labels_group_id.tsv\"\n",
|
916 |
+
"cpc_df = pd.read_csv(label_file_path, sep='\\t')\n",
|
917 |
+
"\n",
|
918 |
+
"print(f\"β
Loaded {len(cpc_df)} real CPC labels from PatentBERT\")\n",
|
919 |
+
"print(f\"π Example labels from the real data:\")\n",
|
920 |
+
"for i in [0, 50, 100, 200, 300, 400, 500, 600, 655]:\n",
|
921 |
+
" if i < len(cpc_df):\n",
|
922 |
+
" row = cpc_df.iloc[i]\n",
|
923 |
+
" print(f\" {i:3d}: {row['id']} - {row['title'][:80]}...\")\n",
|
924 |
+
"\n",
|
925 |
+
"# Extract labels and descriptions\n",
|
926 |
+
"cpc_labels = cpc_df['id'].tolist()\n",
|
927 |
+
"cpc_descriptions = [f\"{row['id']}: {row['title']}\" for _, row in cpc_df.iterrows()]\n",
|
928 |
+
"\n",
|
929 |
+
"print(f\"\\nβ
Real CPC system structure:\")\n",
|
930 |
+
"print(f\" π Total classes: {len(cpc_labels)}\")\n",
|
931 |
+
"\n",
|
932 |
+
"# Analyze the actual distribution by section\n",
|
933 |
+
"section_counts = {}\n",
|
934 |
+
"for label in cpc_labels:\n",
|
935 |
+
" section = label[0]\n",
|
936 |
+
" section_counts[section] = section_counts.get(section, 0) + 1\n",
|
937 |
+
"\n",
|
938 |
+
"print(f\" π Distribution by section:\")\n",
|
939 |
+
"for section, count in sorted(section_counts.items()):\n",
|
940 |
+
" print(f\" {section}: {count} classes\")\n",
|
941 |
+
"\n",
|
942 |
+
"# Create label configuration file\n",
|
943 |
+
"label_config = {\n",
|
944 |
+
" \"id2label\": {str(i): label for i, label in enumerate(cpc_labels)},\n",
|
945 |
+
" \"label2id\": {label: i for i, label in enumerate(cpc_labels)},\n",
|
946 |
+
" \"num_labels\": len(cpc_labels),\n",
|
947 |
+
" \"classification_type\": \"CPC\",\n",
|
948 |
+
" \"description\": \"Real Cooperative Patent Classification (CPC) labels from PatentBERT training data\"\n",
|
949 |
+
"}\n",
|
950 |
+
"\n",
|
951 |
+
"# Save to model directory\n",
|
952 |
+
"model_dir = \"/tmp/patentbert_conversion/pytorch_model\"\n",
|
953 |
+
"labels_file = os.path.join(model_dir, \"labels.json\")\n",
|
954 |
+
"\n",
|
955 |
+
"with open(labels_file, 'w', encoding='utf-8') as f:\n",
|
956 |
+
" json.dump(label_config, f, indent=2, ensure_ascii=False)\n",
|
957 |
+
"\n",
|
958 |
+
"print(f\"β
Labels saved to: {labels_file}\")\n",
|
959 |
+
"\n",
|
960 |
+
"# Update model configuration to include labels\n",
|
961 |
+
"config_file = os.path.join(model_dir, \"config.json\")\n",
|
962 |
+
"\n",
|
963 |
+
"if os.path.exists(config_file):\n",
|
964 |
+
" with open(config_file, 'r') as f:\n",
|
965 |
+
" config = json.load(f)\n",
|
966 |
+
" \n",
|
967 |
+
" # Add labels to config\n",
|
968 |
+
" config[\"id2label\"] = label_config[\"id2label\"]\n",
|
969 |
+
" config[\"label2id\"] = label_config[\"label2id\"]\n",
|
970 |
+
" \n",
|
971 |
+
" # Save updated config\n",
|
972 |
+
" with open(config_file, 'w', encoding='utf-8') as f:\n",
|
973 |
+
" json.dump(config, f, indent=2, ensure_ascii=False)\n",
|
974 |
+
" \n",
|
975 |
+
" print(\"β
Configuration updated with real CPC labels\")\n",
|
976 |
+
"else:\n",
|
977 |
+
" print(\"β οΈ config.json file not found\")\n",
|
978 |
+
"\n",
|
979 |
+
"# Create detailed README with REAL CPC labels and distribution\n",
|
980 |
+
"section_descriptions = {\n",
|
981 |
+
" 'A': 'Human Necessities - Agriculture, Food, Health, Sports',\n",
|
982 |
+
" 'B': 'Performing Operations; Transporting - Manufacturing, Transport',\n",
|
983 |
+
" 'C': 'Chemistry; Metallurgy - Chemical processes, Materials',\n",
|
984 |
+
" 'D': 'Textiles; Paper - Fibers, Fabrics, Paper-making',\n",
|
985 |
+
" 'E': 'Fixed Constructions - Building, Mining, Roads',\n",
|
986 |
+
" 'F': 'Mechanical Engineering; Lightning; Heating; Weapons; Blasting',\n",
|
987 |
+
" 'G': 'Physics - Optics, Acoustics, Computing, Measuring',\n",
|
988 |
+
" 'H': 'Electricity - Electronics, Power generation, Communication',\n",
|
989 |
+
" 'Y': 'General Tagging of New Technological Developments'\n",
|
990 |
+
"}\n",
|
991 |
+
"\n",
|
992 |
+
"readme_with_labels = f\"\"\"# PatentBERT - PyTorch\n",
|
993 |
+
"\n",
|
994 |
+
"BERT model specialized for patent classification using the **real CPC (Cooperative Patent Classification) system** from the original PatentBERT training data.\n",
|
995 |
+
"\n",
|
996 |
+
"## π Specifications\n",
|
997 |
+
"\n",
|
998 |
+
"- **Output classes**: {len(cpc_labels)} (real CPC labels)\n",
|
999 |
+
"- **Classification system**: CPC (Cooperative Patent Classification)\n",
|
1000 |
+
"- **Architecture**: BERT-base (768 hidden, 12 layers, 12 attention heads)\n",
|
1001 |
+
"- **Vocabulary**: 30,522 tokens\n",
|
1002 |
+
"- **Format**: SafeTensors\n",
|
1003 |
+
"\n",
|
1004 |
+
"## π·οΈ CPC Classes (Real Distribution)\n",
|
1005 |
+
"\n",
|
1006 |
+
"The model predicts classes according to the authentic CPC system used in PatentBERT training:\n",
|
1007 |
+
"\n",
|
1008 |
+
"### Main Sections (Actual Counts)\n",
|
1009 |
+
"\"\"\"\n",
|
1010 |
+
"\n",
|
1011 |
+
"# Add real distribution to README\n",
|
1012 |
+
"for section in ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'Y']:\n",
|
1013 |
+
" if section in section_counts:\n",
|
1014 |
+
" count = section_counts[section]\n",
|
1015 |
+
" desc = section_descriptions.get(section, f'Section {section}')\n",
|
1016 |
+
" readme_with_labels += f\"- **{section} ({count} classes)**: {desc}\\n\"\n",
|
1017 |
+
"\n",
|
1018 |
+
"readme_with_labels += f\"\"\"\n",
|
1019 |
+
"### Example Real Classes\n",
|
1020 |
+
"\n",
|
1021 |
+
"- `A01B`: SOIL WORKING IN AGRICULTURE OR FORESTRY\n",
|
1022 |
+
"- `B25J`: MANIPULATORS; CHAMBERS PROVIDED WITH MANIPULATION DEVICES\n",
|
1023 |
+
"- `C07D`: HETEROCYCLIC COMPOUNDS\n",
|
1024 |
+
"- `G06F`: ELECTRIC DIGITAL DATA PROCESSING\n",
|
1025 |
+
"- `H04L`: TRANSMISSION OF DIGITAL INFORMATION\n",
|
1026 |
+
"\n",
|
1027 |
+
"## π Usage\n",
|
1028 |
+
"\n",
|
1029 |
+
"```python\n",
|
1030 |
+
"from transformers import BertForSequenceClassification, BertTokenizer\n",
|
1031 |
+
"import json\n",
|
1032 |
+
"import torch\n",
|
1033 |
+
"\n",
|
1034 |
+
"# Load model and tokenizer\n",
|
1035 |
+
"model = BertForSequenceClassification.from_pretrained('ZoeYou/patentbert-pytorch')\n",
|
1036 |
+
"tokenizer = BertTokenizer.from_pretrained('ZoeYou/patentbert-pytorch')\n",
|
1037 |
+
"\n",
|
1038 |
+
"# Inference example\n",
|
1039 |
+
"text = \"A method for producing synthetic materials with enhanced thermal properties...\"\n",
|
1040 |
+
"inputs = tokenizer(text, return_tensors=\"pt\", max_length=512, truncation=True, padding=True)\n",
|
1041 |
+
"\n",
|
1042 |
+
"with torch.no_grad():\n",
|
1043 |
+
" outputs = model(**inputs)\n",
|
1044 |
+
" predictions = outputs.logits.softmax(dim=-1)\n",
|
1045 |
+
"\n",
|
1046 |
+
"# Get prediction\n",
|
1047 |
+
"predicted_class_id = predictions.argmax().item()\n",
|
1048 |
+
"confidence = predictions.max().item()\n",
|
1049 |
+
"\n",
|
1050 |
+
"# Use model labels (real CPC codes)\n",
|
1051 |
+
"predicted_label = model.config.id2label[predicted_class_id]\n",
|
1052 |
+
"\n",
|
1053 |
+
"\n",
|
1054 |
+
"print(f\"Predicted CPC class: {{predicted_label}} (ID: {{predicted_class_id}})\")\n",
|
1055 |
+
"print(f\"Confidence: {{confidence:.2%}}\")\n",
|
1056 |
+
"```\n",
|
1057 |
+
"\n",
|
1058 |
+
"## π Included Files\n",
|
1059 |
+
"\n",
|
1060 |
+
"- `model.safetensors`: Model weights (420 MB)\n",
|
1061 |
+
"- `config.json`: Configuration with integrated real CPC labels\n",
|
1062 |
+
"- `vocab.txt`: Tokenizer vocabulary\n",
|
1063 |
+
"- `tokenizer_config.json`: Tokenizer configuration\n",
|
1064 |
+
"- `labels.json`: Complete real CPC label mapping ({len(cpc_labels)} authentic labels)\n",
|
1065 |
+
"- `README.md`: This documentation\n",
|
1066 |
+
"\n",
|
1067 |
+
"## π¬ Performance\n",
|
1068 |
+
"\n",
|
1069 |
+
"This model was trained on a large patent corpus to automatically classify documents according to the real CPC system, using the exact same {len(cpc_labels)} CPC codes from the original PatentBERT training data.\n",
|
1070 |
+
"\n",
|
1071 |
+
"## π References\n",
|
1072 |
+
"\n",
|
1073 |
+
"- [Cooperative Patent Classification (CPC)](https://www.cooperativepatentclassification.org/)\n",
|
1074 |
+
"- [Original PatentBERT Paper](https://arxiv.org/abs/2103.02557)\n",
|
1075 |
+
"\n",
|
1076 |
+
"## π Citation\n",
|
1077 |
+
"\n",
|
1078 |
+
"If you use this model, please cite the original PatentBERT work and mention this PyTorch conversion.\n",
|
1079 |
+
"\"\"\"\n",
|
1080 |
+
"\n",
|
1081 |
+
"# Save updated README\n",
|
1082 |
+
"readme_file = os.path.join(model_dir, \"README.md\")\n",
|
1083 |
+
"with open(readme_file, 'w', encoding='utf-8') as f:\n",
|
1084 |
+
" f.write(readme_with_labels)\n",
|
1085 |
+
"\n",
|
1086 |
+
"print(\"β
README updated with REAL CPC label documentation\")\n",
|
1087 |
+
"\n",
|
1088 |
+
"# Summary of created/updated files\n",
|
1089 |
+
"print(\"\\nπ Added/updated files:\")\n",
|
1090 |
+
"print(f\" β’ labels.json - Complete mapping of {len(cpc_labels)} REAL CPC labels\")\n",
|
1091 |
+
"print(f\" β’ config.json - Updated configuration with authentic id2label/label2id\")\n",
|
1092 |
+
"print(f\" β’ README.md - Complete documentation with real CPC distribution\")\n",
|
1093 |
+
"\n",
|
1094 |
+
"print(\"\\nπ― Model is now ready for upload with AUTHENTIC CPC labels!\")"
|
1095 |
+
]
|
1096 |
+
},
|
1097 |
+
{
|
1098 |
+
"cell_type": "code",
|
1099 |
+
"execution_count": null,
|
1100 |
+
"metadata": {},
|
1101 |
+
"outputs": [
|
1102 |
+
{
|
1103 |
+
"name": "stdout",
|
1104 |
+
"output_type": "stream",
|
1105 |
+
"text": [
|
1106 |
+
"Predicted CPC class: A63B (ID: 76)\n",
|
1107 |
+
"Confidence: 99.51%\n"
|
1108 |
+
]
|
1109 |
+
}
|
1110 |
+
],
|
1111 |
+
"source": [
|
1112 |
+
"from transformers import BertForSequenceClassification, BertTokenizer\n",
|
1113 |
+
"import torch\n",
|
1114 |
+
"\n",
|
1115 |
+
"# Load model and tokenizer\n",
|
1116 |
+
"model = BertForSequenceClassification.from_pretrained('ZoeYou/patentbert-pytorch')\n",
|
1117 |
+
"tokenizer = BertTokenizer.from_pretrained('ZoeYou/patentbert-pytorch')\n",
|
1118 |
+
"\n",
|
1119 |
+
"# Inference example\n",
|
1120 |
+
"text = \"A device designed to spin in a user's hands may include a body with a centrally mounted ball bearing positioned within a center orifice of the body, wherein an outer race of the ball bearing is attached to the frame; a button made of a pair of bearing caps attached to one another through the ball bearing and clamped against an inner race of the ball bearing, such that when the button is held between a user's thumb and finger, the body freely rotates about the ball bearing; and a plurality of weights distributed at opposite ends of the body, creating at least a bipolar weight distribution.\"\n",
|
1121 |
+
"inputs = tokenizer(text, return_tensors=\"pt\", max_length=512, truncation=True, padding=True)\n",
|
1122 |
+
"\n",
|
1123 |
+
"with torch.no_grad():\n",
|
1124 |
+
" outputs = model(**inputs)\n",
|
1125 |
+
" predictions = outputs.logits.softmax(dim=-1)\n",
|
1126 |
+
"\n",
|
1127 |
+
"# Get prediction\n",
|
1128 |
+
"predicted_class_id = predictions.argmax().item()\n",
|
1129 |
+
"confidence = predictions.max().item()\n",
|
1130 |
+
"\n",
|
1131 |
+
"# Use model labels (real CPC codes)\n",
|
1132 |
+
"predicted_label = model.config.id2label[predicted_class_id]\n",
|
1133 |
+
"\n",
|
1134 |
+
"print(f\"Predicted CPC class: {predicted_label} (ID: {predicted_class_id})\")\n",
|
1135 |
+
"print(f\"Confidence: {confidence:.2%}\")\n"
|
1136 |
+
]
|
1137 |
+
},
|
1138 |
+
{
|
1139 |
+
"cell_type": "code",
|
1140 |
+
"execution_count": 7,
|
1141 |
+
"metadata": {},
|
1142 |
+
"outputs": [
|
1143 |
+
{
|
1144 |
+
"data": {
|
1145 |
+
"text/plain": [
|
1146 |
+
"'A63B'"
|
1147 |
+
]
|
1148 |
+
},
|
1149 |
+
"execution_count": 7,
|
1150 |
+
"metadata": {},
|
1151 |
+
"output_type": "execute_result"
|
1152 |
+
}
|
1153 |
+
],
|
1154 |
+
"source": [
|
1155 |
+
"model.config.id2label[76]"
|
1156 |
+
]
|
1157 |
+
},
|
1158 |
+
{
|
1159 |
+
"cell_type": "code",
|
1160 |
+
"execution_count": null,
|
1161 |
+
"metadata": {},
|
1162 |
+
"outputs": [],
|
1163 |
+
"source": []
|
1164 |
+
}
|
1165 |
+
],
|
1166 |
+
"metadata": {
|
1167 |
+
"accelerator": "GPU",
|
1168 |
+
"colab": {
|
1169 |
+
"collapsed_sections": [],
|
1170 |
+
"name": "PatentBERT",
|
1171 |
+
"provenance": []
|
1172 |
+
},
|
1173 |
+
"kernelspec": {
|
1174 |
+
"display_name": "simcse",
|
1175 |
+
"language": "python",
|
1176 |
+
"name": "python3"
|
1177 |
+
},
|
1178 |
+
"language_info": {
|
1179 |
+
"codemirror_mode": {
|
1180 |
+
"name": "ipython",
|
1181 |
+
"version": 3
|
1182 |
+
},
|
1183 |
+
"file_extension": ".py",
|
1184 |
+
"mimetype": "text/x-python",
|
1185 |
+
"name": "python",
|
1186 |
+
"nbconvert_exporter": "python",
|
1187 |
+
"pygments_lexer": "ipython3",
|
1188 |
+
"version": "3.9.23"
|
1189 |
+
}
|
1190 |
+
},
|
1191 |
+
"nbformat": 4,
|
1192 |
+
"nbformat_minor": 0
|
1193 |
+
}
|