Spaces:
Running
Running
Download models/base.py from anikethans06/capstone: direct link, hf CLI and curl.
- Browser
- Download file 2.53 kB
-
https://huggingface.co/spaces/anikethans06/capstone/resolve/main/models/base.py
- Command line
-
hf download hf://spaces/anikethans06/capstone/models/base.py
-
curl -L -o base.py https://huggingface.co/spaces/anikethans06/capstone/resolve/main/models/base.py
2.53 kB
| """ | |
| models/base.py | |
| βββββββββββββββ | |
| Abstract base classes that every model tier (Classical ML, LSTM, CNN-LSTM, | |
| BERT, DistilBERT) must implement. | |
| Defines the unified interface used by the comparative inference system. | |
| """ | |
| from abc import ABC, abstractmethod | |
| from typing import Any | |
| import pathlib | |
| import numpy as np | |
| class BaseTrainer(ABC): | |
| """ | |
| Common contract for all model trainers across all tiers. | |
| Training scripts subclass this; the run_* orchestrators call the interface. | |
| """ | |
| name: str # Human-readable model name, e.g. "Logistic Regression" | |
| tier: str # "classical" | "deep_learning" | "transformer" | |
| def train(self, *args, **kwargs) -> float: | |
| """Train the model. Returns wall-clock training time in seconds.""" | |
| def predict(self, X: Any) -> np.ndarray: | |
| """Return integer class predictions for input X.""" | |
| def predict_proba(self, X: Any) -> np.ndarray: | |
| """Return probability array of shape (N, num_classes).""" | |
| def save(self, directory: pathlib.Path) -> pathlib.Path: | |
| """Serialize model to directory. Returns path written.""" | |
| def load(cls, directory: pathlib.Path) -> "BaseTrainer": | |
| """Restore a previously saved model from directory.""" | |
| class BasePredictor(ABC): | |
| """ | |
| Unified inference interface for the comparative system. | |
| Each tier implements this so the comparison harness can call | |
| predict() / predict_single() identically across all 7 models. | |
| """ | |
| model_name: str # Short identifier, e.g. "svm" | |
| tier: str | |
| def predict(self, texts: list[str]) -> dict: | |
| """ | |
| Run inference on a list of raw review strings. | |
| Returns: | |
| { | |
| "labels": list[int], # 0 = negative, 1 = positive | |
| "label_names": list[str], | |
| "confidences": list[float], # P(positive) | |
| "latency_ms": float, # total batch latency | |
| "ms_per_sample": float, | |
| } | |
| """ | |
| def predict_single(self, text: str) -> dict: | |
| """ | |
| Run inference on a single raw review string. | |
| Returns: | |
| { | |
| "label": int, | |
| "label_name": str, | |
| "confidence": float, # P(positive) | |
| "latency_ms": float, | |
| } | |
| """ | |