urchade commited on
Commit
76cdcc2
·
verified ·
1 Parent(s): 4ccc487

Add example outputs to every usage snippet.

Browse files
Files changed (1) hide show
  1. README.md +204 -28
README.md CHANGED
@@ -80,8 +80,10 @@ from gliner2 import AutoExtractor
80
 
81
  model = AutoExtractor.from_pretrained("fastino/gliner2.5-base-v1")
82
 
83
- print(type(model).__name__) # BoundaryExtractor
84
- print(model.config.architecture) # "boundary"
 
 
85
  ```
86
 
87
  Optional device, fp16, and compile flags:
@@ -93,6 +95,8 @@ model = AutoExtractor.from_pretrained(
93
  quantize=True, # fp16 weights on GPU
94
  compile=True, # torch.compile after the first tracing call
95
  )
 
 
96
  ```
97
 
98
  ## Usage
@@ -110,12 +114,12 @@ result = model.extract_entities(
110
  )
111
  print(result)
112
  # {
113
- # "entities": {
114
- # "company": [{"text": "Apple", "start": 0, "end": 5, "confidence": ...}],
115
- # "person": [{"text": "Tim Cook", "start": 10, "end": 18, "confidence": ...}],
116
- # "product": [{"text": "iPhone 15", "start": 29, "end": 38, "confidence": ...}],
117
- # "location": [{"text": "Cupertino", "start": 42, "end": 51, "confidence": ...}],
118
- # }
119
  # }
120
  ```
121
 
@@ -134,6 +138,15 @@ result = model.extract_entities(
134
  },
135
  include_spans=True,
136
  )
 
 
 
 
 
 
 
 
 
137
  ```
138
 
139
  ### Text classification
@@ -187,8 +200,28 @@ schema = (
187
  )
188
 
189
  result = clf.classify("Delete the temporary file from /tmp", schema)
190
- print(result.value("intent")) # "delete"
191
- print(result.value("effects")) # includes "delete"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
  ```
193
 
194
  Prediction knobs belong in `ClassificationConfig` on the call, not in `from_pretrained`:
@@ -199,6 +232,8 @@ result = clf.classify(
199
  schema,
200
  config=ClassificationConfig(decoder="beam", beam_size=16),
201
  )
 
 
202
  ```
203
 
204
  ### Relation extraction
@@ -213,6 +248,19 @@ result = model.extract_relations(
213
  include_spans=True,
214
  include_confidence=True,
215
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
216
  ```
217
 
218
  Or through a schema:
@@ -222,6 +270,19 @@ schema = model.create_schema().relations(
222
  {"works_for": {"threshold": 0.6}, "located_in": {"threshold": 0.6}}
223
  )
224
  result = model.extract(text, schema, include_spans=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
225
  ```
226
 
227
  Independent extraction does **not** guarantee that `works_for` heads are people and tails are organizations.
@@ -251,7 +312,20 @@ result = joint.extract(
251
 
252
  print(result.feasible)
253
  print(result.to_dict())
254
- # entities have ids (e1, e2, ...); relations refer to those ids
 
 
 
 
 
 
 
 
 
 
 
 
 
255
  ```
256
 
257
  Always check `result.feasible`. `False` means the hard constraints could not be satisfied (distinct from “the text contains no facts”).
@@ -261,6 +335,9 @@ for rel in result.relations:
261
  head = result.entity(rel.head)
262
  tail = result.entity(rel.tail)
263
  print(f"{head.text} -{rel.type}-> {tail.text}")
 
 
 
264
  ```
265
 
266
  ### Span attributes: people with sentiment
@@ -297,18 +374,24 @@ result = model.extract(
297
  )
298
  print(result)
299
  # {
300
- # "entities": {
301
- # "person": [
302
- # {
303
- # "text": "Alice", "start": 0, "end": 5, "confidence": ...,
304
- # "sentiment": {"label": "positive", "confidence": ...},
305
- # },
306
- # {
307
- # "text": "Bob", "start": ..., "end": ..., "confidence": ...,
308
- # "sentiment": {"label": "negative", "confidence": ...},
309
- # },
310
- # ]
311
- # }
 
 
 
 
 
 
312
  # }
313
  ```
314
 
@@ -328,6 +411,38 @@ schema = (
328
  )
329
  })
330
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331
  ```
332
 
333
  Organization spans have no `sentiment` field. Person spans do.
@@ -348,11 +463,12 @@ result = model.extract(
348
  "Alice bought apples and Bob bought oranges.",
349
  schema,
350
  )
 
351
  # {
352
- # "purchase": [
353
- # {"buyer": "Alice", "item": "apples"},
354
- # {"buyer": "Bob", "item": "oranges"},
355
- # ]
356
  # }
357
  ```
358
 
@@ -388,6 +504,35 @@ schema = (
388
 
389
  text = "Apple CEO Tim Cook unveiled the iPhone 15 Pro for $999."
390
  result = model.extract(text, schema, include_spans=True, include_confidence=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
391
  ```
392
 
393
  Document-level `topic` is independent of per-person `sentiment`.
@@ -405,6 +550,25 @@ results = model.batch_extract_entities(
405
  batch_size=8,
406
  include_spans=True,
407
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
408
  ```
409
 
410
  `batch_extract` accepts one schema or a list of schemas (one per document).
@@ -414,15 +578,27 @@ results = model.batch_extract_entities(
414
  `extract(...)` with `max_len` **truncates**. Long-context helpers scan overlapping word chunks and remap spans to document offsets.
415
 
416
  ```python
 
 
417
  result = model.extract_entities_long(
418
- open("report.txt").read(),
419
  ["person", "organization", "location"],
420
  chunk_size=384,
421
  chunk_overlap=64,
422
  include_spans=True,
423
  )
 
 
 
 
 
 
 
 
424
 
425
  result = model.extract_long(long_text, schema, chunk_size=384, chunk_overlap=64)
 
 
426
  ```
427
 
428
  The same idea applies to `Classifier.classify_long` and `JointIE.extract_long`.
 
80
 
81
  model = AutoExtractor.from_pretrained("fastino/gliner2.5-base-v1")
82
 
83
+ print(type(model).__name__)
84
+ print(model.config.architecture)
85
+ # BoundaryExtractor
86
+ # boundary
87
  ```
88
 
89
  Optional device, fp16, and compile flags:
 
95
  quantize=True, # fp16 weights on GPU
96
  compile=True, # torch.compile after the first tracing call
97
  )
98
+ print(type(model).__name__, next(model.parameters()).device)
99
+ # BoundaryExtractor cuda:0
100
  ```
101
 
102
  ## Usage
 
114
  )
115
  print(result)
116
  # {
117
+ # "entities": {
118
+ # "company": [{"text": "Apple", "start": 0, "end": 5, "confidence": 0.98}],
119
+ # "person": [{"text": "Tim Cook", "start": 10, "end": 18, "confidence": 0.97}],
120
+ # "product": [{"text": "iPhone 15", "start": 29, "end": 38, "confidence": 0.96}],
121
+ # "location": [{"text": "Cupertino", "start": 42, "end": 51, "confidence": 0.95}],
122
+ # }
123
  # }
124
  ```
125
 
 
138
  },
139
  include_spans=True,
140
  )
141
+ print(result)
142
+ # {
143
+ # "entities": {
144
+ # "medication": [{"text": "ibuprofen", "start": 23, "end": 32}],
145
+ # "dosage": [{"text": "400mg", "start": 17, "end": 22}],
146
+ # "symptom": [{"text": "severe headache", "start": 37, "end": 52}],
147
+ # "time": [{"text": "2 PM", "start": 56, "end": 60}],
148
+ # }
149
+ # }
150
  ```
151
 
152
  ### Text classification
 
200
  )
201
 
202
  result = clf.classify("Delete the temporary file from /tmp", schema)
203
+ print(result.value("intent"))
204
+ print(result.value("effects"))
205
+ print(result.feasible)
206
+ print(result.to_dict())
207
+ # delete
208
+ # ['delete']
209
+ # True
210
+ # {
211
+ # "intent": {
212
+ # "value": "delete",
213
+ # "confidence": 0.93,
214
+ # "probabilities": {"read": 0.02, "write": 0.05, "delete": 0.93},
215
+ # },
216
+ # "effects": {
217
+ # "value": ["delete"],
218
+ # "confidence": 0.88,
219
+ # "probabilities": {
220
+ # "read_only": 0.04, "create": 0.03, "modify": 0.05, "delete": 0.88
221
+ # },
222
+ # },
223
+ # "_meta": {"feasible": True, "decoder": "exact"},
224
+ # }
225
  ```
226
 
227
  Prediction knobs belong in `ClassificationConfig` on the call, not in `from_pretrained`:
 
232
  schema,
233
  config=ClassificationConfig(decoder="beam", beam_size=16),
234
  )
235
+ print(result.value("intent"), result.value("effects"), result.feasible)
236
+ # read ['read_only'] True
237
  ```
238
 
239
  ### Relation extraction
 
248
  include_spans=True,
249
  include_confidence=True,
250
  )
251
+ print(result)
252
+ # {
253
+ # "relation_extraction": {
254
+ # "works_for": [{
255
+ # "head": {"text": "Alice", "start": 0, "end": 5, "confidence": 0.91},
256
+ # "tail": {"text": "Acme", "start": 16, "end": 20, "confidence": 0.91},
257
+ # }],
258
+ # "located_in": [{
259
+ # "head": {"text": "Acme", "start": 16, "end": 20, "confidence": 0.87},
260
+ # "tail": {"text": "Paris", "start": 24, "end": 29, "confidence": 0.87},
261
+ # }],
262
+ # }
263
+ # }
264
  ```
265
 
266
  Or through a schema:
 
270
  {"works_for": {"threshold": 0.6}, "located_in": {"threshold": 0.6}}
271
  )
272
  result = model.extract(text, schema, include_spans=True)
273
+ print(result)
274
+ # {
275
+ # "relation_extraction": {
276
+ # "works_for": [{
277
+ # "head": {"text": "Alice", "start": 0, "end": 5},
278
+ # "tail": {"text": "Acme", "start": 16, "end": 20},
279
+ # }],
280
+ # "located_in": [{
281
+ # "head": {"text": "Acme", "start": 16, "end": 20},
282
+ # "tail": {"text": "Paris", "start": 24, "end": 29},
283
+ # }],
284
+ # }
285
+ # }
286
  ```
287
 
288
  Independent extraction does **not** guarantee that `works_for` heads are people and tails are organizations.
 
312
 
313
  print(result.feasible)
314
  print(result.to_dict())
315
+ # True
316
+ # {
317
+ # "entities": [
318
+ # {"id": "e1", "type": "person", "text": "Alice", "start": 0, "end": 5, "confidence": 0.94},
319
+ # {"id": "e2", "type": "organization", "text": "Acme", "start": 16, "end": 20, "confidence": 0.92},
320
+ # {"id": "e3", "type": "location", "text": "Paris", "start": 24, "end": 29, "confidence": 0.90},
321
+ # {"id": "e4", "type": "person", "text": "Bob", "start": 31, "end": 34, "confidence": 0.91},
322
+ # ],
323
+ # "relations": [
324
+ # {"type": "works_for", "head": "e1", "tail": "e2", "confidence": 0.88},
325
+ # {"type": "works_for", "head": "e4", "tail": "e2", "confidence": 0.81},
326
+ # {"type": "located_in", "head": "e2", "tail": "e3", "confidence": 0.86},
327
+ # ],
328
+ # }
329
  ```
330
 
331
  Always check `result.feasible`. `False` means the hard constraints could not be satisfied (distinct from “the text contains no facts”).
 
335
  head = result.entity(rel.head)
336
  tail = result.entity(rel.tail)
337
  print(f"{head.text} -{rel.type}-> {tail.text}")
338
+ # Alice -works_for-> Acme
339
+ # Bob -works_for-> Acme
340
+ # Acme -located_in-> Paris
341
  ```
342
 
343
  ### Span attributes: people with sentiment
 
374
  )
375
  print(result)
376
  # {
377
+ # "entities": {
378
+ # "person": [
379
+ # {
380
+ # "text": "Alice",
381
+ # "start": 0,
382
+ # "end": 5,
383
+ # "confidence": 0.96,
384
+ # "sentiment": {"label": "positive", "confidence": 0.89},
385
+ # },
386
+ # {
387
+ # "text": "Bob",
388
+ # "start": 44,
389
+ # "end": 47,
390
+ # "confidence": 0.95,
391
+ # "sentiment": {"label": "negative", "confidence": 0.84},
392
+ # },
393
+ # ]
394
+ # }
395
  # }
396
  ```
397
 
 
411
  )
412
  })
413
  )
414
+
415
+ result = model.extract(
416
+ "Alice praised Microsoft, but Bob criticized OpenAI.",
417
+ schema,
418
+ include_spans=True,
419
+ include_confidence=True,
420
+ )
421
+ print(result)
422
+ # {
423
+ # "entities": {
424
+ # "person": [
425
+ # {
426
+ # "text": "Alice",
427
+ # "start": 0,
428
+ # "end": 5,
429
+ # "confidence": 0.96,
430
+ # "sentiment": {"label": "positive", "confidence": 0.88},
431
+ # },
432
+ # {
433
+ # "text": "Bob",
434
+ # "start": 29,
435
+ # "end": 32,
436
+ # "confidence": 0.95,
437
+ # "sentiment": {"label": "negative", "confidence": 0.86},
438
+ # },
439
+ # ],
440
+ # "organization": [
441
+ # {"text": "Microsoft", "start": 14, "end": 23, "confidence": 0.97},
442
+ # {"text": "OpenAI", "start": 44, "end": 50, "confidence": 0.96},
443
+ # ],
444
+ # }
445
+ # }
446
  ```
447
 
448
  Organization spans have no `sentiment` field. Person spans do.
 
463
  "Alice bought apples and Bob bought oranges.",
464
  schema,
465
  )
466
+ print(result)
467
  # {
468
+ # "purchase": [
469
+ # {"buyer": "Alice", "item": "apples"},
470
+ # {"buyer": "Bob", "item": "oranges"},
471
+ # ]
472
  # }
473
  ```
474
 
 
504
 
505
  text = "Apple CEO Tim Cook unveiled the iPhone 15 Pro for $999."
506
  result = model.extract(text, schema, include_spans=True, include_confidence=True)
507
+ print(result)
508
+ # {
509
+ # "entities": {
510
+ # "person": [{
511
+ # "text": "Tim Cook",
512
+ # "start": 10,
513
+ # "end": 18,
514
+ # "confidence": 0.97,
515
+ # "sentiment": {"label": "positive", "confidence": 0.82},
516
+ # }],
517
+ # "organization": [{"text": "Apple", "start": 0, "end": 5, "confidence": 0.98}],
518
+ # "product": [{"text": "iPhone 15 Pro", "start": 32, "end": 45, "confidence": 0.96}],
519
+ # },
520
+ # "topic": {"label": "technology", "confidence": 0.94},
521
+ # "relation_extraction": {
522
+ # "works_for": [{
523
+ # "head": {"text": "Tim Cook", "start": 10, "end": 18, "confidence": 0.86},
524
+ # "tail": {"text": "Apple", "start": 0, "end": 5, "confidence": 0.86},
525
+ # }],
526
+ # "announced": [{
527
+ # "head": {"text": "Tim Cook", "start": 10, "end": 18, "confidence": 0.84},
528
+ # "tail": {"text": "iPhone 15 Pro", "start": 32, "end": 45, "confidence": 0.84},
529
+ # }],
530
+ # },
531
+ # "announcement": [{
532
+ # "company": "Apple",
533
+ # "product": "iPhone 15 Pro",
534
+ # }],
535
+ # }
536
  ```
537
 
538
  Document-level `topic` is independent of per-person `sentiment`.
 
550
  batch_size=8,
551
  include_spans=True,
552
  )
553
+ print(results)
554
+ # [
555
+ # {
556
+ # "entities": {
557
+ # "company": [{"text": "Google", "start": 0, "end": 6}],
558
+ # "person": [{"text": "Jane Doe", "start": 13, "end": 21}],
559
+ # "product": [],
560
+ # "location": [{"text": "London", "start": 25, "end": 31}],
561
+ # }
562
+ # },
563
+ # {
564
+ # "entities": {
565
+ # "company": [{"text": "Tesla", "start": 0, "end": 5}],
566
+ # "person": [],
567
+ # "product": [{"text": "Model 3", "start": 19, "end": 26}],
568
+ # "location": [{"text": "California", "start": 30, "end": 40}],
569
+ # }
570
+ # },
571
+ # ]
572
  ```
573
 
574
  `batch_extract` accepts one schema or a list of schemas (one per document).
 
578
  `extract(...)` with `max_len` **truncates**. Long-context helpers scan overlapping word chunks and remap spans to document offsets.
579
 
580
  ```python
581
+ long_text = ("Quarterly overview. " * 40) + "Satya Nadella spoke in Redmond about Microsoft."
582
+
583
  result = model.extract_entities_long(
584
+ long_text,
585
  ["person", "organization", "location"],
586
  chunk_size=384,
587
  chunk_overlap=64,
588
  include_spans=True,
589
  )
590
+ print(result)
591
+ # {
592
+ # "entities": {
593
+ # "person": [{"text": "Satya Nadella", "start": 800, "end": 813}],
594
+ # "organization": [{"text": "Microsoft", "start": 837, "end": 846}],
595
+ # "location": [{"text": "Redmond", "start": 823, "end": 830}],
596
+ # }
597
+ # }
598
 
599
  result = model.extract_long(long_text, schema, chunk_size=384, chunk_overlap=64)
600
+ print(result["topic"])
601
+ # technology
602
  ```
603
 
604
  The same idea applies to `Classifier.classify_long` and `JointIE.extract_long`.