File size: 2,832 Bytes
827158f
4c77892
 
827158f
 
4c77892
 
 
 
 
 
 
 
 
827158f
 
4c77892
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
827158f
4c77892
 
 
 
827158f
4c77892
827158f
4c77892
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
---
base_model: unsloth/Qwen2.5-1.5B-Instruct
library_name: peft
license: apache-2.0
language:
  - en
tags:
  - unsloth
  - lora
  - json
  - extraction
  - structured-output
  - qwen2.5
pipeline_tag: text-generation
---

# json-extract

A fine-tuned **Qwen2.5-1.5B-Instruct** model with LoRA adapters for extracting structured JSON from natural language text.

## What it does

Give it any unstructured text and a target JSON schema — it returns clean, structured JSON output.

**Input:**
```
Paid 500 to Ravi for lunch on Jan 5
```

**Output:**
```json
{
  "amount": 500,
  "person": "Ravi",
  "date": "2025-01-05",
  "note": "lunch"
}
```

## How to use

### With Unsloth (recommended)

```python
from unsloth import FastLanguageModel
import json

model, tokenizer = FastLanguageModel.from_pretrained(
    "suneeldk/json-extract",
    load_in_4bit=True,
)
FastLanguageModel.for_inference(model)

def extract(text, schema):
    prompt = f"### Input: {text}\n### Schema: {json.dumps(schema)}\n### Output:"
    inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
    outputs = model.generate(
        **inputs,
        max_new_tokens=512,
        temperature=0.1,
        do_sample=True,
        pad_token_id=tokenizer.eos_token_id,
        use_cache=False,
    )
    result = tokenizer.decode(outputs[0], skip_special_tokens=True)
    output_part = result.split("### Output:")[-1].strip()
    return json.loads(output_part)

schema = {
    "amount": "number",
    "person": "string|null",
    "date": "ISO date|null",
    "note": "string|null"
}

result = extract("Paid 500 to Ravi for lunch on Jan 5", schema)
print(json.dumps(result, indent=2))
```

### With Transformers + PEFT

```python
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel

base_model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-1.5B-Instruct")
model = PeftModel.from_pretrained(base_model, "YOUR_USERNAME/json-extract")
tokenizer = AutoTokenizer.from_pretrained("YOUR_USERNAME/json-extract")
```

## Training details

| Parameter | Value |
|---|---|
| Base model | Qwen2.5-1.5B-Instruct |
| Method | LoRA (r=16, alpha=16) |
| Target modules | q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj |
| Epochs | 3 |
| Learning rate | 2e-4 |
| Batch size | 4 (x4 gradient accumulation) |
| Scheduler | Cosine |
| Optimizer | AdamW 8-bit |
| Precision | 4-bit quantized (QLoRA) |
| Max sequence length | 2048 |

## Prompt format

```
### Input: <your text here>
### Schema: <json schema>
### Output:
```

The model will generate a JSON object matching the provided schema.

## Limitations

- Optimized for short-to-medium length text inputs
- Works best with schemas similar to the training data
- May not handle highly nested or complex JSON structures
- English language only

## License

Apache 2.0