max-long commited on
Commit
f625748
·
verified ·
1 Parent(s): 4fc3bec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -21
app.py CHANGED
@@ -12,37 +12,34 @@ dataset_iter = load_dataset(
12
  ).shuffle(seed=42) # Shuffle added
13
 
14
  # Load the model
15
- model = GLiNER.from_pretrained("max-long/textile_machines_3_oct", trust_remote_code=True)
16
 
17
- def ner(text: str, labels: str, threshold: float):
18
  # Convert user-provided labels (comma-separated string) into a list
19
  labels_list = [label.strip() for label in labels.split(",")]
20
 
21
- # Predict entities using the fine-tuned GLiNER model
22
- entities = model.predict_entities(text, labels_list, flat_ner=True, threshold=threshold)
23
 
24
- # Prepare data for HighlightedText
25
- highlighted_text = text
26
- for ent in sorted(entities, key=lambda x: x['start'], reverse=True):
27
- highlighted_text = (
28
- highlighted_text[:ent['start']] +
29
- f"<span style='background-color: yellow; font-weight: bold;'>{highlighted_text[ent['start']:ent['end']]}</span>" +
30
- highlighted_text[ent['end']:]
31
- )
32
 
33
- return highlighted_text, entities
 
 
 
34
 
35
- with gr.Blocks(title="General NER Demo") as demo:
36
  gr.Markdown(
37
  """
38
  # General Entity Recognition Demo
39
- This demo selects a random text snippet from a subset of the British Library's books dataset and identifies entities using a fine-tuned GLiNER model. You can specify the entities you want to find.
40
  """
41
  )
42
 
43
  # Display a random example
44
  input_text = gr.Textbox(
45
- value="The machine is fed by means of an endless apron, the wool entering at the smaller end...",
46
  label="Text input",
47
  placeholder="Enter your text here",
48
  lines=5
@@ -50,7 +47,7 @@ with gr.Blocks(title="General NER Demo") as demo:
50
 
51
  with gr.Row() as row:
52
  labels = gr.Textbox(
53
- value="Machine, Wool", # Default example labels
54
  label="Labels",
55
  placeholder="Enter your labels here (comma separated)",
56
  scale=2,
@@ -64,9 +61,14 @@ with gr.Blocks(title="General NER Demo") as demo:
64
  info="Lower the threshold to increase how many entities get predicted.",
65
  scale=1,
66
  )
 
 
 
 
 
67
 
68
- # Define output components
69
- output_highlighted = gr.HTML(label="Predicted Entities")
70
  output_entities = gr.JSON(label="Entities")
71
 
72
  submit_btn = gr.Button("Find Entities!")
@@ -84,8 +86,8 @@ with gr.Blocks(title="General NER Demo") as demo:
84
 
85
  # Connect submit button
86
  submit_btn.click(
87
- fn=lambda text, labels, threshold: ner(text, labels, threshold),
88
- inputs=[input_text, labels, threshold],
89
  outputs=[output_highlighted, output_entities]
90
  )
91
 
 
12
  ).shuffle(seed=42) # Shuffle added
13
 
14
  # Load the model
15
+ model = GLiNER.from_pretrained("urchade/gliner_multi-v2.1", trust_remote_code=True)
16
 
17
+ def ner(text: str, labels: str, threshold: float, nested_ner: bool):
18
  # Convert user-provided labels (comma-separated string) into a list
19
  labels_list = [label.strip() for label in labels.split(",")]
20
 
21
+ # Predict entities using the GLiNER model
22
+ entities = model.predict_entities(text, labels_list, flat_ner=not nested_ner, threshold=threshold)
23
 
24
+ # Prepare entities for color-coded display using gr.HighlightedText
25
+ highlights = [(ent["start"], ent["end"], ent["label"]) for ent in entities]
 
 
 
 
 
 
26
 
27
+ return {
28
+ "text": text,
29
+ "entities": highlights
30
+ }
31
 
32
+ with gr.Blocks(title="General NER with Color-Coded Output") as demo:
33
  gr.Markdown(
34
  """
35
  # General Entity Recognition Demo
36
+ This demo selects a random text snippet from a subset of the British Library's books dataset and identifies entities using the GLiNER NER model. You can specify the entities you want to find, and results will be displayed in a color-coded format.
37
  """
38
  )
39
 
40
  # Display a random example
41
  input_text = gr.Textbox(
42
+ value="Click 'Get New Snippet' to load a random sample from the British Library Dataset",
43
  label="Text input",
44
  placeholder="Enter your text here",
45
  lines=5
 
47
 
48
  with gr.Row() as row:
49
  labels = gr.Textbox(
50
+ value="Places, People", # Default example labels
51
  label="Labels",
52
  placeholder="Enter your labels here (comma separated)",
53
  scale=2,
 
61
  info="Lower the threshold to increase how many entities get predicted.",
62
  scale=1,
63
  )
64
+ nested_ner = gr.Checkbox(
65
+ value=False,
66
+ label="Nested NER",
67
+ info="Enable Nested NER?",
68
+ )
69
 
70
+ # Define output components using HighlightedText for color-coded display
71
+ output_highlighted = gr.HighlightedText(label="Predicted Entities")
72
  output_entities = gr.JSON(label="Entities")
73
 
74
  submit_btn = gr.Button("Find Entities!")
 
86
 
87
  # Connect submit button
88
  submit_btn.click(
89
+ fn=ner,
90
+ inputs=[input_text, labels, threshold, nested_ner],
91
  outputs=[output_highlighted, output_entities]
92
  )
93