Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from huggingface_hub import login
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Initialize global pipeline
|
7 |
+
ner_pipeline = None
|
8 |
+
|
9 |
+
def load_healthcare_ner_pipeline():
|
10 |
+
"""Load the Hugging Face pipeline for Healthcare NER."""
|
11 |
+
global ner_pipeline
|
12 |
+
if ner_pipeline is None:
|
13 |
+
|
14 |
+
ner_pipeline = pipeline(
|
15 |
+
"token-classification",
|
16 |
+
model="TypicaAI/magbert-ner",
|
17 |
+
aggregation_strategy="first" # Groups B- and I- tokens into entities
|
18 |
+
)
|
19 |
+
return ner_pipeline
|
20 |
+
|
21 |
+
|
22 |
+
def process_text(text):
|
23 |
+
"""Process input text and return highlighted entities."""
|
24 |
+
pipeline = load_healthcare_ner_pipeline()
|
25 |
+
entities = pipeline(text)
|
26 |
+
return {"text": text, "entities": entities}
|
27 |
+
|
28 |
+
|
29 |
+
def log_demo_usage(text, num_entities):
|
30 |
+
"""Log demo usage for analytics."""
|
31 |
+
print(f"Processed text: {text[:50]}... | Entities found: {num_entities}")
|
32 |
+
|
33 |
+
# Define the main demo interface
|
34 |
+
demo = gr.Interface(
|
35 |
+
fn=process_text,
|
36 |
+
inputs=gr.Textbox(
|
37 |
+
label="Paste French text",
|
38 |
+
placeholder="La Coupe du monde de football 2030 se déroulera au Maroc, en Espagne et au Portugal.",
|
39 |
+
lines=5
|
40 |
+
),
|
41 |
+
outputs=gr.HighlightedText(label="Identified Entities"),
|
42 |
+
title="🌟 MagBERT-NER: High-Performing French NER",
|
43 |
+
description="""
|
44 |
+
_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)
|
45 |
+
|
46 |
+
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**.
|
47 |
+
## 🚀 Highlights:
|
48 |
+
- **20,000+ Downloads**: Trusted by developers and researchers for French NER tasks.
|
49 |
+
- **🌐 Recognized by John Snow Labs**: Adapted for scalability and enterprise-grade applications like **healthcare**.
|
50 |
+
""",
|
51 |
+
article="""
|
52 |
+
## ⚠️ Disclaimer
|
53 |
+
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.
|
54 |
+
|
55 |
+
For production-grade or commercial models, please contact us at **[email protected]**.
|
56 |
+
""",
|
57 |
+
examples=[
|
58 |
+
["Fatima Zahra a acheté une babouche artisanale au souk de Fès pour 250 dirhams."],
|
59 |
+
["Youssef a participé à une réunion importante à Tétouan pendant Ramadan."],
|
60 |
+
["Amina a vendu des tapis au marché local pour 1 200 dirhams."],
|
61 |
+
["Le projet Al Massira, situé près d'Oujda, sera lancé en septembre."],
|
62 |
+
["Khadija a reçu un prix pour ses recherches sur l'arganier lors d'une cérémonie à Agadir."],
|
63 |
+
["L'association Amal prévoit une collecte de fonds à Tanger pour soutenir les artisans locaux."], ]
|
64 |
+
)
|
65 |
+
|
66 |
+
# Launch the Gradio demo
|
67 |
+
if __name__ == "__main__":
|
68 |
+
demo.launch()
|