|
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 |
|
|
|
|
|
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "service-key.json" |
|
|
|
|
|
client = speech.SpeechClient() |
|
|
|
|
|
analyzer = AnalyzerEngine() |
|
anonymizer = AnonymizerEngine() |
|
|
|
|
|
|
|
|
|
credit_card_pattern = Pattern(name="credit_card_pattern", regex=r"\b(?:\d[ -]*?){13,16}\b", score=0.90) |
|
credit_card_recognizer = PatternRecognizer(supported_entity="CREDIT_CARD", patterns=[credit_card_pattern]) |
|
|
|
|
|
account_number_pattern = Pattern(name="account_number_pattern", regex=r"\b\d{6,12}\b", score=0.85) |
|
account_number_recognizer = PatternRecognizer(supported_entity="ACCOUNT_NUMBER", patterns=[account_number_pattern]) |
|
|
|
|
|
ssn_pattern = Pattern(name="ssn_pattern", regex=r"\b\d{3}-\d{2}-\d{4}\b", score=0.90) |
|
ssn_recognizer = PatternRecognizer(supported_entity="SSN", patterns=[ssn_pattern]) |
|
|
|
|
|
bank_pattern = Pattern(name="bank_pattern", regex=r"\b(Barclays|HSBC|Lloyds|NatWest|Santander|Metro Bank|Revolut)\b", score=0.85) |
|
bank_recognizer = PatternRecognizer(supported_entity="BANK", patterns=[bank_pattern]) |
|
|
|
|
|
amount_pattern = Pattern(name="amount_pattern", regex=r"\b[$Β£β¬]?\d{1,3}(?:,?\d{3})*(?:\.\d{2})?\b", score=0.85) |
|
amount_recognizer = PatternRecognizer(supported_entity="AMOUNT", patterns=[amount_pattern]) |
|
|
|
|
|
analyzer.registry.add_recognizer(credit_card_recognizer) |
|
analyzer.registry.add_recognizer(account_number_recognizer) |
|
analyzer.registry.add_recognizer(ssn_recognizer) |
|
analyzer.registry.add_recognizer(bank_recognizer) |
|
analyzer.registry.add_recognizer(amount_recognizer) |
|
|
|
def transcribe_audio(audio_file): |
|
""" |
|
Converts uploaded audio to text using Google Cloud Speech-to-Text. |
|
""" |
|
|
|
audio = AudioSegment.from_file(audio_file) |
|
audio = audio.set_channels(1).set_frame_rate(16000) |
|
|
|
|
|
buffer = io.BytesIO() |
|
audio.export(buffer, format="flac") |
|
buffer.seek(0) |
|
|
|
|
|
audio_recognition = speech.RecognitionAudio(content=buffer.read()) |
|
config = speech.RecognitionConfig(language_code="en-GB") |
|
|
|
response = client.recognize(config=config, audio=audio_recognition) |
|
|
|
|
|
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 financial PII from the transcript. |
|
""" |
|
transcribed_text = transcribe_audio(audio_file) |
|
|
|
|
|
results = analyzer.analyze( |
|
text=transcribed_text, |
|
entities=["PERSON", "PHONE_NUMBER", "DATE_TIME", "LOCATION", "CREDIT_CARD", "SSN", "ACCOUNT_NUMBER", "BANK", "AMOUNT"], |
|
language="en" |
|
) |
|
anonymized_text = anonymizer.anonymize(text=transcribed_text, analyzer_results=results) |
|
|
|
return transcribed_text, anonymized_text.text |
|
|
|
|
|
sample_transcript = """Example script for the demo: |
|
'Hello, my name is John Doe. My credit card number is 4111-1111-1111-1111, and I bank with HSBC. |
|
Iβd like to transfer $5,000 to my account ending in 123456.'""" |
|
|
|
iface = gr.Interface( |
|
fn=redact_pii, |
|
inputs=gr.Audio(type="filepath"), |
|
outputs=["text", "text"], |
|
title="Financial Call Redaction Demo", |
|
description=f"Upload a financial call recording, and Presidio will transcribe and anonymize sensitive financial data.\n\n" |
|
f"π **Sample Script for Testing:**\n{sample_transcript}", |
|
examples=["sample_financial_call.wav"] |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|