File size: 4,610 Bytes
a350303
a4f3cd1
9e0b404
f33c0ca
b9cfeca
a4f3cd1
 
a350303
93fdf42
 
 
 
 
 
 
 
a4f3cd1
 
 
93fdf42
9cd5bcf
 
 
 
a4f3cd1
 
 
93fdf42
b9cfeca
a4f3cd1
f33c0ca
f020d40
f33c0ca
b9cfeca
a4f3cd1
 
0726629
 
b9cfeca
 
 
 
 
93fdf42
c87d13e
 
 
 
 
 
 
75d26cc
c87d13e
 
 
75d26cc
bb39b50
0c16af8
 
dfbea41
c87d13e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c42d5a
e4219fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f33c0ca
e4219fa
 
 
f33c0ca
e4219fa
b9cfeca
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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/HealthcareNER-Fr",
            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 medical text",
        placeholder="Le patient présente une hypertension artérielle...",
        lines=5
    ),
    outputs=gr.HighlightedText(label="Identified Medical Entities"),
    #outputs=gr.HTML(label="Identified Medical Entities"),
    title="French Healthcare NER Demo",
    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)

    🔬 **Try the French Healthcare NER model**, developed as part of the healthcare NLP case study from the book *[Natural Language Processing on Oracle Cloud Infrastructure: Building Transformer-Based NLP Solutions Using Oracle AI and Hugging Face](https://a.co/d/h0xL4lo).

    This Space demonstrates a Healthcare NER model developed through the step-by-step process detailed in 📖 Chapters 4 to 7 of the book. It covers healthcare dataset preparation and fine-tuning a transformer-based NER model, offering a practical example of how NLP can extract valuable insights from 🏥 French medical texts, such as identifying conditions, treatments, and more.
    """,
    article="""
    ### **Disclaimer**
    This is a **demo model** provided for educational purposes. It was trained on a limited dataset and is not intended for production use, clinical decision-making, or real-world medical applications.
    """,
    examples=[
        ["Le medecin donne des antibiotiques en cas d'infections des voies respiratoires e.g. pneumonie."],
        ["Dans le cas de l'asthme, le médecin peut recommander des corticoïdes pour réduire l'inflammation dans les poumons."],
        ["Pour soulager les symptômes d'allergie, le médecin prescrit des antihistaminiques."],
        ["Si le patient souffre de diabète de type 2, le médecin peut prescrire une insulinothérapie par exemple: Metformine 500mg."],
        ["Après une blessure musculaire ou une maladies douloureuses des tendons comme une tendinopathie, le patient pourrait suivre une kinésithérapie ou une physiothérapie."],
        ["En cas d'infection bactérienne, le médecin recommande une antibiothérapie."],
        ["Antécédents: infarctus du myocarde en 2019. Allergie à la pénicilline."]
    ]
)


# Add marketing elements
with gr.Blocks() as marketing_elements:
    gr.Markdown("""
    ### 📖 Get the Complete Guide
    
    Learn how to build and deploy this exact model in 'Natural Language Processing on Oracle Cloud Infrastructure: Building Transformer-Based NLP Solutions Using Oracle AI and Hugging Face Kindle Edition'
    - ✓ Step-by-step implementation
    - ✓ Performance optimization
    - ✓ Enterprise deployment patterns
    - ✓ Complete source code
    
    [Get the Book](https://a.co/d/eg7my5G)
    """)
    
    with gr.Row():
        email_input = gr.Textbox(
            label="Get the French Healthcare NER Dataset",
            placeholder="Enter your business email"
        )
        submit_btn = gr.Button("Access Dataset")

# Launch the Gradio demo
if __name__ == "__main__":
    demo.launch()