Views
No views yet
pip install transformers torch huggingface-hub1from src.models.hf_pipeline import LBMPipeline
2
3# Load model from Hugging Face Hub
4pipeline = LBMPipeline(model="souvik16011991roy/LBM-ecom")
5
6# Prepare inputs
7inputs = {
8 'product_ids': ['1000978', '1001588', '1001606'],
9 'event_types': ['view', 'view', 'cart'],
10 'category_ids': ['cat1', 'cat1', 'cat2'],
11 'hours': [10, 14, 18],
12 'days': [0, 0, 1], # 0=Monday
13 'prices': [29.99, 49.99, 79.99],
14 'segment_id': 0
15}
16
17# Make predictions
18results = pipeline(inputs)
19print(results)Input Embeddings:
├── Product Embedding (164,577 × 256)
├── Event Embedding (3 × 16)
├── Category Embedding (624 × 32)
├── Segment Embedding (8 × 32)
├── Hour Embedding (24 × 16)
├── Day Embedding (7 × 16)
└── Price Projection (1 × 16)
Transformer Encoder:
├── 4 Transformer Layers
│ ├── Multi-head Attention (8 heads)
│ └── Feed-forward Network (2048 dim)
└── Layer Normalization
Output Heads:
├── Action Head (512 × 3)
├── Product Head (512 × 164,577)
└── Timing Head (512 × 1)https://api-inference.huggingface.co/models/souvik16011991roy/LBM-ecomAuthorization: Bearer YOUR_HF_TOKEN1POST https://api-inference.huggingface.co/models/souvik16011991roy/LBM-ecom
2Content-Type: application/json
3Authorization: Bearer YOUR_HF_TOKEN
4
5{
6 "inputs": {
7 "product_ids": ["1000978", "1001588", "1001606"],
8 "event_types": ["view", "view", "cart"],
9 "category_ids": ["cat1", "cat1", "cat2"],
10 "hours": [10, 14, 18],
11 "days": [0, 0, 1],
12 "prices": [29.99, 49.99, 79.99],
13 "segment_id": 0,
14 "task": "all"
15 }
16}1{
2 "next_action": {
3 "predicted": "purchase",
4 "probabilities": {
5 "view": 0.15,
6 "cart": 0.25,
7 "purchase": 0.60
8 }
9 },
10 "next_products": [
11 {
12 "product_id": "1002042",
13 "score": 0.234
14 },
15 {
16 "product_id": "1002062",
17 "score": 0.189
18 },
19 {
20 "product_id": "1002098",
21 "score": 0.156
22 },
23 {
24 "product_id": "1002099",
25 "score": 0.134
26 },
27 {
28 "product_id": "1002100",
29 "score": 0.112
30 },
31 {
32 "product_id": "1002101",
33 "score": 0.098
34 },
35 {
36 "product_id": "1002102",
37 "score": 0.087
38 },
39 {
40 "product_id": "1002103",
41 "score": 0.076
42 },
43 {
44 "product_id": "1002225",
45 "score": 0.065
46 },
47 {
48 "product_id": "1002266",
49 "score": 0.049
50 }
51 ],
52 "next_purchase_days": 3.5
53}1POST https://api-inference.huggingface.co/models/souvik16011991roy/LBM-ecom
2Content-Type: application/json
3Authorization: Bearer YOUR_HF_TOKEN
4
5{
6 "inputs": {
7 "product_ids": ["1000978", "1001588"],
8 "event_types": ["view", "cart"],
9 "task": "action"
10 }
11}1{
2 "next_action": {
3 "predicted": "purchase",
4 "probabilities": {
5 "view": 0.20,
6 "cart": 0.30,
7 "purchase": 0.50
8 }
9 }
10}1POST https://api-inference.huggingface.co/models/souvik16011991roy/LBM-ecom
2Content-Type: application/json
3Authorization: Bearer YOUR_HF_TOKEN
4
5{
6 "inputs": {
7 "product_ids": ["1000978", "1001588", "1001606"],
8 "event_types": ["view", "view", "cart"],
9 "category_ids": ["cat1", "cat1", "cat2"],
10 "top_k": 5,
11 "task": "product"
12 }
13}1{
2 "next_products": [
3 {
4 "product_id": "1002042",
5 "score": 0.234
6 },
7 {
8 "product_id": "1002062",
9 "score": 0.189
10 },
11 {
12 "product_id": "1002098",
13 "score": 0.156
14 },
15 {
16 "product_id": "1002099",
17 "score": 0.134
18 },
19 {
20 "product_id": "1002100",
21 "score": 0.112
22 }
23 ]
24}1POST https://api-inference.huggingface.co/models/souvik16011991roy/LBM-ecom
2Content-Type: application/json
3Authorization: Bearer YOUR_HF_TOKEN
4
5{
6 "inputs": {
7 "product_ids": ["1000978", "1001588", "1001606"],
8 "event_types": ["view", "cart", "purchase"],
9 "task": "timing"
10 }
11}1{
2 "next_purchase_days": 7.2
3}1POST https://api-inference.huggingface.co/models/souvik16011991roy/LBM-ecom
2Content-Type: application/json
3Authorization: Bearer YOUR_HF_TOKEN
4
5{
6 "inputs": [
7 {
8 "product_ids": ["1000978", "1001588"],
9 "event_types": ["view", "cart"],
10 "task": "all"
11 },
12 {
13 "product_ids": ["1001606", "1002042"],
14 "event_types": ["view", "view"],
15 "task": "all"
16 }
17 ]
18}1{
2 "results": [
3 {
4 "next_action": {
5 "predicted": "purchase",
6 "probabilities": {
7 "view": 0.20,
8 "cart": 0.30,
9 "purchase": 0.50
10 }
11 },
12 "next_products": [
13 {"product_id": "1002042", "score": 0.234},
14 {"product_id": "1002062", "score": 0.189}
15 ],
16 "next_purchase_days": 3.5
17 },
18 {
19 "next_action": {
20 "predicted": "view",
21 "probabilities": {
22 "view": 0.65,
23 "cart": 0.25,
24 "purchase": 0.10
25 }
26 },
27 "next_products": [
28 {"product_id": "1002098", "score": 0.198},
29 {"product_id": "1002099", "score": 0.167}
30 ],
31 "next_purchase_days": 12.3
32 }
33 ]
34}1{
2 "error": "product_ids and event_types are required",
3 "status_code": 400
4}1{
2 "error": "product_ids and event_types must have the same length",
3 "status_code": 400
4}1{
2 "error": "Model inference failed: ...",
3 "status_code": 500
4}1from src.models.hf_pipeline import LBMPipeline
2
3# Initialize pipeline
4pipeline = LBMPipeline(model="souvik16011991roy/LBM-ecom")
5
6# Example 1: Basic prediction
7inputs = {
8 'product_ids': ['1000978', '1001588'],
9 'event_types': ['view', 'cart'],
10 'segment_id': 0
11}
12
13results = pipeline(inputs)
14print(f"Next action: {results['next_action']['predicted']}")
15print(f"Top product: {results['next_products'][0]['product_id']}")
16print(f"Days until purchase: {results['next_purchase_days']}")
17
18# Example 2: Predict next action only
19next_action = pipeline.predict_next_action(
20 product_ids=['1000978', '1001588'],
21 event_types=['view', 'cart']
22)
23print(next_action)
24
25# Example 3: Get top 10 product recommendations
26top_products = pipeline.predict_next_product(
27 product_ids=['1000978', '1001588', '1001606'],
28 event_types=['view', 'view', 'cart'],
29 top_k=10
30)
31for product in top_products:
32 print(f"Product {product['product_id']}: {product['score']:.4f}")
33
34# Example 4: Predict purchase timing
35days_until = pipeline.predict_next_purchase(
36 product_ids=['1000978', '1001588'],
37 event_types=['view', 'cart']
38)
39print(f"Days until next purchase: {days_until:.2f}")1import requests
2
3API_URL = "https://api-inference.huggingface.co/models/souvik16011991roy/LBM-ecom"
4headers = {"Authorization": f"Bearer YOUR_HF_TOKEN"}
5
6def query(payload):
7 response = requests.post(API_URL, headers=headers, json=payload)
8 return response.json()
9
10# Predict all
11output = query({
12 "inputs": {
13 "product_ids": ["1000978", "1001588", "1001606"],
14 "event_types": ["view", "view", "cart"],
15 "category_ids": ["cat1", "cat1", "cat2"],
16 "hours": [10, 14, 18],
17 "days": [0, 0, 1],
18 "prices": [29.99, 49.99, 79.99],
19 "segment_id": 0,
20 "task": "all"
21 }
22})
23
24print(output)1const fetch = require('node-fetch');
2
3const API_URL = 'https://api-inference.huggingface.co/models/souvik16011991roy/LBM-ecom';
4const headers = {
5 'Authorization': 'Bearer YOUR_HF_TOKEN',
6 'Content-Type': 'application/json'
7};
8
9async function predict(inputs) {
10 const response = await fetch(API_URL, {
11 method: 'POST',
12 headers: headers,
13 body: JSON.stringify({ inputs })
14 });
15
16 return await response.json();
17}
18
19// Usage
20predict({
21 product_ids: ['1000978', '1001588'],
22 event_types: ['view', 'cart'],
23 task: 'all'
24}).then(result => {
25 console.log(result);
26});1curl -X POST https://api-inference.huggingface.co/models/souvik16011991roy/LBM-ecom \
2 -H "Authorization: Bearer YOUR_HF_TOKEN" \
3 -H "Content-Type: application/json" \
4 -d '{
5 "inputs": {
6 "product_ids": ["1000978", "1001588"],
7 "event_types": ["view", "cart"],
8 "task": "all"
9 }
10 }'| Field | Type | Required | Description | Example |
|---|---|---|---|---|
product_ids | List[str/int] | ✅ Yes | List of product IDs in sequence | ["1000978", "1001588"] |
event_types | List[str] | ✅ Yes | List of event types (view/cart/purchase) | ["view", "cart"] |
category_ids | List[str/int] | ❌ No | List of category IDs | ["cat1", "cat2"] |
hours | List[int] | ❌ No | Hour of day (0-23) | [10, 14, 18] |
days | List[int] | ❌ No | Day of week (0=Monday, 6=Sunday) | [0, 0, 1] |
prices | List[float] | ❌ No | Product prices | [29.99, 49.99] |
segment_id | int | ❌ No | User segment ID (0-7) | 0 |
task | str | ❌ No | Task type: "all", "action", "product", "timing" | "all" |
top_k | int | ❌ No | Number of top products to return (default: 10) | 10 |
product_ids and event_types must have the same length1{
2 "next_action": {
3 "predicted": "purchase",
4 "probabilities": {
5 "view": 0.15,
6 "cart": 0.25,
7 "purchase": 0.60
8 }
9 },
10 "next_products": [
11 {
12 "product_id": "1002042",
13 "score": 0.234
14 },
15 ...
16 ],
17 "next_purchase_days": 3.5
18}1{
2 "next_action": {
3 "predicted": "purchase",
4 "probabilities": {
5 "view": 0.20,
6 "cart": 0.30,
7 "purchase": 0.50
8 }
9 }
10}1{
2 "next_products": [
3 {
4 "product_id": "1002042",
5 "score": 0.234
6 },
7 ...
8 ]
9}1{
2 "next_purchase_days": 7.2
3}1from src.models.hf_lbm import LBMModel
2from src.models.hf_tokenizer import LBMTokenizer
3import torch
4
5# Load model and tokenizer
6model = LBMModel.from_pretrained("souvik16011991roy/LBM-ecom")
7tokenizer = LBMTokenizer.from_pretrained("souvik16011991roy/LBM-ecom")
8
9# Encode inputs
10encoded = tokenizer.encode_sequence(
11 product_ids=['1000978', '1001588'],
12 event_types=['view', 'cart'],
13 return_tensors='pt'
14)
15
16# Forward pass
17model.eval()
18with torch.no_grad():
19 outputs = model(
20 product_ids=encoded['product_ids'],
21 event_types=encoded['event_types'],
22 category_ids=encoded['category_ids'],
23 segment_ids=encoded['segment_id'],
24 hours=encoded['hours'],
25 days=encoded['days'],
26 prices=encoded['prices'],
27 attention_mask=encoded['attention_mask']
28 )
29
30# Process outputs
31import torch.nn.functional as F
32action_probs = F.softmax(outputs.action_logits[:, -1, :], dim=-1)
33product_probs = F.softmax(outputs.product_logits[:, -1, :], dim=-1)
34timing = outputs.timing_pred[:, -1, 0]1from src.models.hf_lbm import LBMConfig, LBMModel
2
3# Create custom config
4config = LBMConfig(
5 product_vocab_size=200000,
6 hidden_dim=1024,
7 num_layers=6,
8 num_heads=16
9)
10
11# Initialize model
12model = LBMModel(config)1@misc{lbm-ecom-model,
2 title={Large Behavioral Model for E-commerce},
3 author={Souvik Roy},
4 year={2024},
5 howpublished={\url{https://huggingface.co/souvik16011991roy/LBM-ecom}}
6}