|
|
|
""" |
|
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() |
|
|
|
|
|
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 |
|
|
|
|
|
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") |
|
|
|
|
|
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() |
|
|