| from huggingface_hub.utils import RepositoryNotFoundError | |
| from huggingface_hub import HfApi, create_repo | |
| import os | |
| model_repo_id = "SharleyK/TourismPackagePrediction-GradientBoosting" # Corrected repository ID for the model | |
| model_repo_type = "model" | |
| # Initialize API client | |
| api = HfApi(token=os.getenv("HF_TOKEN")) | |
| # Step 1: Check if the model space exists, create if not | |
| try: | |
| api.repo_info(repo_id=model_repo_id, repo_type=model_repo_type) | |
| print(f"Model repository '{model_repo_id}' already exists. Using it.") | |
| except RepositoryNotFoundError: | |
| print(f"Model repository '{model_repo_id}' not found. Creating new repository...") | |
| create_repo(repo_id=model_repo_id, repo_type=model_repo_type, private=False) | |
| print(f"Model repository '{model_repo_id}' created.") | |
| # Step 2: Upload the model file and README.md | |
| # Assuming model and README.md are in the same folder | |
| upload_folder_path = "tourism_project/data/model_building" | |
| api.upload_folder( | |
| folder_path=upload_folder_path, | |
| repo_id=model_repo_id, | |
| repo_type=model_repo_type, | |
| commit_message="Upload Gradient Boosting model and README.md" | |
| ) | |
| print("Model and README.md uploaded to Hugging Face Model Hub.") | |