Spaces:
Sleeping
Sleeping
Download reviewguard/deploy.py from Emirinth/ReviewGuard: direct link, hf CLI and curl.
- Browser
- Download file 4.54 kB
-
https://huggingface.co/spaces/Emirinth/ReviewGuard/resolve/main/reviewguard/deploy.py
- Command line
-
hf download hf://spaces/Emirinth/ReviewGuard/reviewguard/deploy.py
-
curl -L -o deploy.py https://huggingface.co/spaces/Emirinth/ReviewGuard/resolve/main/reviewguard/deploy.py
4.54 kB
| """Publish ReviewGuard to Hugging Face: the app as a Space and the trained models as a model repo. | |
| The Space runs the app in the cloud, and the model repo lets anyone skip retraining. | |
| Uploads use the token saved by `hf auth login`. | |
| """ | |
| import shutil | |
| import tempfile | |
| from pathlib import Path | |
| from huggingface_hub import HfApi, snapshot_download | |
| from reviewguard import config | |
| SPACE_ID = "Emirinth/ReviewGuard" | |
| MODEL_REPO_ID = "Emirinth/ReviewGuard-models" | |
| # Training leftovers that are not needed to run the models. | |
| SKIPPED_MODEL_FILES = ["*training_args.bin", "roberta_checkpoints/*"] | |
| # Spaces read their settings from the front matter of README.md. | |
| SPACE_README = """--- | |
| title: ReviewGuard | |
| emoji: 🛡️ | |
| colorFrom: indigo | |
| colorTo: red | |
| sdk: gradio | |
| sdk_version: {gradio_version} | |
| python_version: "3.12" | |
| app_file: app.py | |
| pinned: false | |
| short_description: AI triage for retail reviews and customer photos | |
| --- | |
| # ReviewGuard | |
| AI triage for retail reviews: sentiment, issue type and photo checks combined into one decision. | |
| """ | |
| # The CPU build of PyTorch is far smaller than the default GPU build. | |
| SPACE_REQUIREMENTS = """--extra-index-url https://download.pytorch.org/whl/cpu | |
| torch | |
| transformers | |
| accelerate | |
| captum | |
| sentence-transformers | |
| textblob | |
| vaderSentiment | |
| joblib | |
| pandas | |
| pyarrow | |
| scikit-learn | |
| matplotlib | |
| plotly | |
| pillow | |
| """ | |
| ARTIFACT_FOLDERS = ["reports", "examples", "labels"] | |
| def stage_space(folder: Path) -> None: | |
| """Copy everything the Space needs into one folder.""" | |
| import gradio | |
| shutil.copy(config.CODE_DIR / "app.py", folder / "app.py") | |
| shutil.copytree( | |
| config.CODE_DIR / "reviewguard", | |
| folder / "reviewguard", | |
| ignore=shutil.ignore_patterns("__pycache__"), | |
| ) | |
| (folder / "README.md").write_text( | |
| SPACE_README.format(gradio_version=gradio.__version__), encoding="utf-8" | |
| ) | |
| (folder / "requirements.txt").write_text(SPACE_REQUIREMENTS, encoding="utf-8") | |
| artifacts = folder / "artifacts" | |
| for name in ARTIFACT_FOLDERS: | |
| shutil.copytree(config.ARTIFACTS_DIR / name, artifacts / name) | |
| shutil.copytree( | |
| config.MODELS_DIR, | |
| artifacts / "models", | |
| ignore=shutil.ignore_patterns("training_args.bin", "roberta_checkpoints"), | |
| ) | |
| def deploy() -> str: | |
| """Create the Space if needed, upload the staged folder and return the Space URL.""" | |
| api = HfApi() | |
| api.create_repo(SPACE_ID, repo_type="space", space_sdk="gradio", exist_ok=True) | |
| with tempfile.TemporaryDirectory() as temp: | |
| stage_space(Path(temp)) | |
| api.upload_folder( | |
| folder_path=temp, | |
| repo_id=SPACE_ID, | |
| repo_type="space", | |
| commit_message="Deploy ReviewGuard", | |
| delete_patterns=["*"], | |
| ) | |
| return f"https://huggingface.co/spaces/{SPACE_ID}" | |
| # ---------------------------------------------------------------- trained models | |
| MODEL_CARD = """--- | |
| license: mit | |
| tags: [sentiment-analysis, retail, reviews, text-classification] | |
| --- | |
| # ReviewGuard trained models | |
| Models behind the ReviewGuard review-triage app. | |
| - `roberta_sentiment/`: RoBERTa fine-tuned on 72,000 Amazon Reviews 2023 reviews. | |
| It predicts negative, neutral or positive. | |
| - `distilroberta_sentiment/`: a smaller, faster version trained the same way. | |
| - `classical/`: TF-IDF with Naive Bayes and Logistic Regression baselines. | |
| - `issue_student.joblib`: issue classifier distilled from a zero-shot teacher. | |
| - `photo_calibration.json` and `lexicon_bands.json`: calibration and tuning settings. | |
| Download them into the project with `python pipeline.py fetch-models`. | |
| """ | |
| def publish_models() -> str: | |
| """Upload the trained models and a short model card, then return the repo URL.""" | |
| api = HfApi() | |
| api.create_repo(MODEL_REPO_ID, repo_type="model", exist_ok=True) | |
| api.upload_file( | |
| path_or_fileobj=MODEL_CARD.encode("utf-8"), | |
| path_in_repo="README.md", | |
| repo_id=MODEL_REPO_ID, | |
| repo_type="model", | |
| ) | |
| api.upload_folder( | |
| folder_path=str(config.MODELS_DIR), | |
| repo_id=MODEL_REPO_ID, | |
| repo_type="model", | |
| ignore_patterns=SKIPPED_MODEL_FILES, | |
| commit_message="Upload trained ReviewGuard models", | |
| ) | |
| return f"https://huggingface.co/{MODEL_REPO_ID}" | |
| def fetch_models() -> str: | |
| """Download the published models into the local models folder. | |
| Returns the folder they were saved to. | |
| """ | |
| config.MODELS_DIR.mkdir(parents=True, exist_ok=True) | |
| return snapshot_download(MODEL_REPO_ID, repo_type="model", local_dir=str(config.MODELS_DIR)) | |