File size: 3,151 Bytes
a8f3b12
2e278b7
 
 
 
818a740
a8f3b12
2e278b7
a8f3b12
2e278b7
 
 
 
 
 
 
a8f3b12
 
 
2e278b7
818a740
 
 
2e278b7
818a740
 
2e278b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
818a740
2e278b7
818a740
 
 
2e278b7
 
 
a8f3b12
2e278b7
1221d0a
 
 
 
a8f3b12
 
2e278b7
 
a8f3b12
1221d0a
 
2e278b7
a8f3b12
 
1221d0a
2e278b7
a8f3b12
 
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
import gradio as gr
import os
import io
import json
from pydub import AudioSegment
from presidio_analyzer import AnalyzerEngine, PatternRecognizer, Pattern
from presidio_anonymizer import AnonymizerEngine
from google.cloud import speech

# βœ… Step 1: Set Google Cloud Credentials (File Uploaded in Hugging Face)
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "service-key.json"

# βœ… Step 2: Initialize Google Speech-to-Text API Client
client = speech.SpeechClient()

# βœ… Step 3: Initialize Presidio Analyzer & Anonymizer
analyzer = AnalyzerEngine()
anonymizer = AnonymizerEngine()

# βœ… Step 4: Define a Custom NHS Number Recognizer (UK Format)
nhs_pattern = Pattern(name="nhs_number_pattern", regex=r"\b\d{3}[- ]?\d{3}[- ]?\d{4}\b", score=0.85)
nhs_recognizer = PatternRecognizer(supported_entity="NHS_NUMBER", patterns=[nhs_pattern])

# Add NHS Recognizer to Presidio
analyzer.registry.add_recognizer(nhs_recognizer)

def transcribe_audio(audio_file):
    """
    Converts uploaded audio to text using Google Cloud Speech-to-Text.
    """
    # Convert audio to FLAC format for Google API compatibility
    audio = AudioSegment.from_file(audio_file)
    audio = audio.set_channels(1).set_frame_rate(16000)  # Ensure proper format
    
    # Save as FLAC in memory
    buffer = io.BytesIO()
    audio.export(buffer, format="flac")
    buffer.seek(0)

    # Send audio for transcription
    audio_recognition = speech.RecognitionAudio(content=buffer.read())
    config = speech.RecognitionConfig(language_code="en-GB")

    response = client.recognize(config=config, audio=audio_recognition)

    # Get transcribed text
    transcribed_text = " ".join([result.alternatives[0].transcript for result in response.results])
    
    return transcribed_text if transcribed_text else "No speech detected."

def redact_pii(audio_file):
    """
    1. Transcribes the audio using Google Speech-to-Text.
    2. Uses Presidio to redact sensitive PII from the transcript.
    """
    transcribed_text = transcribe_audio(audio_file)

    # Analyze and redact PII
    results = analyzer.analyze(
        text=transcribed_text,
        entities=["PERSON", "PHONE_NUMBER", "DATE_TIME", "LOCATION", "ID_NUMBER", "NHS_NUMBER"],
        language="en"
    )
    anonymized_text = anonymizer.anonymize(text=transcribed_text, analyzer_results=results)

    return transcribed_text, anonymized_text.text

# βœ… Step 5: Gradio UI for Audio Upload & Redaction
sample_transcript = """Example script for the demo:
'Hello, my name is Sarah Johnson. My NHS number is 123-456-7890, and I have an appointment with Dr. Smith on March 15th.
Please update my contact number to 07911 123456.'"""

iface = gr.Interface(
    fn=redact_pii,
    inputs=gr.Audio(type="filepath"),
    outputs=["text", "text"],
    title="Healthcare Call Redaction Demo",
    description=f"Upload a healthcare call recording, and Presidio will transcribe and anonymize sensitive data.\n\n"
                f"πŸ”Š **Sample Script for Testing:**\n{sample_transcript}",
    examples=["sample_healthcare_call.wav"]
)


# βœ… Step 6: Launch the Gradio Web App
if __name__ == "__main__":
    iface.launch()