File size: 8,439 Bytes
80f351c
 
 
 
 
 
 
 
 
 
a7fd202
6df72b8
093fabc
6df72b8
a7fd202
6df72b8
093fabc
 
 
 
9a9ec03
a7fd202
9a9ec03
a7fd202
 
 
b50a781
 
a7fd202
9a9ec03
a7fd202
9a9ec03
a7fd202
9a9ec03
a7fd202
 
 
 
 
9a9ec03
b50a781
 
 
 
 
 
 
093fabc
9a9ec03
a7fd202
 
 
 
9a9ec03
093fabc
 
 
 
 
 
 
a7fd202
9a9ec03
b50a781
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7fd202
9a9ec03
a7fd202
9a9ec03
b50a781
9a9ec03
a7fd202
 
 
 
b50a781
 
 
a7fd202
9a9ec03
 
a7fd202
9a9ec03
a7fd202
 
b50a781
a7fd202
 
 
 
 
 
 
 
 
 
b50a781
 
 
 
 
 
 
 
 
 
 
a7fd202
 
 
9a9ec03
b525236
 
b50a781
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7fd202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a9ec03
a7fd202
 
 
 
 
 
 
 
 
9a9ec03
a7fd202
9a9ec03
a7fd202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b50a781
a7fd202
 
9a9ec03
a7fd202
 
 
 
 
9a9ec03
a7fd202
 
 
 
 
 
9a9ec03
a7fd202
 
 
 
 
9a9ec03
a7fd202
 
 
 
9a9ec03
a7fd202
9a9ec03
093fabc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7fd202
9a9ec03
a7fd202
 
 
 
 
9a9ec03
a7fd202
9a9ec03
a7fd202
 
 
9a9ec03
a7fd202
 
 
9a9ec03
a7fd202
9a9ec03
a7fd202
 
 
 
9a9ec03
a7fd202
9a9ec03
a7fd202
093fabc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
---
title: Bielik App Service
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: docker
app_port: 7860
pinned: false
---

# Bielik App Service

Multi-model LLM service for description enhancement, batch gap-filling, and A/B testing.

## Overview

This service provides an API for generating enhanced descriptions using multiple open-source LLMs. It supports:
- **Description Enhancement**: Generate marketing descriptions from structured data
- **Batch Infill**: Fill gaps (`[GAP:n]` or `___`) in ad texts with natural words
- **Multi-Model Comparison**: Compare outputs across different models for A/B testing

## Models

| Model | Size | Polish Support | Type |
|-------|------|----------------|------|
| Bielik-1.5B | 1.5B | Excellent | Local |
| Qwen2.5-3B | 3B | Good | Local |
| Gemma-2-2B | 2B | Medium | Local |
| PLLuM-12B | 12B | Excellent | API |

## API Endpoints

### Health & Info

| Method | Endpoint | Description |
|--------|----------|-------------|
| `GET` | `/` | Welcome message |
| `GET` | `/health` | API health check and model status |
| `GET` | `/models` | List all available models |

### Model Management (Lazy Loading)

| Method | Endpoint | Description |
|--------|----------|-------------|
| `POST` | `/models/{name}/load` | Load a model into memory |
| `POST` | `/models/{name}/unload` | Unload a model from memory |

### Description Generation

| Method | Endpoint | Description |
|--------|----------|-------------|
| `POST` | `/enhance-description` | Generate description with single model |
| `POST` | `/compare` | Compare outputs from multiple models |

### Batch Infill (Gap-Filling)

| Method | Endpoint | Description |
|--------|----------|-------------|
| `POST` | `/infill` | Batch gap-filling with single model |
| `POST` | `/compare-infill` | Compare gap-filling across multiple models |

---

## Lazy Loading

Models are **not loaded at startup** to conserve memory. Instead:
- Models are loaded **on first request** (lazy loading)
- Only **one local model** is loaded at a time
- Switching to a different local model **automatically unloads** the previous one
- API models (PLLuM) don't affect local model memory

### Example: Load/Unload Flow
```
1. Request with bielik-1.5b → Loads Bielik (first use)
2. Request with qwen2.5-3b → Unloads Bielik, loads Qwen
3. Request with pllum-12b → Qwen stays loaded (API model doesn't affect local)
4. POST /models/qwen2.5-3b/unload → Manually free memory
```

---

## Endpoint Details

### `GET /health`

Check API status and loaded models.

**Response:**
```json
{
  "status": "ok",
  "available_models": 4,
  "loaded_models": ["bielik-1.5b"],
  "active_local_model": "bielik-1.5b"
}
```

---

### `GET /models`

List all available models with their load status.

**Response:**
```json
[
  {
    "name": "bielik-1.5b",
    "model_id": "speakleash/Bielik-1.5B-v3.0-Instruct",
    "type": "local",
    "polish_support": "excellent",
    "size": "1.5B",
    "loaded": true,
    "active": true
  },
  {
    "name": "qwen2.5-3b",
    "model_id": "Qwen/Qwen2.5-3B-Instruct",
    "type": "local",
    "polish_support": "good",
    "size": "3B",
    "loaded": false,
    "active": false
  }
]
```

---

### `POST /models/{name}/load`

Explicitly load a model. For local models, unloads the previous one first.

**Response:**
```json
{
  "status": "loaded",
  "model": {
    "name": "bielik-1.5b",
    "loaded": true,
    "active": true
  }
}
```

---

### `POST /models/{name}/unload`

Explicitly unload a model to free memory.

**Response:**
```json
{
  "status": "unloaded",
  "model": "bielik-1.5b"
}
```

---

### `POST /enhance-description`

Generate enhanced description using a single model.

**Request:**
```json
{
  "domain": "cars",
  "data": {
    "make": "BMW",
    "model": "320i",
    "year": 2020,
    "mileage": 45000,
    "features": ["nawigacja", "klimatyzacja"],
    "condition": "bardzo dobry"
  },
  "model": "bielik-1.5b"
}
```

**Response:**
```json
{
  "description": "Generated description text...",
  "model_used": "speakleash/Bielik-1.5B-v3.0-Instruct",
  "generation_time": 2.34,
  "user_email": "anonymous"
}
```

---

### `POST /compare`

Compare outputs from multiple models for the same input.

**Request:**
```json
{
  "domain": "cars",
  "data": {
    "make": "BMW",
    "model": "320i",
    "year": 2020,
    "mileage": 45000,
    "features": ["nawigacja", "klimatyzacja"],
    "condition": "bardzo dobry"
  },
  "models": ["bielik-1.5b", "qwen2.5-3b", "gemma-2-2b", "pllum-12b"]
}
```

**Response:**
```json
{
  "domain": "cars",
  "results": [
    {
      "model": "bielik-1.5b",
      "output": "Generated text from Bielik...",
      "time": 2.3,
      "type": "local",
      "error": null
    },
    {
      "model": "pllum-12b",
      "output": "Generated text from PLLuM...",
      "time": 1.1,
      "type": "inference_api",
      "error": null
    }
  ],
  "total_time": 5.67
}
```

---

### `POST /infill`

Batch gap-filling for ads using a single model. Accepts texts with `[GAP:n]` markers or `___` and returns filled text with per-gap choices and alternatives.

**Gap Notation:**
- `[GAP:1]`, `[GAP:2]`, ... → Explicit numbered gaps (preferred)
- `___` → Auto-numbered in scan order

**Request:**
```json
{
  "domain": "cars",
  "items": [
    {
      "id": "ad1",
      "text_with_gaps": "Sprzedam [GAP:1] BMW w [GAP:2] stanie technicznym"
    },
    {
      "id": "ad2", 
      "text_with_gaps": "Auto ma ___ km przebiegu i ___ lakier"
    }
  ],
  "model": "bielik-1.5b",
  "options": {
    "top_n_per_gap": 3,
    "language": "pl",
    "temperature": 0.6
  }
}
```

**Response:**
```json
{
  "model": "bielik-1.5b",
  "results": [
    {
      "id": "ad1",
      "status": "ok",
      "filled_text": "Sprzedam eleganckie BMW w doskonałym stanie technicznym",
      "gaps": [
        {
          "index": 1,
          "marker": "[GAP:1]",
          "choice": "eleganckie",
          "alternatives": ["piękne", "zadbane"]
        },
        {
          "index": 2,
          "marker": "[GAP:2]",
          "choice": "doskonałym",
          "alternatives": ["bardzo dobrym", "idealnym"]
        }
      ],
      "error": null
    }
  ],
  "total_time": 3.45,
  "processed_count": 2,
  "error_count": 0
}
```

**Options:**
| Field | Type | Default | Description |
|-------|------|---------|-------------|
| `gap_notation` | string | `"auto"` | `"auto"`, `"[GAP:n]"`, or `"___"` |
| `top_n_per_gap` | int | `3` | Alternatives per gap (1-5) |
| `language` | string | `"pl"` | Output language |
| `temperature` | float | `0.6` | Generation temperature (0-1) |
| `max_new_tokens` | int | `256` | Max tokens to generate |

---

### `POST /compare-infill`

Multi-model batch gap-filling comparison for A/B testing.

**Request:**
```json
{
  "domain": "cars",
  "items": [
    {
      "id": "ad1",
      "text_with_gaps": "Sprzedam [GAP:1] BMW w [GAP:2] stanie"
    }
  ],
  "models": ["bielik-1.5b", "qwen2.5-3b", "pllum-12b"],
  "options": {
    "top_n_per_gap": 3
  }
}
```

**Response:**
```json
{
  "domain": "cars",
  "models": [
    {
      "model": "bielik-1.5b",
      "type": "local",
      "results": [...],
      "time": 2.1,
      "error_count": 0
    },
    {
      "model": "qwen2.5-3b",
      "type": "local",
      "results": [...],
      "time": 1.8,
      "error_count": 0
    }
  ],
  "total_time": 5.2
}
```

---

## Domains

Currently supported domains:

| Domain | Schema Fields |
|--------|---------------|
| `cars` | `make`, `model`, `year`, `mileage`, `features[]`, `condition` |

---

## Environment Variables

| Variable | Description | Required |
|----------|-------------|----------|
| `HF_TOKEN` | HuggingFace API token for Inference API | Yes (for API models) |
| `LOCAL_MODEL_PATH` | Path to pre-downloaded local model | No (default: `/app/pretrain_model`) |
| `FRONTEND_URL` | Frontend URL for CORS | No |

## Running Locally

```bash
# Install dependencies
pip install -r requirements.txt

# Run server
uvicorn app.main:app --reload --port 8000
```

## Docker

```bash
# Build and run
./start_container.ps1
```

API available at `http://localhost:8000`

Docs at `http://localhost:8000/docs`

## Live Demo

Deployed on HuggingFace Spaces:

**URL:** `https://studzinsky-bielik-app-service.hf.space`

**Quick Test:**
```bash
# Health check
curl https://studzinsky-bielik-app-service.hf.space/health

# List models
curl https://studzinsky-bielik-app-service.hf.space/models
```