Download code/aya_ner.py from croguana/hr-ner-chat: direct link, hf CLI and curl.
- Browser
- Download file 3.49 kB
-
https://huggingface.co/datasets/croguana/hr-ner-chat/resolve/main/code/aya_ner.py
- Command line
-
hf download hf://datasets/croguana/hr-ner-chat/code/aya_ner.py
-
curl -L -o aya_ner.py https://huggingface.co/datasets/croguana/hr-ner-chat/resolve/main/code/aya_ner.py
3.49 kB
| """hr-aya-ner part: Croatian named-entity rows from CohereLabs/aya_collection_language_split, croatian/ (Apache-2.0). | |
| The Croatian split is only UNER-LLM-inst: 9,007 rows from one template, made from Universal NER hr500k news | |
| sentences (PER/ORG/LOC/OTH with character offsets; OTH = other names: products, events, laws, works, nationality nouns; kept as 'ostalo'; 39% of rows have no entities). Train and validation are used and the | |
| UNER test split is left out. Each row is rewritten as a short chat: one of several Croatian instructions plus the | |
| sentence, answered with entity lines "- <text> (osoba|organizacija|lokacija|ostalo)" or a no-entities sentence. The 250-token | |
| boilerplate prompt and JSON offsets are dropped (a 122M model cannot count characters). Screened like build.py. | |
| python aya_ner.py (source: sources/aya-hr/croatian/*.parquet) | |
| """ | |
| import gzip | |
| import json | |
| import random | |
| import re | |
| from pathlib import Path | |
| import pandas as pd | |
| from build import HERE, clean_hit, load_screen, strings | |
| SRC = Path('sources/aya-hr/croatian') | |
| KIND = {'PER': 'osoba', 'ORG': 'organizacija', 'LOC': 'lokacija', 'OTH': 'ostalo'} | |
| ASK = [ | |
| 'Pronađi sve imenovane entitete (osobe, organizacije, lokacije i ostala imena) u rečenici:\n\n{s}', | |
| 'Koje se osobe, organizacije, mjesta i druga imena spominju u ovoj rečenici?\n\n{s}', | |
| 'Izdvoji imenovane entitete iz rečenice i označi jesu li osoba, organizacija, lokacija ili ostalo (proizvodi, događaji, zakoni, djela, narodi).\n\n{s}', | |
| 'Rečenica: {s}\n\nNavedi imena osoba, organizacija, lokacija i ostala imena koja se u njoj pojavljuju.', | |
| 'Prepoznaj imenovane entitete (osoba / organizacija / lokacija / ostalo):\n{s}', | |
| ] | |
| NONE = ['U rečenici nema imenovanih entiteta.', 'Nema spomenutih osoba, organizacija, lokacija ni drugih imena.', | |
| 'Ova rečenica ne sadrži imenovane entitete.'] | |
| def main(): | |
| rng = random.Random(251125) | |
| d = pd.concat([pd.read_parquet(SRC / f'{s}-00000-of-00001.parquet') for s in ('train', 'validation')]) | |
| rows, bad = [], 0 | |
| for r in d.itertuples(): | |
| sent = r.inputs.split('koji je konačni izlaz?', 1)[1].strip() | |
| ents = json.loads('{' + r.targets.strip() + '}')['Results'] | |
| if any(not e.get('Text') or e['Text'] not in sent or e['TypeName'] not in KIND for e in ents): | |
| bad += 1 | |
| continue | |
| seen, lines = set(), [] | |
| for e in ents: | |
| if (e['Text'], e['TypeName']) not in seen: | |
| seen.add((e['Text'], e['TypeName'])) | |
| lines.append(f"- {e['Text']} ({KIND[e['TypeName']]})") | |
| rows.append(dict(id=f'aya-ner:{r.id}', messages=[ | |
| dict(role='user', content=rng.choice(ASK).format(s=sent)), | |
| dict(role='assistant', content='\n'.join(lines) if lines else rng.choice(NONE))])) | |
| auto, _ = load_screen() | |
| kept = [r for r in rows if clean_hit(auto, r)] | |
| with gzip.open(HERE / 'parts/hr-aya-ner.jsonl.gz', 'wt', encoding='utf-8') as f: | |
| for r in kept: | |
| f.write(json.dumps(r, ensure_ascii=False) + '\n') | |
| rep = json.loads((HERE / 'build.json').read_text()) | |
| rep['parts']['hr-aya-ner'] = dict(rows=len(d), bad_spans=bad, kept=len(kept), screened_out=len(rows) - len(kept), | |
| chars=sum(len(' '.join(strings(r['messages']))) for r in kept)) | |
| (HERE / 'build.json').write_text(json.dumps(rep, indent=1, ensure_ascii=False)) | |
| print(rep['parts']['hr-aya-ner']) | |
| if __name__ == '__main__': | |
| main() | |