max-long commited on
Commit
a695cd7
·
verified ·
1 Parent(s): fc2149d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -32
app.py CHANGED
@@ -15,14 +15,14 @@ dataset_iter = iter(
15
  # Load the model
16
  model = GLiNER.from_pretrained("max-long/textile_machines_3_oct", trust_remote_code=True)
17
 
18
- def ner(text: str, labels: str, threshold: float, nested_ner: bool):
19
- # Split and clean labels
20
- labels = [label.strip() for label in labels.split(",")]
21
 
22
  # Predict entities using the fine-tuned GLiNER model
23
- entities = model.predict_entities(text, labels, flat_ner=not nested_ner, threshold=threshold)
24
 
25
- # Filter for "textile machinery" entities
26
  textile_entities = [
27
  {
28
  "entity": ent["label"],
@@ -32,7 +32,7 @@ def ner(text: str, labels: str, threshold: float, nested_ner: bool):
32
  "score": ent.get("score", 0),
33
  }
34
  for ent in entities
35
- if ent["label"].lower() == "textile machinery"
36
  ]
37
 
38
  # Prepare data for HighlightedText
@@ -50,7 +50,7 @@ with gr.Blocks(title="Textile Machinery NER Demo") as demo:
50
  gr.Markdown(
51
  """
52
  # Textile Machinery Entity Recognition Demo
53
- This demo selects a random text snippet from the British Library's books dataset and identifies "textile machinery" entities using a fine-tuned GLiNER model.
54
  """
55
  )
56
 
@@ -62,34 +62,11 @@ with gr.Blocks(title="Textile Machinery NER Demo") as demo:
62
  lines=5
63
  )
64
 
65
- with gr.Row():
66
- labels = gr.Textbox(
67
- value="textile machinery",
68
- label="Labels",
69
- placeholder="Enter your labels here (comma separated)",
70
- scale=2,
71
- )
72
- threshold = gr.Slider(
73
- 0,
74
- 1,
75
- value=0.3,
76
- step=0.01,
77
- label="Threshold",
78
- info="Lower the threshold to increase how many entities get predicted.",
79
- scale=1,
80
- )
81
- nested_ner = gr.Checkbox(
82
- value=False,
83
- label="Nested NER",
84
- info="Allow for nested NER?",
85
- scale=0,
86
- )
87
-
88
  # Define output components
89
  output_highlighted = gr.HTML(label="Predicted Entities")
90
  output_entities = gr.JSON(label="Entities")
91
 
92
- submit_btn = gr.Button("Analyze Random Snippet")
93
  refresh_btn = gr.Button("Get New Snippet")
94
 
95
  def get_new_snippet():
@@ -110,7 +87,7 @@ with gr.Blocks(title="Textile Machinery NER Demo") as demo:
110
  # Connect submit button
111
  submit_btn.click(
112
  fn=ner,
113
- inputs=[input_text, labels, threshold, nested_ner],
114
  outputs=[output_highlighted, output_entities]
115
  )
116
 
 
15
  # Load the model
16
  model = GLiNER.from_pretrained("max-long/textile_machines_3_oct", trust_remote_code=True)
17
 
18
+ def ner(text: str):
19
+ labels = ["Textile Machinery"] # Capitalized label for entity matching
20
+ threshold = 0.5
21
 
22
  # Predict entities using the fine-tuned GLiNER model
23
+ entities = model.predict_entities(text, labels, flat_ner=True, threshold=threshold)
24
 
25
+ # Filter for "Textile Machinery" entities
26
  textile_entities = [
27
  {
28
  "entity": ent["label"],
 
32
  "score": ent.get("score", 0),
33
  }
34
  for ent in entities
35
+ if ent["label"] == "Textile Machinery" # Exact match with capitalization
36
  ]
37
 
38
  # Prepare data for HighlightedText
 
50
  gr.Markdown(
51
  """
52
  # Textile Machinery Entity Recognition Demo
53
+ This demo selects a random text snippet from a subset of the British Library's books dataset and identifies "Textile Machinery" entities using a fine-tuned GLiNER model.
54
  """
55
  )
56
 
 
62
  lines=5
63
  )
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  # Define output components
66
  output_highlighted = gr.HTML(label="Predicted Entities")
67
  output_entities = gr.JSON(label="Entities")
68
 
69
+ submit_btn = gr.Button("Find Textile Machinery!")
70
  refresh_btn = gr.Button("Get New Snippet")
71
 
72
  def get_new_snippet():
 
87
  # Connect submit button
88
  submit_btn.click(
89
  fn=ner,
90
+ inputs=[input_text],
91
  outputs=[output_highlighted, output_entities]
92
  )
93