vumichien commited on
Commit
d9ab8fb
1 Parent(s): 4284c63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -16
app.py CHANGED
@@ -5,6 +5,36 @@ import gradio as gr
5
  jp_model = GLiNER.from_pretrained("vumichien/ner-jp-gliner")
6
  meal_model = GLiNER.from_pretrained("urchade/gliner_mediumv2.1")
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  examples = [
9
  [
10
  "ner_jp",
@@ -29,23 +59,39 @@ def ner(
29
  labels = labels.split(",")
30
  if models == "ner_jp":
31
  model = jp_model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  else:
33
  model = meal_model
34
- return {
35
- "text": text,
36
- "entities": [
37
- {
38
- "entity": entity["label"],
39
- "word": entity["text"],
40
- "start": entity["start"],
41
- "end": entity["end"],
42
- "score": 0,
43
- }
44
- for entity in model.predict_entities(
45
- text, labels, flat_ner=not nested_ner, threshold=threshold
46
- )
47
- ],
48
- }
49
 
50
 
51
  with gr.Blocks(title="GLiNER-M-v2.1") as demo:
@@ -108,7 +154,7 @@ European Championship => competitions
108
  value=examples[0][0], label="Text input", placeholder="Enter your text here"
109
  )
110
  with gr.Row() as row:
111
- models = gr.Dropdown((
112
  choices=["ner_meals", "ner_jp"]
113
  value="ner_jp",
114
  label="Models",
 
5
  jp_model = GLiNER.from_pretrained("vumichien/ner-jp-gliner")
6
  meal_model = GLiNER.from_pretrained("urchade/gliner_mediumv2.1")
7
 
8
+
9
+ def merge_tokens(entities, text):
10
+ # Remove spaces from the text
11
+ merged_text = text.replace(" ", "")
12
+
13
+ updated_entities = []
14
+ for entity in entities:
15
+ # Calculate the new start and end positions
16
+ start = entity['start']
17
+ end = entity['end']
18
+
19
+ # Get the text without spaces
20
+ entity_text = entity['text'].replace(" ", "")
21
+
22
+ # Find the new start and end in the merged text
23
+ new_start = merged_text.find(entity_text)
24
+ new_end = new_start + len(entity_text)
25
+
26
+ # Update the entity with new positions
27
+ updated_entities.append({
28
+ 'start': new_start,
29
+ 'end': new_end,
30
+ 'text': entity_text,
31
+ 'label': entity['label'],
32
+ 'score': entity['score']
33
+ })
34
+
35
+ return updated_entities
36
+
37
+
38
  examples = [
39
  [
40
  "ner_jp",
 
59
  labels = labels.split(",")
60
  if models == "ner_jp":
61
  model = jp_model
62
+ tokenized_text = " ".join(list(text))
63
+ entities = model.predict_entities(tokenized_text, labels, flat_ner=not nested_ner, threshold=threshold)
64
+ updated_entities = merge_tokens(entities, tokenized_text)
65
+ return {
66
+ "text": text,
67
+ "entities": [
68
+ {
69
+ "entity": entity["label"],
70
+ "word": entity["text"],
71
+ "start": entity["start"],
72
+ "end": entity["end"],
73
+ "score": 0,
74
+ }
75
+ for entity in updated_entities
76
+ ],
77
+ }
78
  else:
79
  model = meal_model
80
+ return {
81
+ "text": text,
82
+ "entities": [
83
+ {
84
+ "entity": entity["label"],
85
+ "word": entity["text"],
86
+ "start": entity["start"],
87
+ "end": entity["end"],
88
+ "score": 0,
89
+ }
90
+ for entity in model.predict_entities(
91
+ text, labels, flat_ner=not nested_ner, threshold=threshold
92
+ )
93
+ ],
94
+ }
95
 
96
 
97
  with gr.Blocks(title="GLiNER-M-v2.1") as demo:
 
154
  value=examples[0][0], label="Text input", placeholder="Enter your text here"
155
  )
156
  with gr.Row() as row:
157
+ models = gr.Dropdown(
158
  choices=["ner_meals", "ner_jp"]
159
  value="ner_jp",
160
  label="Models",