"""Script 07: Inference demo. Run a few hand-crafted reviews through the Proposed model to show the per-aspect output the customer asked for. Usage: python scripts/07_inference.py """ import json import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) from src.utils import setup_logging from src import config as cfg from src.inference import AspectPredictor # A few representative reviews + matching product metadata DEMO_INPUTS = [ { "review_text": ( "The size runs really small, I ordered an XL but it fits like a Medium. " "That said, the fabric feels super soft and the print looks great." ), "product_meta": { "features_text": "100% Cotton, Slim Fit, Machine Wash Cold", "categories_text": "Clothing > Men > T-Shirts > Graphic Tees", "price": 19.99, "average_rating": 4.2, "rating_number": 312, }, }, { "review_text": ( "Cheap quality, the stitching came apart after one wash. " "Not worth even the discount price." ), "product_meta": { "features_text": "Polyester blend, machine washable", "categories_text": "Clothing > Women > Dresses > Casual", "price": 9.99, "average_rating": 3.1, "rating_number": 87, }, }, { "review_text": ( "Absolutely beautiful dress. The colour matches the photo perfectly " "and it's very flattering on. Got many compliments. Worth every penny." ), "product_meta": { "features_text": "Stretch jersey, A-line silhouette, midi length", "categories_text": "Clothing > Women > Dresses > Formal", "price": 89.00, "average_rating": 4.7, "rating_number": 540, }, }, ] def main(): setup_logging() ckpt = cfg.CHECKPOINT_DIR / "meta_acsa" / "best.pt" if not ckpt.exists(): raise SystemExit(f"Proposed model checkpoint not found at {ckpt}. " "Train it first (scripts/04_train_proposed.py).") predictor = AspectPredictor() for i, item in enumerate(DEMO_INPUTS, 1): print(f"\n--- Demo {i} ---") print(f"Review: {item['review_text']}") print(f"Meta: features={item['product_meta']['features_text'][:60]}... " f"price={item['product_meta']['price']}") out = predictor.predict(item["review_text"], item["product_meta"]) print("Aspect-level prediction:") for a in cfg.ASPECTS: print(f" {a:<10} -> {out['aspects'][a]}") if "meta_attention" in out: top = max(out["meta_attention"], key=out["meta_attention"].get) print(f" (model attended most to {top}: " f"{out['meta_attention'][top]:.3f})") if __name__ == "__main__": main()