ArneBinder commited on
Commit
bfcba2d
1 Parent(s): 54625d7

Upload 3 files

Browse files
Files changed (2) hide show
  1. app.py +3 -57
  2. rendering_utils.py +1 -1
app.py CHANGED
@@ -1,75 +1,21 @@
1
  import json
2
- from collections import defaultdict
3
- from typing import Dict, List, Optional, Tuple, Union
4
 
5
  import gradio as gr
6
  from pie_modules.models import * # noqa: F403
7
  from pie_modules.taskmodules import * # noqa: F403
8
- from pytorch_ie.annotations import BinaryRelation, LabeledSpan
9
  from pytorch_ie.auto import AutoPipeline
10
  from pytorch_ie.documents import TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions
11
  from pytorch_ie.models import * # noqa: F403
12
  from pytorch_ie.taskmodules import * # noqa: F403
13
 
14
- from .rendering_utils import render_pretty_table, render_spacy
15
-
16
 
17
  RENDER_WITH_DISPLACY = "displaCy + highlighted arguments"
18
  RENDER_WITH_PRETTY_TABLE = "Pretty Table"
19
 
20
 
21
- def inject_relation_data(
22
- html: str,
23
- sorted_entities,
24
- binary_relations: List[BinaryRelation],
25
- additional_colors: Optional[Dict[str, Union[str, dict]]] = None,
26
- ) -> str:
27
- from bs4 import BeautifulSoup
28
-
29
- # Parse the HTML using BeautifulSoup
30
- soup = BeautifulSoup(html, "html.parser")
31
-
32
- entity2tails = defaultdict(list)
33
- entity2heads = defaultdict(list)
34
- for relation in binary_relations:
35
- entity2heads[relation.tail].append((relation.head, relation.label))
36
- entity2tails[relation.head].append((relation.tail, relation.label))
37
-
38
- entity2id = {entity: f"entity-{idx}" for idx, entity in enumerate(sorted_entities)}
39
-
40
- # Add unique IDs to each entity
41
- entities = soup.find_all(class_="entity")
42
- for idx, entity in enumerate(entities):
43
- entity["id"] = f"entity-{idx}"
44
- original_color = entity["style"].split("background:")[1].split(";")[0].strip()
45
- entity["data-color-original"] = original_color
46
- if additional_colors is not None:
47
- for key, color in additional_colors.items():
48
- entity[f"data-color-{key}"] = (
49
- json.dumps(color) if isinstance(color, dict) else color
50
- )
51
- entity_annotation = sorted_entities[idx]
52
- # sanity check
53
- if str(entity_annotation) != entity.next:
54
- raise ValueError(f"Entity text mismatch: {entity_annotation} != {entity.text}")
55
- entity["data-label"] = entity_annotation.label
56
- entity["data-relation-tails"] = json.dumps(
57
- [
58
- {"entity-id": entity2id[tail], "label": label}
59
- for tail, label in entity2tails.get(entity_annotation, [])
60
- ]
61
- )
62
- entity["data-relation-heads"] = json.dumps(
63
- [
64
- {"entity-id": entity2id[head], "label": label}
65
- for head, label in entity2heads.get(entity_annotation, [])
66
- ]
67
- )
68
-
69
- # Return the modified HTML as a string
70
- return str(soup)
71
-
72
-
73
  def predict(text: str) -> Tuple[dict, str]:
74
  document = TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions(text=text)
75
 
 
1
  import json
2
+ from typing import Tuple
 
3
 
4
  import gradio as gr
5
  from pie_modules.models import * # noqa: F403
6
  from pie_modules.taskmodules import * # noqa: F403
7
+ from pytorch_ie.annotations import LabeledSpan
8
  from pytorch_ie.auto import AutoPipeline
9
  from pytorch_ie.documents import TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions
10
  from pytorch_ie.models import * # noqa: F403
11
  from pytorch_ie.taskmodules import * # noqa: F403
12
 
13
+ from rendering_utils import render_pretty_table, render_spacy
 
14
 
15
  RENDER_WITH_DISPLACY = "displaCy + highlighted arguments"
16
  RENDER_WITH_PRETTY_TABLE = "Pretty Table"
17
 
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  def predict(text: str) -> Tuple[dict, str]:
20
  document = TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions(text=text)
21
 
rendering_utils.py CHANGED
@@ -5,7 +5,7 @@ from typing import Dict, List, Optional, Union
5
  from pytorch_ie.annotations import BinaryRelation
6
  from pytorch_ie.documents import TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions
7
 
8
- from .rendering_utils_displacy import EntityRenderer
9
 
10
 
11
  def render_pretty_table(
 
5
  from pytorch_ie.annotations import BinaryRelation
6
  from pytorch_ie.documents import TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions
7
 
8
+ from rendering_utils_displacy import EntityRenderer
9
 
10
 
11
  def render_pretty_table(