File size: 2,793 Bytes
9df7f3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Example script for deploying the Shopping Assistant model to Hugging Face Hub
"""
import os
import argparse
import subprocess
import sys

def check_requirements():
    """
    Check if the required packages are installed
    """
    try:
        import huggingface_hub
        import transformers
        return True
    except ImportError as e:
        print(f"Error: {e}")
        print("Please install the required packages:")
        print("pip install huggingface_hub transformers")
        return False

def deploy_model():
    """
    Deploy the model to Hugging Face Hub
    """
    parser = argparse.ArgumentParser(description="Deploy the Shopping Assistant model to Hugging Face Hub")
    parser.add_argument("--repo-name", type=str, required=True, help="Hugging Face Hub repository name (e.g., username/model-name)")
    parser.add_argument("--token", type=str, help="Hugging Face API token")
    parser.add_argument("--private", action="store_true", help="Make the repository private")
    args = parser.parse_args()
    
    # Check if the deploy_to_huggingface.py script exists
    script_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "deploy_to_huggingface.py")
    if not os.path.exists(script_path):
        print(f"Error: deploy_to_huggingface.py not found at {script_path}")
        print("Please make sure the script exists in the project directory")
        return False
    
    # Build the command
    cmd = [sys.executable, script_path, "--repo-name", args.repo_name, "--create-inference"]
    
    if args.token:
        cmd.extend(["--token", args.token])
    
    if args.private:
        cmd.append("--private")
    
    # Execute the command
    print(f"Deploying model to {args.repo_name}...")
    try:
        subprocess.run(cmd, check=True)
        print("\nDeployment successful!")
        print(f"You can access your model at: https://huggingface.co/{args.repo_name}")
        print("\nTo use the model for inference:")
        print(f"1. Visit https://huggingface.co/{args.repo_name}")
        print("2. Use the inference widget on the model page")
        print("3. Or use the API with the following code:")
        print(f"""
import requests

API_URL = "https://api-inference.huggingface.co/models/{args.repo_name}"
headers = {{"Authorization": "Bearer YOUR_API_TOKEN"}}

def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.json()

output = query({{
    "inputs": "I'm looking for headphones"
}})
print(output)
        """)
        return True
    except subprocess.CalledProcessError as e:
        print(f"Error deploying model: {e}")
        return False

if __name__ == "__main__":
    if check_requirements():
        deploy_model()