Upload deploy_example.py with huggingface_hub
Browse files- deploy_example.py +82 -0
deploy_example.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Example script for deploying the Shopping Assistant model to Hugging Face Hub
|
| 4 |
+
"""
|
| 5 |
+
import os
|
| 6 |
+
import argparse
|
| 7 |
+
import subprocess
|
| 8 |
+
import sys
|
| 9 |
+
|
| 10 |
+
def check_requirements():
|
| 11 |
+
"""
|
| 12 |
+
Check if the required packages are installed
|
| 13 |
+
"""
|
| 14 |
+
try:
|
| 15 |
+
import huggingface_hub
|
| 16 |
+
import transformers
|
| 17 |
+
return True
|
| 18 |
+
except ImportError as e:
|
| 19 |
+
print(f"Error: {e}")
|
| 20 |
+
print("Please install the required packages:")
|
| 21 |
+
print("pip install huggingface_hub transformers")
|
| 22 |
+
return False
|
| 23 |
+
|
| 24 |
+
def deploy_model():
|
| 25 |
+
"""
|
| 26 |
+
Deploy the model to Hugging Face Hub
|
| 27 |
+
"""
|
| 28 |
+
parser = argparse.ArgumentParser(description="Deploy the Shopping Assistant model to Hugging Face Hub")
|
| 29 |
+
parser.add_argument("--repo-name", type=str, required=True, help="Hugging Face Hub repository name (e.g., username/model-name)")
|
| 30 |
+
parser.add_argument("--token", type=str, help="Hugging Face API token")
|
| 31 |
+
parser.add_argument("--private", action="store_true", help="Make the repository private")
|
| 32 |
+
args = parser.parse_args()
|
| 33 |
+
|
| 34 |
+
# Check if the deploy_to_huggingface.py script exists
|
| 35 |
+
script_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "deploy_to_huggingface.py")
|
| 36 |
+
if not os.path.exists(script_path):
|
| 37 |
+
print(f"Error: deploy_to_huggingface.py not found at {script_path}")
|
| 38 |
+
print("Please make sure the script exists in the project directory")
|
| 39 |
+
return False
|
| 40 |
+
|
| 41 |
+
# Build the command
|
| 42 |
+
cmd = [sys.executable, script_path, "--repo-name", args.repo_name, "--create-inference"]
|
| 43 |
+
|
| 44 |
+
if args.token:
|
| 45 |
+
cmd.extend(["--token", args.token])
|
| 46 |
+
|
| 47 |
+
if args.private:
|
| 48 |
+
cmd.append("--private")
|
| 49 |
+
|
| 50 |
+
# Execute the command
|
| 51 |
+
print(f"Deploying model to {args.repo_name}...")
|
| 52 |
+
try:
|
| 53 |
+
subprocess.run(cmd, check=True)
|
| 54 |
+
print("\nDeployment successful!")
|
| 55 |
+
print(f"You can access your model at: https://huggingface.co/{args.repo_name}")
|
| 56 |
+
print("\nTo use the model for inference:")
|
| 57 |
+
print(f"1. Visit https://huggingface.co/{args.repo_name}")
|
| 58 |
+
print("2. Use the inference widget on the model page")
|
| 59 |
+
print("3. Or use the API with the following code:")
|
| 60 |
+
print(f"""
|
| 61 |
+
import requests
|
| 62 |
+
|
| 63 |
+
API_URL = "https://api-inference.huggingface.co/models/{args.repo_name}"
|
| 64 |
+
headers = {{"Authorization": "Bearer YOUR_API_TOKEN"}}
|
| 65 |
+
|
| 66 |
+
def query(payload):
|
| 67 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 68 |
+
return response.json()
|
| 69 |
+
|
| 70 |
+
output = query({{
|
| 71 |
+
"inputs": "I'm looking for headphones"
|
| 72 |
+
}})
|
| 73 |
+
print(output)
|
| 74 |
+
""")
|
| 75 |
+
return True
|
| 76 |
+
except subprocess.CalledProcessError as e:
|
| 77 |
+
print(f"Error deploying model: {e}")
|
| 78 |
+
return False
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
if check_requirements():
|
| 82 |
+
deploy_model()
|