File size: 6,539 Bytes
f0f2280
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Script to prepare and package the B2B Ecommerce NER model for Hugging Face upload
"""

import os
import shutil
import json
from pathlib import Path
import sys

# Add parent directory to path to import our modules
sys.path.append(str(Path(__file__).parent.parent))


def prepare_huggingface_model():
    """Prepare the model for Hugging Face upload"""
    
    print("Preparing B2B Ecommerce NER model for Hugging Face...")
    
    # Paths
    base_dir = Path(__file__).parent.parent
    hf_dir = Path(__file__).parent
    spacy_model_path = base_dir / "models" / "food_ner_model"
    catalog_path = base_dir / "data" / "product_catalog.csv"
    
    print(f"Base directory: {base_dir}")
    print(f"HuggingFace directory: {hf_dir}")
    print(f"spaCy model path: {spacy_model_path}")
    print(f"Catalog path: {catalog_path}")
    
    # Check if required files exist
    if not spacy_model_path.exists():
        print(f"❌ spaCy model not found at {spacy_model_path}")
        print("Please train the model first using: python src/train_model.py")
        return False
    
    if not catalog_path.exists():
        print(f"❌ Product catalog not found at {catalog_path}")
        print("Please ensure product_catalog.csv exists in the data directory")
        return False
    
    print("βœ… Required files found")
    
    # Copy spaCy model
    target_spacy_path = hf_dir / "spacy_model"
    if target_spacy_path.exists():
        shutil.rmtree(target_spacy_path)
    
    print(f"Copying spaCy model to {target_spacy_path}")
    shutil.copytree(spacy_model_path, target_spacy_path)
    
    # Copy product catalog
    target_catalog_path = hf_dir / "product_catalog.csv"
    print(f"Copying product catalog to {target_catalog_path}")
    shutil.copy(catalog_path, target_catalog_path)
    
    # Update model configuration with actual paths
    config_path = hf_dir / "config.json"
    with open(config_path, 'r') as f:
        config = json.load(f)
    
    config["spacy_model_path"] = "spacy_model"
    config["catalog_path"] = "product_catalog.csv"
    config["prepared_for_upload"] = True
    
    with open(config_path, 'w') as f:
        json.dump(config, f, indent=2)
    
    print("βœ… Model prepared successfully!")
    print("\nNext steps:")
    print("1. Test the model using: python huggingface_model/example.py")
    print("2. Upload to Hugging Face using the upload script")
    
    return True


def test_prepared_model():
    """Test the prepared model"""
    print("\nTesting prepared model...")
    
    try:
        from model import B2BEcommerceNER
        
        # Initialize model with local paths
        model = B2BEcommerceNER(
            model_path="spacy_model",
            catalog_path="product_catalog.csv"
        )
        
        # Test prediction
        test_texts = ["Order 5 Coke Zero 650ML"]
        results = model.predict(test_texts)
        
        print("βœ… Model test successful!")
        print("Sample result:", json.dumps(results[0], indent=2, default=str))
        
        return True
        
    except Exception as e:
        print(f"❌ Model test failed: {e}")
        return False


def create_upload_script():
    """Create a script for uploading to Hugging Face"""
    
    upload_script = '''#!/usr/bin/env python3
"""
Upload the B2B Ecommerce NER model to Hugging Face Hub
"""

from huggingface_hub import HfApi, create_repo
import os
from pathlib import Path


def upload_to_huggingface(repo_name: str, token: str = None):
    """
    Upload the model to Hugging Face Hub
    
    Args:
        repo_name: Name of the repository (e.g., "username/b2b-ecommerce-ner")
        token: Hugging Face token (or set HF_TOKEN environment variable)
    """
    
    if token is None:
        token = os.getenv("HF_TOKEN")
        if not token:
            print("Please provide a Hugging Face token or set HF_TOKEN environment variable")
            return False
    
    api = HfApi()
    
    try:
        # Create repository
        print(f"Creating repository: {repo_name}")
        create_repo(repo_name, token=token, exist_ok=True)
        
        # Upload all files in the current directory
        model_dir = Path(__file__).parent
        
        print("Uploading files...")
        api.upload_folder(
            folder_path=model_dir,
            repo_id=repo_name,
            token=token,
            repo_type="model"
        )
        
        print(f"βœ… Model uploaded successfully to: https://huggingface.co/{repo_name}")
        return True
        
    except Exception as e:
        print(f"❌ Upload failed: {e}")
        return False


if __name__ == "__main__":
    import sys
    
    if len(sys.argv) != 2:
        print("Usage: python upload.py <repo_name>")
        print("Example: python upload.py username/b2b-ecommerce-ner")
        sys.exit(1)
    
    repo_name = sys.argv[1]
    success = upload_to_huggingface(repo_name)
    
    if success:
        print("\\nYour model is now available on Hugging Face!")
        print(f"You can use it with: B2BEcommerceNER.from_pretrained('{repo_name}')")
    else:
        print("\\nUpload failed. Please check your token and try again.")
'''
    
    upload_script_path = Path(__file__).parent / "upload.py"
    with open(upload_script_path, 'w') as f:
        f.write(upload_script)
    
    # Make it executable
    os.chmod(upload_script_path, 0o755)
    
    print(f"βœ… Upload script created at {upload_script_path}")


def main():
    """Main function to prepare the model"""
    
    print("B2B Ecommerce NER - Hugging Face Preparation")
    print("=" * 50)
    
    # Change to the HuggingFace directory
    os.chdir(Path(__file__).parent)
    
    # Prepare the model
    if not prepare_huggingface_model():
        return False
    
    # Test the model
    if not test_prepared_model():
        return False
    
    # Create upload script
    create_upload_script()
    
    print("\nπŸŽ‰ Model preparation complete!")
    print("\nFiles in huggingface_model directory:")
    for file_path in Path(".").iterdir():
        if file_path.is_file():
            print(f"  πŸ“„ {file_path.name}")
        elif file_path.is_dir():
            print(f"  πŸ“ {file_path.name}/")
    
    print("\nπŸ“š Usage instructions:")
    print("1. Test locally: python example.py")
    print("2. Upload to HF: python upload.py username/model-name")
    print("3. Use remotely: B2BEcommerceNER.from_pretrained('username/model-name')")
    
    return True


if __name__ == "__main__":
    main()