max-long commited on
Commit
e21f4c1
·
verified ·
1 Parent(s): c2564fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -8
app.py CHANGED
@@ -26,6 +26,7 @@ def ner(text: str):
26
  # Predict entities using the fine-tuned GLiNER model
27
  entities = model.predict_entities(text, labels, flat_ner=True, threshold=threshold)
28
 
 
29
  textile_entities = [
30
  {
31
  "entity": ent["label"],
@@ -38,15 +39,14 @@ def ner(text: str):
38
  if ent["label"] == "Textile Machinery"
39
  ]
40
 
41
- highlighted_text = text
42
- for ent in sorted(textile_entities, key=lambda x: x['start'], reverse=True):
43
- highlighted_text = (
44
- highlighted_text[:ent['start']] +
45
- f"<span style='background-color: yellow; font-weight: bold;'>{highlighted_text[ent['start']:ent['end']]}</span>" +
46
- highlighted_text[ent['end']:]
47
- )
48
 
49
- return highlighted_text, textile_entities
 
 
 
 
50
 
51
  # Gradio Interface
52
  with gr.Blocks(title="Textile Machinery NER Demo") as demo:
 
26
  # Predict entities using the fine-tuned GLiNER model
27
  entities = model.predict_entities(text, labels, flat_ner=True, threshold=threshold)
28
 
29
+ # Filter for "Textile Machinery" entities
30
  textile_entities = [
31
  {
32
  "entity": ent["label"],
 
39
  if ent["label"] == "Textile Machinery"
40
  ]
41
 
42
+ # Prepare entities for color-coded display using gr.HighlightedText in the required dictionary format
43
+ highlights = [{"start": ent["start"], "end": ent["end"], "entity": ent["entity"]} for ent in textile_entities]
 
 
 
 
 
44
 
45
+ # Return two outputs: one for the highlighted text and one for the entities in JSON format
46
+ return {
47
+ "text": text,
48
+ "entities": highlights
49
+ }, textile_entities
50
 
51
  # Gradio Interface
52
  with gr.Blocks(title="Textile Machinery NER Demo") as demo: