Spaces:
Sleeping
Sleeping
push: used only the logistic
Browse filesFiles changed (2)
4 unmodified lines56789101111 unmodified lines2324252627282951 unmodified lines81828384858687884 unmodified linesfrom .model_loader import get_default_top_models, load_artifactsTOP_K_MODELS = 2def normalize_nepali_text(text: str) -> str:11 unmodified linesreturn list(models.keys())[:top_k]def classify_text(text: str, model_names=None, top_k: int = 2):artifacts = load_artifacts()models = artifacts["models"]if not models:51 unmodified linesreturn {"label": final_label,"confidence": round(avg_conf * 100, 2),# "selected_models": selected_names,# "model_predictions": per_model,# "votes": {"AI": ai_votes, "Human": human_votes},# "available_models": list(models.keys()),# "unavailable_models": artifacts["unavailable_models"],4 unmodified lines56789101111 unmodified lines2324252627282951 unmodified lines81828384858687884 unmodified linesfrom .model_loader import get_default_top_models, load_artifactsTOP_K_MODELS = 1def normalize_nepali_text(text: str) -> str:11 unmodified linesreturn list(models.keys())[:top_k]def classify_text(text: str, model_names="Logistic Regression", top_k: int = 1):artifacts = load_artifacts()models = artifacts["models"]if not models:51 unmodified linesreturn {"label": final_label,"confidence": round(avg_conf * 100, 2),"selected_models": selected_names,"model_predictions": per_model,# "votes": {"AI": ai_votes, "Human": human_votes},# "available_models": list(models.keys()),# "unavailable_models": artifacts["unavailable_models"],
11 unmodified lines121314151617181920212223242522 unmodified lines4849505152535426 unmodified lines818283848586878889909192939440 unmodified lines1351361371381391401412 unmodified lines14414514614714814915015115215311 unmodified lines16516616716816917017111 unmodified linesfrom config import ConfigLOGGER = logging.getLogger(__name__)MODEL_FILES = {"Logistic Regression": "Logistic_Regression.pkl","Random Forest": "Random_Forest.pkl","Gradient Boosting": "Gradient_Boosting.pkl","Linear SVC": "Linear_SVC.pkl","Ridge Classifier": "Ridge_Classifier.pkl","Multinomial NB": "Multinomial_NB.pkl",22 unmodified linesdef _patch_legacy_logistic_model(model):"""Backfill attributes expected by newer sklearn versions."""if isinstance(model, (LogisticRegression, LogisticRegressionCV)) and not hasattr(model, "multi_class"):model.multi_class = "auto"return model26 unmodified lineswords = str(text).split()num_words = max(len(words), 1)num_chars = max(len(str(text)), 1)num_sentences = max(len([s for s in re.split(r"[।!?]", str(text)) if s.strip()]), 1)avg_word_len = float(np.mean([len(w) for w in words])) if words else 0.0avg_sent_len = num_words / num_sentenceslexical_diversity = len(set(words)) / num_wordspunct_count = str(text).count("।") + str(text).count("?") + str(text).count("!") + str(text).count(",")punct_ratio = punct_count / num_charsbigrams = [" ".join(words[i : i + 2]) for i in range(len(words) - 1)]rep_bigram_ratio = (1.0 - len(set(bigrams)) / max(len(bigrams), 1)) if bigrams else 0.0diacritic_count = sum(1 for c in str(text) if "\u093e" <= c <= "\u094d")diacritic_ratio = diacritic_count / num_charsreturn {40 unmodified linesdefault_dir = repo / "features" / "Model" / "Nepali_model"candidates.extend([default_dir, default_dir / NEPALI_SUBDIR])candidates.append(repo / "notebook" / "ai_vs_human_nepali" / "final_model" / "saved_models")return candidates2 unmodified linesraise ValueError("English_model repo id is not configured")repo = _repo_root()target_dir = Path(Config.Nepali_model_folder) if Config.Nepali_model_folder else repo / "features" / "Model" / "Nepali_model"snapshot_path = Path(snapshot_download(repo_id=REPO_ID, token=HF_TOKEN))source_dir = snapshot_path / NEPALI_SUBDIR if (snapshot_path / NEPALI_SUBDIR).is_dir() else snapshot_pathtarget_dir.mkdir(parents=True, exist_ok=True)shutil.copytree(source_dir, target_dir, dirs_exist_ok=True)11 unmodified linesif _has_required_artifacts(path):return pathraise FileNotFoundError("Nepali model directory not found. Set Nepali_model env or add expected artifacts.")@lru_cache(maxsize=1)11 unmodified lines1213141516171819202122232422 unmodified lines47484950515253545526 unmodified lines82838485868788899091929394959697989910010110210310440 unmodified lines1451461471481491501511521532 unmodified lines15615715815916016116216316416516616716816917017117217311 unmodified lines18518618718818919019119219311 unmodified linesfrom config import ConfigLOGGER = logging.getLogger(__name__)MODEL_FILES = {"Logistic Regression": "Logistic_Regression.pkl","Random Forest": "Random_Forest.pkl",# "Gradient Boosting": "Gradient_Boosting.pkl","Linear SVC": "Linear_SVC.pkl","Ridge Classifier": "Ridge_Classifier.pkl","Multinomial NB": "Multinomial_NB.pkl",22 unmodified linesdef _patch_legacy_logistic_model(model):"""Backfill attributes expected by newer sklearn versions."""if isinstance(model, (LogisticRegression, LogisticRegressionCV)) and not hasattr(model, "multi_class"):model.multi_class = "auto"return model26 unmodified lineswords = str(text).split()num_words = max(len(words), 1)num_chars = max(len(str(text)), 1)num_sentences = max(len([s for s in re.split(r"[।!?]", str(text)) if s.strip()]), 1)avg_word_len = float(np.mean([len(w) for w in words])) if words else 0.0avg_sent_len = num_words / num_sentenceslexical_diversity = len(set(words)) / num_wordspunct_count = (str(text).count("।")+ str(text).count("?")+ str(text).count("!")+ str(text).count(","))punct_ratio = punct_count / num_charsbigrams = [" ".join(words[i : i + 2]) for i in range(len(words) - 1)]rep_bigram_ratio = ((1.0 - len(set(bigrams)) / max(len(bigrams), 1)) if bigrams else 0.0)diacritic_count = sum(1 for c in str(text) if "\u093e" <= c <= "\u094d")diacritic_ratio = diacritic_count / num_charsreturn {40 unmodified linesdefault_dir = repo / "features" / "Model" / "Nepali_model"candidates.extend([default_dir, default_dir / NEPALI_SUBDIR])candidates.append(repo / "notebook" / "ai_vs_human_nepali" / "final_model" / "saved_models")return candidates2 unmodified linesraise ValueError("English_model repo id is not configured")repo = _repo_root()target_dir = (Path(Config.Nepali_model_folder)if Config.Nepali_model_folderelse repo / "features" / "Model" / "Nepali_model")snapshot_path = Path(snapshot_download(repo_id=REPO_ID, token=HF_TOKEN))source_dir = (snapshot_path / NEPALI_SUBDIRif (snapshot_path / NEPALI_SUBDIR).is_dir()else snapshot_path)target_dir.mkdir(parents=True, exist_ok=True)shutil.copytree(source_dir, target_dir, dirs_exist_ok=True)11 unmodified linesif _has_required_artifacts(path):return pathraise FileNotFoundError("Nepali model directory not found. Set Nepali_model env or add expected artifacts.")@lru_cache(maxsize=1)
features/nepali_text_classifier/inferencer.py+4-4
features/nepali_text_classifier/model_loader.py+32-10