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'
{highlighted}
' table_rows = "" for ent in entities: label = ent["entity_group"] style = ENTITY_STYLES.get(label, {"color": "#aaa", "emoji": "•", "label": label}) table_rows += ( f'{ent["word"]}' f'{style["emoji"]} {label} — {style["label"]}' f'{ent["score"]:.1%}' ) table_html = f'{table_rows}
EntityTypeConfidence
' return full_html, table_html with gr.Blocks(title="Named Entity Recognizer", theme=gr.themes.Soft()) as demo: gr.Markdown(""" # 🏷️ Named Entity Recognizer Paste any English text and this AI will highlight named entities. **👤 PER** — People   **🏢 ORG** — Organizations   **📍 LOC** — Locations   **🏷️ MISC** — Miscellaneous """) with gr.Row(): with gr.Column(): input_text = gr.Textbox(label="📝 Enter text", placeholder="Paste a sentence here...", lines=5) with gr.Row(): submit_btn = gr.Button("🔍 Find Entities", variant="primary", scale=3) clear_btn = gr.ClearButton([input_text], value="🗑️ Clear", scale=1) gr.Examples(examples=EXAMPLES, inputs=input_text, label="📌 Click an example to try it") highlighted_output = gr.HTML(label="📄 Highlighted Text") entity_table = gr.HTML(label="📊 Detected Entities") gr.Markdown("---\n**Model:** `samandar1105/named_entity-recognition` fine-tuned on CoNLL-2003 | F1: ~95%") submit_btn.click(fn=run_ner, inputs=[input_text], outputs=[highlighted_output, entity_table]) input_text.submit(fn=run_ner, inputs=[input_text], outputs=[highlighted_output, entity_table]) if __name__ == "__main__": demo.launch(share=False, server_port=7860)