Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,10 @@
|
|
1 |
import os
|
2 |
-
import tarfile
|
3 |
import torch
|
4 |
import torchaudio
|
5 |
-
import
|
6 |
import streamlit as st
|
7 |
from huggingface_hub import login
|
8 |
-
from transformers import
|
9 |
-
AutoProcessor,
|
10 |
-
AutoModelForSpeechSeq2Seq,
|
11 |
-
)
|
12 |
from cryptography.fernet import Fernet
|
13 |
|
14 |
# ================================
|
@@ -24,52 +20,19 @@ def authenticate_hf():
|
|
24 |
authenticate_hf()
|
25 |
|
26 |
# ================================
|
27 |
-
# 2οΈβ£ Load Model & Processor (Cached)
|
28 |
# ================================
|
29 |
@st.cache_resource
|
30 |
def load_model():
|
31 |
-
MODEL_NAME = "
|
32 |
processor = AutoProcessor.from_pretrained(MODEL_NAME)
|
33 |
-
model =
|
34 |
return processor, model
|
35 |
|
36 |
processor, model = load_model()
|
37 |
|
38 |
# ================================
|
39 |
-
# 3οΈβ£
|
40 |
-
# ================================
|
41 |
-
@st.cache_resource
|
42 |
-
def extract_dataset():
|
43 |
-
DATASET_TAR_PATH = "dev-clean.tar.gz"
|
44 |
-
EXTRACT_PATH = "./librispeech_dev_clean"
|
45 |
-
|
46 |
-
if not os.path.exists(EXTRACT_PATH):
|
47 |
-
with tarfile.open(DATASET_TAR_PATH, "r:gz") as tar:
|
48 |
-
tar.extractall(EXTRACT_PATH)
|
49 |
-
return os.path.join(EXTRACT_PATH, "LibriSpeech", "dev-clean")
|
50 |
-
|
51 |
-
AUDIO_FOLDER = extract_dataset()
|
52 |
-
|
53 |
-
# ================================
|
54 |
-
# 4οΈβ£ Load Transcripts (Cached)
|
55 |
-
# ================================
|
56 |
-
@st.cache_resource
|
57 |
-
def load_transcripts():
|
58 |
-
transcripts = {}
|
59 |
-
for root, _, files in os.walk(AUDIO_FOLDER):
|
60 |
-
for file in files:
|
61 |
-
if file.endswith(".txt"):
|
62 |
-
with open(os.path.join(root, file), "r", encoding="utf-8") as f:
|
63 |
-
for line in f:
|
64 |
-
parts = line.strip().split(" ", 1)
|
65 |
-
if len(parts) == 2:
|
66 |
-
transcripts[parts[0]] = parts[1]
|
67 |
-
return transcripts
|
68 |
-
|
69 |
-
transcripts = load_transcripts()
|
70 |
-
|
71 |
-
# ================================
|
72 |
-
# 5οΈβ£ Streamlit Sidebar for Fine-Tuning & Security
|
73 |
# ================================
|
74 |
st.sidebar.title("π§ Fine-Tuning & Security Settings")
|
75 |
|
@@ -83,7 +46,7 @@ enable_encryption = st.sidebar.checkbox("π Encrypt Transcription", value=True
|
|
83 |
show_transcription = st.sidebar.checkbox("π Show Transcription", value=False)
|
84 |
|
85 |
# ================================
|
86 |
-
#
|
87 |
# ================================
|
88 |
encryption_key = Fernet.generate_key()
|
89 |
fernet = Fernet(encryption_key)
|
@@ -95,9 +58,9 @@ def decrypt_text(encrypted_text):
|
|
95 |
return fernet.decrypt(encrypted_text).decode()
|
96 |
|
97 |
# ================================
|
98 |
-
#
|
99 |
# ================================
|
100 |
-
st.title("ποΈ Speech-to-Text ASR Model
|
101 |
|
102 |
audio_file = st.file_uploader("Upload an audio file", type=["wav", "mp3", "flac"])
|
103 |
|
@@ -106,27 +69,29 @@ if audio_file:
|
|
106 |
with open(audio_path, "wb") as f:
|
107 |
f.write(audio_file.read())
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
# ================================
|
113 |
# β
Optimized Adversarial Attack Handling
|
114 |
# ================================
|
115 |
-
noise = attack_strength * torch.randn_like(
|
116 |
-
adversarial_waveform =
|
117 |
adversarial_waveform = torch.clamp(adversarial_waveform, -1.0, 1.0)
|
118 |
|
119 |
# Remove background noise for speed & accuracy
|
120 |
denoised_waveform = torchaudio.functional.vad(adversarial_waveform, sample_rate=16000)
|
121 |
|
122 |
# ================================
|
123 |
-
# β
Fast Transcription Processing
|
124 |
# ================================
|
125 |
-
|
|
|
|
|
|
|
126 |
|
127 |
-
|
128 |
-
|
129 |
-
transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
130 |
|
131 |
if attack_strength > 0.3:
|
132 |
st.warning("β οΈ Adversarial attack detected! Denoising applied.")
|
@@ -135,7 +100,7 @@ if audio_file:
|
|
135 |
# β
Optimized Encryption Handling
|
136 |
# ================================
|
137 |
if enable_encryption:
|
138 |
-
encrypted_transcription = encrypt_text(transcription)
|
139 |
st.info("π Transcription is encrypted. Enable 'Show Transcription' to view.")
|
140 |
|
141 |
if show_transcription:
|
@@ -146,4 +111,4 @@ if audio_file:
|
|
146 |
st.write("π [Encrypted] Transcription hidden. Enable 'Show Transcription' to view.")
|
147 |
else:
|
148 |
st.success("π Transcription:")
|
149 |
-
st.write(transcription)
|
|
|
1 |
import os
|
|
|
2 |
import torch
|
3 |
import torchaudio
|
4 |
+
import librosa
|
5 |
import streamlit as st
|
6 |
from huggingface_hub import login
|
7 |
+
from transformers import AutoProcessor, AutoModelForCTC
|
|
|
|
|
|
|
8 |
from cryptography.fernet import Fernet
|
9 |
|
10 |
# ================================
|
|
|
20 |
authenticate_hf()
|
21 |
|
22 |
# ================================
|
23 |
+
# 2οΈβ£ Load Conformer Model & Processor (Cached)
|
24 |
# ================================
|
25 |
@st.cache_resource
|
26 |
def load_model():
|
27 |
+
MODEL_NAME = "deepl-project/conformer-finetunning"
|
28 |
processor = AutoProcessor.from_pretrained(MODEL_NAME)
|
29 |
+
model = AutoModelForCTC.from_pretrained(MODEL_NAME).to("cuda" if torch.cuda.is_available() else "cpu")
|
30 |
return processor, model
|
31 |
|
32 |
processor, model = load_model()
|
33 |
|
34 |
# ================================
|
35 |
+
# 3οΈβ£ Streamlit Sidebar for Fine-Tuning & Security
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
# ================================
|
37 |
st.sidebar.title("π§ Fine-Tuning & Security Settings")
|
38 |
|
|
|
46 |
show_transcription = st.sidebar.checkbox("π Show Transcription", value=False)
|
47 |
|
48 |
# ================================
|
49 |
+
# 4οΈβ£ Encryption Handling (Precomputed Key)
|
50 |
# ================================
|
51 |
encryption_key = Fernet.generate_key()
|
52 |
fernet = Fernet(encryption_key)
|
|
|
58 |
return fernet.decrypt(encrypted_text).decode()
|
59 |
|
60 |
# ================================
|
61 |
+
# 5οΈβ£ Optimized ASR Web App
|
62 |
# ================================
|
63 |
+
st.title("ποΈ Speech-to-Text ASR Model using Conformer with Security Features")
|
64 |
|
65 |
audio_file = st.file_uploader("Upload an audio file", type=["wav", "mp3", "flac"])
|
66 |
|
|
|
69 |
with open(audio_path, "wb") as f:
|
70 |
f.write(audio_file.read())
|
71 |
|
72 |
+
# Load and preprocess the audio file using librosa
|
73 |
+
speech, sr = librosa.load(audio_path, sr=16000)
|
74 |
+
|
75 |
# ================================
|
76 |
# β
Optimized Adversarial Attack Handling
|
77 |
# ================================
|
78 |
+
noise = attack_strength * torch.randn_like(torch.tensor(speech))
|
79 |
+
adversarial_waveform = torch.tensor(speech) + noise
|
80 |
adversarial_waveform = torch.clamp(adversarial_waveform, -1.0, 1.0)
|
81 |
|
82 |
# Remove background noise for speed & accuracy
|
83 |
denoised_waveform = torchaudio.functional.vad(adversarial_waveform, sample_rate=16000)
|
84 |
|
85 |
# ================================
|
86 |
+
# β
Fast Transcription Processing with Conformer
|
87 |
# ================================
|
88 |
+
inputs = processor(denoised_waveform.numpy(), sampling_rate=sr, return_tensors="pt", padding=True).to("cuda" if torch.cuda.is_available() else "cpu")
|
89 |
+
|
90 |
+
with torch.no_grad():
|
91 |
+
logits = model(**inputs).logits
|
92 |
|
93 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
94 |
+
transcription = processor.batch_decode(predicted_ids)
|
|
|
95 |
|
96 |
if attack_strength > 0.3:
|
97 |
st.warning("β οΈ Adversarial attack detected! Denoising applied.")
|
|
|
100 |
# β
Optimized Encryption Handling
|
101 |
# ================================
|
102 |
if enable_encryption:
|
103 |
+
encrypted_transcription = encrypt_text(transcription[0])
|
104 |
st.info("π Transcription is encrypted. Enable 'Show Transcription' to view.")
|
105 |
|
106 |
if show_transcription:
|
|
|
111 |
st.write("π [Encrypted] Transcription hidden. Enable 'Show Transcription' to view.")
|
112 |
else:
|
113 |
st.success("π Transcription:")
|
114 |
+
st.write(transcription[0])
|