import gradio as gr from transformers import pipeline from huggingface_hub import login import os # Initialize global pipeline ner_pipeline = None # Authenticate using the secret `HFTOKEN` def authenticate_with_token(): """Authenticate with the Hugging Face API using the HFTOKEN secret.""" hf_token = os.getenv("HFTOKEN") # Retrieve the token from environment variables if not hf_token: raise ValueError("HFTOKEN is not set. Please add it to the Secrets in your Space settings.") login(token=hf_token) def load_healthcare_ner_pipeline(): """Load the Hugging Face pipeline for Healthcare NER.""" global ner_pipeline if ner_pipeline is None: # Authenticate and initialize pipeline authenticate_with_token() 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 et Youssef ont discutĂ© du projet Al Amal Ă Rabat pendant Ramadan et ont allouĂ© un budget de 50 000 dirhams pour les artisans locaux."], ["Hassan et Jean ont visitĂ© la zone industrielle de Settat avant de se rendre Ă Toulouse, Ă lâusine dâAirbus, pour discuter dâun contrat de 5 millions dâeuros."], ["L'association Noor, basĂ©e Ă Marrakech, a organisĂ© un atelier en septembre pour former des femmes sur les techniques de tissage, avec un financement de 20 000 dirhams."], ["Lâassociation Amal, basĂ©e Ă Tanger, a collaborĂ© avec lâUNICEF Ă GenĂšve pour un programme de soutien aux Ă©coles rurales, avec un budget de 1,5 million de dollars."], ["Fatima et Michael ont participĂ© Ă une rĂ©union Ă Casablanca pour le projet Green Energy, financĂ© par une banque suisse Ă hauteur de 2 millions DHS."], ["Khalid a rencontrĂ© Salma au cafĂ© Bab Al Madina Ă FĂšs pour discuter du festival Al Maghrib, qui aura lieu en octobre avec un budget de 100 000 dirhams."], ["Youssef et Sarah ont signĂ© un accord Ă Rabat avec IBM et une start-up Canadienne pour dĂ©velopper des solutions AI destinĂ©es au marchĂ© africain."], ["La sociĂ©tĂ© Zainab Artisans a ouvert une boutique Ă Casablanca en mars, oĂč elle propose des produits faits main, notamment des bijoux et des tapis pour 5 000 Ă 20 000 dirhams."], ["Lors du Forum Ă©conomique Ă Marrakech, Khalid a rencontrĂ© des reprĂ©sentants dâAmazon Ă Seattle et dâOCP Group pour un partenariat Ă©valuĂ© Ă 20 millions de dirhams."], ["L'association Rif Talents, basĂ©e Ă Al Hoceima, a annoncĂ© une bourse de 15 000 dirhams pour les jeunes artistes peintres lors du Festival du Zellige."], ["Jean-Luc et Youssef ont dĂźnĂ© au restaurant Le Kasbah Ă Chefchaouen, discutant dâune exposition sur la poterie berbĂšre prĂ©vue Ă El Hajeb."], ] ) # Launch the Gradio demo if __name__ == "__main__": demo.launch()