import gradio as gr from transformers import pipeline import torch MODEL_ID = "samandar1105/named_entity-recognition" ENTITY_STYLES = { "PER": {"color": "#FF6B6B", "emoji": "👤", "label": "Person"}, "ORG": {"color": "#4ECDC4", "emoji": "🏢", "label": "Organization"}, "LOC": {"color": "#45B7D1", "emoji": "📍", "label": "Location"}, "MISC": {"color": "#FFA07A", "emoji": "🏷️", "label": "Miscellaneous"}, } EXAMPLES = [ ["Elon Musk, CEO of Tesla and SpaceX, met with Emmanuel Macron at the Élysée Palace in Paris."], ["The United Nations Security Council held an emergency meeting at its headquarters in New York City."], ["Apple announced a new partnership with Samsung to develop chips in South Korea and Taiwan."], ["Cristiano Ronaldo left Manchester United to join Al Nassr, a club based in Riyadh, Saudi Arabia."], ["NASA's Artemis program, led by Bill Nelson, plans to land astronauts on the Moon near the South Pole."], ] print(f"Loading model: {MODEL_ID}") ner_pipeline = pipeline( "token-classification", model=MODEL_ID, aggregation_strategy="simple", device=0 if torch.cuda.is_available() else -1, ) print("Model loaded!") def run_ner(text: str): if not text or text.strip() == "": return "
Please enter some text above.
", "" if len(text.strip()) < 5: return "Text too short. Please enter a full sentence.
", "" entities = ner_pipeline(text) if not entities: return f"{text}
No named entities detected.
", "" highlighted = "" last_end = 0 for ent in entities: start, end, label, score = ent["start"], ent["end"], ent["entity_group"], ent["score"] style = ENTITY_STYLES.get(label, {"color": "#aaa", "emoji": "•", "label": label}) highlighted += text[last_end:start] highlighted += ( f'' f'{text[start:end]}' f'' f'{style["emoji"]} {label}' ) last_end = end highlighted += text[last_end:] full_html = f'| Entity | Type | Confidence |
|---|