Files
encoach_be_odoo19/scripts/import_training_tips.py
Talal Sharabi 32d987c7cf Fix avatar API to return all fields, add TMPDIR workaround to import script
- Use to_encoach_dict() for /api/exam/avatars so voiceId, voiceProvider,
  canvas are included in the response
- Set TMPDIR=/tmp in import script for PyTorch compatibility in Docker

Made-with: Cursor
2026-03-15 01:38:13 +04:00

82 lines
2.4 KiB
Python

"""Import training tips from pathways JSON into encoach.training.tip model.
Run inside the Odoo container via:
odoo shell -d encoach --db_host=db --db_user=odoo --db_password=<pw> \
--no-http --addons-path=... < /mnt/custom/scripts/import_training_tips.py
This script:
1. Reads pathways_2_rw.json
2. Creates encoach.training.tip records (tip_id, category, content)
3. Computes sentence-transformer embeddings and stores them
"""
import json
import os
import pickle
import sys
os.environ.setdefault("TMPDIR", "/tmp")
JSON_PATH = os.environ.get(
"TIPS_JSON_PATH",
"/mnt/custom/scripts/pathways_2_rw.json",
)
def main():
with open(JSON_PATH, "r", encoding="utf-8") as f:
data = json.load(f)
TipModel = env["encoach.training.tip"].sudo()
tips_data = []
for unit in data["units"]:
for page in unit["pages"]:
for tip in page["tips"]:
category = tip["category"].lower().replace(" ", "_")
tips_data.append({
"tip_id": tip["id"],
"category": category,
"content": tip["text"],
})
created = 0
skipped = 0
for td in tips_data:
existing = TipModel.search([("tip_id", "=", td["tip_id"])], limit=1)
if existing:
skipped += 1
continue
TipModel.create(td)
created += 1
env.cr.commit()
print(f"Tips import complete: {created} created, {skipped} skipped (already exist)")
try:
from sentence_transformers import SentenceTransformer
import numpy as np
print("Computing embeddings with all-MiniLM-L6-v2...")
model = SentenceTransformer("all-MiniLM-L6-v2")
tips = TipModel.search([("embedding", "=", False)])
if not tips:
print("All tips already have embeddings.")
return
for i, tip in enumerate(tips):
vec = model.encode([tip.content]).astype(np.float32)[0]
tip.embedding = pickle.dumps(vec)
if (i + 1) % 10 == 0:
print(f" Embedded {i + 1}/{len(tips)} tips...")
env.cr.commit()
print(f"Embeddings computed for {len(tips)} tips.")
except ImportError:
print("WARNING: sentence-transformers not available. Tips created without embeddings.")
print("Embeddings can be computed later by re-running this script.")
main()