← All files for this project · the case study

run_model.py

"""Ask the AI decision model about every review and save its answers to data/ai_answers.csv.You don't have to run this: its answers are already saved. Run it only to see the model work, or on new reviews.The model is Laya (convaiinnovations/laya on Hugging Face, Apache 2.0): an open "System One" decision model. It readsa text and answers typed questions (pick one option, yes or no) with a probability for each option, in one quick pass.It never writes text, so there is nothing to parse and nothing it can make up.Setup once:  pip install laya pandas       (downloads PyTorch too; the model itself is about 800 MB, fetched on first use)Run:         python3 run_model.py           (about 40 minutes on a laptop processor; much faster on an Apple chip)"""import os os.environ.setdefault("USE_TF", "0")        # stop the transformers library from loading TensorFlow (it can freeze the load) import layaimport pandas as pd MOOD = {"type": "choice", "instructions": "Is the reviewer happy with the product?",        "criteria": {"A": "yes, the reviewer is happy with it", "B": "no, the reviewer is unhappy with it"}} COMPLAINT_TYPES = {    "leaks_lid": "it leaks or spills, or the lid, straw, cap or seal is a problem",    "temperature": "it does not keep drinks cold or hot long enough",    "damage": "it arrived damaged, or it dented, chipped, rusted or broke",    "wrong_item": "a wrong or missing part, a used item, or a fake",    "size_design": "the size, weight, shape, handle or color",    "cleaning_taste": "cleaning, smell, taste or mold",    "price": "the price is too high",}COMPLAINTS = {name: {"type": "choice", "instructions": f"Does the reviewer complain that {words}?",                     "criteria": {"A": "yes", "B": "no"}}              for name, words in COMPLAINT_TYPES.items()} reviews = pd.read_csv("data/reviews.csv")reviews = reviews[reviews["language"] == "en"]          # this checkpoint reads English; 59 Spanish reviews are set asideagent = laya.load("convaiinnovations/laya") rows = []for r in reviews.itertuples():    title = r.title if isinstance(r.title, str) else ""    body = r.text if isinstance(r.text, str) else ""    text = (title.rstrip(".") + ". " + body).strip()[:1500]    questions = {"mood": MOOD}    if r.rating <= 2:                                    # the complaint questions only for one- and two-star reviews        questions.update(COMPLAINTS)    answers = agent.predict(text, questions)["answers"]    row = {"review_id": r.review_id, "p_happy": round(answers["mood"]["probabilities"]["A"], 4)}    for name in COMPLAINT_TYPES:        row["c_" + name] = round(answers[name]["probabilities"]["A"], 4) if name in answers else None    rows.append(row)    if len(rows) % 100 == 0:        print(len(rows), "reviews done") pd.DataFrame(rows).to_csv("data/ai_answers.csv", index=False)print("Saved data/ai_answers.csv with", len(rows), "rows")