Spaces:
Sleeping
Sleeping
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() | |