andreagemelli/xfund-kie-it
Viewer • Updated • 199
How to use andreagemelli/LFM2.5-350M-IT-Extract with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-generation", model="andreagemelli/LFM2.5-350M-IT-Extract")
messages = [
{"role": "user", "content": "Who are you?"},
]
pipe(messages) # Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("andreagemelli/LFM2.5-350M-IT-Extract")
model = AutoModelForCausalLM.from_pretrained("andreagemelli/LFM2.5-350M-IT-Extract", device_map="auto")
messages = [
{"role": "user", "content": "Who are you?"},
]
inputs = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
return_tensors="pt",
).to(model.device)
outputs = model.generate(**inputs, max_new_tokens=40)
print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:]))How to use andreagemelli/LFM2.5-350M-IT-Extract with vLLM:
# Install vLLM from pip:
pip install vllm
# Start the vLLM server:
vllm serve "andreagemelli/LFM2.5-350M-IT-Extract"
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:8000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "andreagemelli/LFM2.5-350M-IT-Extract",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'docker model run hf.co/andreagemelli/LFM2.5-350M-IT-Extract
How to use andreagemelli/LFM2.5-350M-IT-Extract with SGLang:
# Install SGLang from pip:
pip install sglang
# Start the SGLang server:
python3 -m sglang.launch_server \
--model-path "andreagemelli/LFM2.5-350M-IT-Extract" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "andreagemelli/LFM2.5-350M-IT-Extract",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'docker run --gpus all \
--shm-size 32g \
-p 30000:30000 \
-v ~/.cache/huggingface:/root/.cache/huggingface \
--env "HF_TOKEN=<secret>" \
--ipc=host \
lmsysorg/sglang:latest \
python3 -m sglang.launch_server \
--model-path "andreagemelli/LFM2.5-350M-IT-Extract" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "andreagemelli/LFM2.5-350M-IT-Extract",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'How to use andreagemelli/LFM2.5-350M-IT-Extract with Docker Model Runner:
docker model run hf.co/andreagemelli/LFM2.5-350M-IT-Extract
xfund-kie (derived from XFUND Italian split, see xfund-kie/README.md)it)xfund-kie validation)
| Model | Avg. F1 | Errors (JSON parse / total val docs) |
|---|---|---|
LiquidAI/LFM2.5-350M (base) |
0.2877 | 6 / 50 |
andreagemelli/LFM2.5-350M-IT-Extract (fine-tuned) |
0.6639 | 10 / 50 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
import torch, json
from datasets import load_dataset
model_id = "andreagemelli/LFM2.5-350M-IT-Extract"
device = "cuda" if torch.cuda.is_available() else "cpu"
model = AutoModelForCausalLM.from_pretrained(model_id, dtype=torch.bfloat16).to(device)
tokenizer = AutoTokenizer.from_pretrained(model_id)
# Load dataset from Hugging Face Hub (no local repo needed)
dataset = load_dataset("andreagemelli/xfund-kie-it", split="validation")
doc = dataset.filter(lambda x: x["source"] == "it_val_0")[0]
annotation = doc["annotation"] # from dataset messages or annotation field
schema_text = "".join([f"{k}: {v}.\n" for k, v in REF_SCHEMA.items() if k in annotation]) # cognome: surname of the person.\nnome: ...
user_text = doc["text"] if "text" in doc else doc["messages"][1]["content"]
messages = [
{"role": "system", "content": SYTEM_PROMPT_DEFAULT + schema_text},
{"role": "user", "content": user_text},
]
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(device)
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
output = model.generate(inputs, max_new_tokens=1024, do_sample=False, streamer=streamer)
Expected snippet output (ref: it_val_0 from xfund-kie/it.val.json):
{
"cognome": "VALLE",
"nome": "LUISA",
...
}
Defaults I used in my experiments:
REF_SCHEMA = json.load('/path/to/schema/json') # https://huggingface.co/datasets/andreagemelli/xfund-kie-it/blob/main/schema.json
SYTEM_PROMPT_DEFAULT = f"""Identify and extract information matching the following schema.
Return data as a JSON object. Missing data should be omitted.
"""
@misc{gemelli2026LFM2.5-350M-IT-Extract
title = {LFM2.5-350M-IT-Extract: A tiny model for italian document key information extraction},
author = {Gemelli, Andrea},
year = {2026},
howpublished = {\url{https://huggingface.co/andreagemelli/LFM2.5-350M-IT-Extract}}
}