File size: 1,168 Bytes
3873e40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from __future__ import annotations

import json
import os

import requests

MODEL_REPO = "arun-gharami/lead-ai-customer-predictor"
HF_TOKEN = os.environ.get("HF_TOKEN", "")


def call_hf_inference_api(payload: dict) -> dict:
    api_url = f"https://api-inference.huggingface.co/models/{MODEL_REPO}"
    headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
    response = requests.post(api_url, headers=headers, json={"inputs": payload}, timeout=30)
    response.raise_for_status()
    return response.json()


if __name__ == "__main__":
    sample_payload = {
        "customer_tenure": 11,
        "total_spent": 900,
        "last_purchase_days": 17,
        "visit_count": 26,
        "email_open_rate": 0.62,
        "discount_usage": 0.22,
        "support_tickets": 1,
        "satisfaction_score": 8.4,
    }

    print("Payload:")
    print(json.dumps(sample_payload, indent=2))
    print("\nResponse:")
    try:
        print(json.dumps(call_hf_inference_api(sample_payload), indent=2))
    except Exception as exc:
        print(f"API call failed: {exc}")
        print("Run local scoring by importing predict_customer_status from app.py.")