urchade commited on
Commit
d75bf6a
·
verified ·
1 Parent(s): d782b12

Add example outputs to every usage snippet.

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