File size: 1,840 Bytes
b2ac475
 
b2d8dd2
 
b2ac475
b2d8dd2
74da365
 
 
 
 
b2d8dd2
 
 
 
 
b2ac475
 
 
 
 
 
b2d8dd2
932db51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2d8dd2
b2ac475
 
932db51
 
 
 
b2d8dd2
932db51
 
b2ac475
b2d8dd2
b2ac475
 
932db51
 
 
 
b2ac475
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import gradio as gr
from transformers import pipeline
from huggingface_hub import login
import os

token = os.getenv("HF_TOKEN")  # Get token from environment variable
if token:
    login(token=token)
else:
    print("⚠️ Warning: HF_TOKEN not found. Set it in Space settings if using private models.")


if token:
    login(token=token)

# Load your custom NER model (unchanged)
ner_pipeline = pipeline(
    "token-classification",
    model="gamalyxd/outlaw-ocean-ner",
    aggregation_strategy="simple"
)

# Optional: merge consecutive spans of the same entity (unchanged)
def merge_spans(entities):
    merged = []
    for ent in entities:
        if (merged
            and ent['entity_group'] == merged[-1]['entity_group']
            and ent['start'] == merged[-1]['end']):
            # Extend previous span
            merged[-1]['word'] += ent['word']
            merged[-1]['end'] = ent['end']
            merged[-1]['score'] = max(merged[-1]['score'], ent['score'])
        else:
            merged.append(ent.copy())
    for ent in merged:
        ent['word'] = ent['word'].strip()
    return merged

# Function to run NER (unchanged)
def run_ner(text):
    results = ner_pipeline(text)
    results = merge_spans(results)
    if not results:
        return "No entities detected."
    return "\n".join(
        f"{ent['entity_group']:12s} ({ent['score']:.2f}): \"{ent['word']}\""
        for ent in results
    )

# Gradio interface (unchanged)
demo = gr.Interface(
    fn=run_ner,
    inputs=gr.Textbox(lines=8, placeholder="Enter maritime or fisheries text here..."),
    outputs=gr.Textbox(label="Detected Entities"),
    title="🌊 Outlaw Ocean NER Model",
    description="Identifies key entities in maritime and fisheries reports, including vessels, labor issues, IUU fishing, ESG claims, and more."
)

demo.launch()