#!/usr/bin/env python3
import sys, json
try:
    import torch
    import open_clip
except Exception as e:
    print(json.dumps({"error":"deps_missing","message":str(e)}))
    sys.exit(0)

def main():
    raw = sys.stdin.read()
    data = json.loads(raw) if raw.strip() else {}
    text = data.get('text') or ''
    model, _, preprocess = open_clip.create_model_and_transforms('ViT-B-32', pretrained='laion2b_s34b_b79k')
    tokenizer = open_clip.get_tokenizer('ViT-B-32')
    model.eval()
    with torch.no_grad():
        tokens = tokenizer([text])
        emb = model.encode_text(tokens)
        emb = emb / emb.norm(dim=-1, keepdim=True)
        arr = emb.squeeze(0).cpu().numpy().astype('float32').tolist()
    print(json.dumps({"embedding": arr}))

if __name__ == '__main__':
    main()

