| """ | |
| Gradio/Space entrypoint: predict backlink quality and risk. Returns formatted string. | |
| """ | |
| from config import MODEL_DIR, FEATURE_COLUMNS, TARGET_QUALITY, TARGET_RISK | |
| def predict(domain_authority: float, dofollow: int, ref_domains: int, anchor_length: int, same_topic: int) -> str: | |
| try: | |
| import joblib | |
| import pandas as pd | |
| except ImportError as e: | |
| return f"Error: {e}" | |
| row = { | |
| "domain_authority": float(domain_authority or 0), | |
| "dofollow": int(dofollow if dofollow is not None else 1), | |
| "ref_domains": int(ref_domains or 0), | |
| "anchor_length": int(anchor_length or 0), | |
| "same_topic": int(same_topic if same_topic is not None else 0), | |
| } | |
| df = pd.DataFrame([row]) | |
| features = joblib.load(MODEL_DIR / "feature_columns.joblib") if (MODEL_DIR / "feature_columns.joblib").exists() else [c for c in FEATURE_COLUMNS if c in df.columns] | |
| if not features: | |
| return "Model not found." | |
| X = df[features].fillna(0) | |
| out = [] | |
| if (MODEL_DIR / "quality_model.joblib").exists(): | |
| model = joblib.load(MODEL_DIR / "quality_model.joblib") | |
| out.append(f"**pred_{TARGET_QUALITY}:** {model.predict(X)[0]:.3f}") | |
| if (MODEL_DIR / "risk_model.joblib").exists(): | |
| model = joblib.load(MODEL_DIR / "risk_model.joblib") | |
| out.append(f"**pred_risk_label:** {int(model.predict(X)[0])} (0=ok, 1=risk)") | |
| return "\n".join(out) if out else "Model not found. Run train.py and re-upload." | |