Spaces:
Sleeping
Sleeping
File size: 3,829 Bytes
96a57ab 83365c5 96a57ab 83365c5 88af0ff 83365c5 7599b35 83365c5 d1a4bde 51ca89b d1a4bde 51ca89b d1a4bde 83365c5 51ca89b 83365c5 96a57ab | 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | import gradio as gr
from transformers import pipeline
# Model names (keeping it programmatic)
model_names = [
"c-ho/2026-04-24-crf-classweights-clean",
"c-ho/2026-04-23-crf-classweights-clean"
]
example_sent = (
"As a result, Indo-European developed a minimal vowel system combined with a very large consonant inventory including glottalized stops, also grammatical gender and adjectival agreement."
)
# -----------------------
# UI helpers
# -----------------------
# Programmatically build the model info dict
model_info = {
model_name: {
"link": f"https://huggingface.co/{model_name}",
"usage": f"""from transformers import pipeline
ner = pipeline("ner", model="{model_name}", grouped_entities=True)
result = ner("{example_sent}")
print(result)""",
}
for model_name in model_names
}
# Load models into a dictionary programmatically for the analyze function
models = {
model_name: pipeline("ner", model=model_name) #, grouped_entities=True)
for model_name in model_names
}
def display_model_info(model_name):
info = model_info[model_name]
usage_code = info["usage"]
link = f"[Open model page]({info['link']})"
return usage_code, link
model_cache = {}
def get_model(model_name):
if model_name not in model_cache:
model_cache[model_name] = pipeline(
"ner",
model=model_name,
aggregation_strategy="simple" # safer for display
)
return model_cache[model_name]
# -----------------------
# NER function (SAFE OUTPUT)
# -----------------------
'''
# Function to run NER on input text
def analyze_text(text, model_name):
ner = models[model_name]
ner_results = ner(text)
highlighted_text = []
last_idx = 0
for entity in ner_results:
start = entity["start"]
end = entity["end"]
label = entity["entity_group"]
# Add non-entity text
if start > last_idx:
highlighted_text.append((text[last_idx:start], None))
# Add entity text
highlighted_text.append((text[start:end], label))
last_idx = end
# Add any remaining text after the last entity
if last_idx < len(text):
highlighted_text.append((text[last_idx:], None))
return highlighted_text
'''
def analyze_text(text, model_name):
ner = get_model(model_name)
results = ner(text)
# Convert to safe string output (avoids HighlightedText SSR issues)
if not results:
return "No entities found."
lines = []
for ent in results:
word = ent.get("word", "")
label = ent.get("entity_group", ent.get("entity", "UNK"))
score = ent.get("score", 0.0)
lines.append(f"{word} -> {label} ({score:.2f})")
return "\n".join(lines)
# -----------------------
# UI
# -----------------------
with gr.Blocks() as demo:
gr.Markdown("# Named Entity Recognition (NER) Demo")
model_selector = gr.Dropdown(
choices=model_names,
value=model_names[0],
label="Select Model"
)
text_input = gr.Textbox(
label="Input Text",
lines=5,
value=example_sent
)
btn = gr.Button("Run NER")
output = gr.Textbox(label="Entities")
code_output = gr.Code(label="Usage Example")
link_output = gr.Markdown()
btn.click(
analyze_text,
inputs=[text_input, model_selector],
outputs=output
)
model_selector.change(
display_model_info,
inputs=model_selector,
outputs=[code_output, link_output]
)
# Initialize UI on load
demo.load(
display_model_info,
inputs=model_selector,
outputs=[code_output, link_output]
)
#def greet(name):
# return "Hello " + name + "!!"
#demo = gr.Interface(fn=greet, inputs="text", outputs="text")
demo.launch() |