Views
No views yet
emails: list of stringsorgs: list of {"name": "...", "type": "..."}persons: list of {"name": "...", "role": "..."}addresses: list of {"street": "...", "city": "...", "state": "...", "zip_code": "...", "country": "...", "full_text": "..."}phones: list of stringsbank_accounts: list of {"account_number": "...", "bank_name": "..."}dates: list of stringsSERVICE AGREEMENT
This agreement is made on June 1, 2025 between Acme Robotics Inc.,
located at 500 Market Street, Suite 12, Austin, TX 78701, and
Jane Doe, acting as an independent contractor. Contact: jane.doe@acme-robotics.com.1{
2 "emails": ["jane.doe@acme-robotics.com"],
3 "orgs": [{"name": "Acme Robotics Inc.", "type": "Inc."}],
4 "persons": [{"name": "Jane Doe", "role": "independent contractor"}],
5 "addresses": [{"street": null, "city": "Austin", "state": "TX", "zip_code": "78701", "country": null, "full_text": "500 Market Street, Suite 12, Austin, TX 78701"}],
6 "phones": [],
7 "bank_accounts": [],
8 "dates": ["June 1, 2025"]
9}1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4model = AutoModelForCausalLM.from_pretrained(
5 "khaled12331/qwen3-4b-cuad-entities",
6 torch_dtype=torch.bfloat16,
7 device_map="auto",
8 trust_remote_code=True
9)
10tokenizer = AutoTokenizer.from_pretrained(
11 "khaled12331/qwen3-4b-cuad-entities",
12 trust_remote_code=True
13)1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4MODEL_ID = "khaled12331/qwen3-4b-cuad-entities"
5
6tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
7model = AutoModelForCausalLM.from_pretrained(
8 MODEL_ID,
9 torch_dtype=torch.bfloat16,
10 device_map="auto",
11 trust_remote_code=True
12)
13
14SYSTEM_PROMPT = (
15 "You are a legal document entity extraction specialist.
16"
17 "Extract all entities from the contract into strict JSON with these keys:
18"
19 "emails, orgs, persons, addresses, phones, bank_accounts, dates.
20
21"
22 "CRITICAL FORMAT RULES:
23"
24 '- emails: list of plain strings
25'
26 '- orgs: list of objects like {"name": "...", "type": "..."}
27'
28 '- persons: list of objects like {"name": "...", "role": "..."}
29'
30 '- addresses: list of objects like {"street": "...", "city": "...", "state": "...", "zip_code": "...", "country": "...", "full_text": "..."}
31'
32 '- phones: list of plain strings
33'
34 '- bank_accounts: list of objects like {"account_number": "...", "bank_name": "..."}
35'
36 '- dates: Extract ALL dates
37
38'
39 "Output ONLY valid JSON. No markdown, no explanations."
40)
41
42contract = "YOUR CONTRACT TEXT HERE"
43
44messages = [
45 {"role": "system", "content": SYSTEM_PROMPT},
46 {"role": "user", "content": contract},
47]
48prompt = tokenizer.apply_chat_template(
49 messages, tokenize=False, add_generation_prompt=True, enable_thinking=False
50)
51inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
52
53with torch.no_grad():
54 output = model.generate(**inputs, max_new_tokens=512, do_sample=False)
55
56result = tokenizer.decode(output[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
57print(result)