File size: 2,954 Bytes
4398e81
b9aea23
 
 
 
 
 
 
 
 
3200fc7
4398e81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
---
title: ACCESS EMBEDDING
emoji: 🐨
colorFrom: green
colorTo: red
sdk: docker
pinned: false
---

Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
---
title: ACCESS SBERT Embedding Service
colorFrom: blue
colorTo: indigo
sdk: docker
app_port: 7860
pinned: false
---

# ACCESS SBERT Embedding Service

FastAPI service for generating SBERT embeddings for ACCESS semantic context search.

The service only converts text into normalized sentence embeddings. It does not store ACCESS data, query PostgreSQL, generate HEAT suggestions, or perform business workflow logic.

## Model

Default model:

```txt
sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2
```

Expected embedding dimension:

```txt
384
```

## Endpoints

- `GET /health`
- `POST /embed`
- `POST /embed/batch`

## Environment Variables

```env
MODEL_NAME=sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2
EMBEDDING_API_KEY=change-this-secret
MAX_TEXT_LENGTH=3000
MAX_BATCH_SIZE=64
```

`EMBEDDING_API_KEY` is optional for local development. Configure it for deployed Spaces.

## Local Run

```bash
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
uvicorn app.main:app --host 0.0.0.0 --port 7860 --reload
```

## Health Check

```bash
curl http://localhost:7860/health
```

Expected response:

```json
{
  "status": "ok",
  "model": "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2",
  "dimension": 384
}
```

## Single Embedding

```bash
curl -X POST http://localhost:7860/embed \
  -H "Content-Type: application/json" \
  -H "X-API-Key: change-this-secret" \
  -d "{\"text\":\"Saldo saya terpotong tapi tiket tidak muncul.\"}"
```

## Batch Embedding

```bash
curl -X POST http://localhost:7860/embed/batch \
  -H "Content-Type: application/json" \
  -H "X-API-Key: change-this-secret" \
  -d "{\"texts\":[\"Saldo terpotong tapi tiket belum muncul.\",\"Refund pembatalan tiket belum diterima.\"]}"
```

## Tests

```bash
pytest
```

The first embedding test run downloads and loads the SBERT model, so it can be slow.

## Docker

```bash
docker build -t access-sbert-embedding-service .
docker run --rm -p 7860:7860 -e EMBEDDING_API_KEY=change-this-secret access-sbert-embedding-service
```

## ACCESS Backend Integration

ACCESS Backend should call this service over HTTP, then store/search embeddings in PostgreSQL + pgvector.

Backend env shape:

```env
EMBEDDING_ENABLED=true
EMBEDDING_SERVICE_URL=https://<space-url>/embed
EMBEDDING_BATCH_SERVICE_URL=https://<space-url>/embed/batch
EMBEDDING_API_KEY=<same-secret>
EMBEDDING_MODEL=sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2
EMBEDDING_DIMENSION=384
EMBEDDING_TIMEOUT_MS=10000
```

Failure rules:

- If embedding fails during preview, ACCESS Backend should keep HEAT suggestions working.
- If embedding fails during reference/case indexing, backend should keep source data saved and backfill embeddings later.