import gradio as gr from transformers import pipeline from huggingface_hub import login import os # Initialize global pipeline ner_pipeline = None def load_healthcare_ner_pipeline(): """Load the Hugging Face pipeline for Healthcare NER.""" global ner_pipeline if ner_pipeline is None: ner_pipeline = pipeline( "token-classification", model="TypicaAI/magbert-ner", aggregation_strategy="first" # Groups B- and I- tokens into entities ) return ner_pipeline def process_text(text): """Process input text and return highlighted entities.""" pipeline = load_healthcare_ner_pipeline() entities = pipeline(text) return {"text": text, "entities": entities} def log_demo_usage(text, num_entities): """Log demo usage for analytics.""" print(f"Processed text: {text[:50]}... | Entities found: {num_entities}") # Define the main demo interface demo = gr.Interface( fn=process_text, inputs=gr.Textbox( label="Paste French text", placeholder="La Coupe du monde de football 2030 se déroulera au Maroc, en Espagne et au Portugal.", lines=5 ), outputs=gr.HighlightedText(label="Identified Entities"), title="🌟 MagBERT-NER: High-Performing French NER", description=""" _By **[Hicham Assoudi](https://huggingface.co/hassoudi)** – AI Researcher (Ph.D.), Oracle Consultant, and Author._ 🔗 [Follow me on LinkedIn](https://www.linkedin.com/in/assoudi) MagBERT-NER is a robust **Named Entity Recognition (NER)** model for the **French language**, trained on a **manually curated dataset** from diverse Moroccan sources. It’s designed to handle **names, places, currencies**, and other entities with exceptional precision, especially in **Moroccan contexts**. ## 🚀 Highlights: - **20,000+ Downloads**: Trusted by developers and researchers for French NER tasks. - **🌐 Recognized by John Snow Labs**: Adapted for scalability and enterprise-grade applications like **healthcare**. """, article=""" ## ⚠️ Disclaimer This is a **free demo model** provided without support. While it showcases high precision and is ideal for educational and exploratory purposes, it is not intended for production or commercial use. For production-grade or commercial models, please contact us at **assoudi@typica.ai**. """, examples=[ ["Fatima Zahra a acheté une babouche artisanale au souk de Fès pour 250 dirhams."], ["Youssef a participé à une réunion importante à Tétouan pendant Ramadan."], ["Amina a vendu des tapis au marché local pour 1 200 dirhams."], ["Le projet Al Massira, situé près d'Oujda, sera lancé en septembre."], ["Khadija a reçu un prix pour ses recherches sur l'arganier lors d'une cérémonie à Agadir."], ["L'association Amal prévoit une collecte de fonds à Tanger pour soutenir les artisans locaux."], ] ) # Launch the Gradio demo if __name__ == "__main__": demo.launch()