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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -13
app.py CHANGED
@@ -3,7 +3,7 @@ from gliner import GLiNER
3
  import gradio as gr
4
  from datasets import load_dataset
5
 
6
- # Load the BL dataset with streaming
7
  dataset_iter = iter(
8
  load_dataset(
9
  "max-long/bl_books_textile_filter",
@@ -13,10 +13,10 @@ dataset_iter = iter(
13
  )
14
 
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
@@ -35,16 +35,13 @@ def ner(text: str):
35
  if ent["label"] == "Textile Machinery" # Exact match with capitalization
36
  ]
37
 
38
- # Prepare data for HighlightedText
39
- highlighted_text = text
40
- for ent in sorted(textile_entities, key=lambda x: x['start'], reverse=True):
41
- highlighted_text = (
42
- highlighted_text[:ent['start']] +
43
- f"<span style='background-color: yellow; font-weight: bold;'>{highlighted_text[ent['start']:ent['end']]}</span>" +
44
- highlighted_text[ent['end']:]
45
- )
46
 
47
- return highlighted_text, textile_entities
 
 
 
48
 
49
  with gr.Blocks(title="Textile Machinery NER Demo") as demo:
50
  gr.Markdown(
@@ -63,7 +60,7 @@ with gr.Blocks(title="Textile Machinery NER Demo") as demo:
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!")
 
3
  import gradio as gr
4
  from datasets import load_dataset
5
 
6
+ # Load the BL dataset
7
  dataset_iter = iter(
8
  load_dataset(
9
  "max-long/bl_books_textile_filter",
 
13
  )
14
 
15
  # Load the model
16
+ model = GLiNER.from_pretrained("max-long/textile_machines_ner_5_oct", trust_remote_code=True)
17
 
18
  def ner(text: str):
19
+ labels = ["Textile Machinery"]
20
  threshold = 0.5
21
 
22
  # Predict entities using the fine-tuned GLiNER model
 
35
  if ent["label"] == "Textile Machinery" # Exact match with capitalization
36
  ]
37
 
38
+ # Prepare entities for color-coded display using gr.HighlightedText
39
+ highlights = [(ent["start"], ent["end"], ent["entity"]) for ent in textile_entities]
 
 
 
 
 
 
40
 
41
+ return {
42
+ "text": text,
43
+ "entities": highlights
44
+ }
45
 
46
  with gr.Blocks(title="Textile Machinery NER Demo") as demo:
47
  gr.Markdown(
 
60
  )
61
 
62
  # Define output components
63
+ output_highlighted = gr.HighlightedText(label="Predicted Entities")
64
  output_entities = gr.JSON(label="Entities")
65
 
66
  submit_btn = gr.Button("Find Textile Machinery!")