Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,10 @@
|
|
1 |
import os
|
2 |
import torch
|
3 |
import torchaudio
|
|
|
4 |
import streamlit as st
|
5 |
from huggingface_hub import login
|
6 |
-
from transformers import AutoProcessor,
|
7 |
|
8 |
# ================================
|
9 |
# 1️⃣ Authenticate with Hugging Face Hub (Securely)
|
@@ -18,9 +19,9 @@ login(token=HF_TOKEN)
|
|
18 |
# ================================
|
19 |
# 2️⃣ Load Conformer Model & Processor
|
20 |
# ================================
|
21 |
-
MODEL_NAME = "
|
22 |
processor = AutoProcessor.from_pretrained(MODEL_NAME)
|
23 |
-
model =
|
24 |
|
25 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
26 |
model.to(device)
|
@@ -47,25 +48,23 @@ if audio_file:
|
|
47 |
with open(audio_path, "wb") as f:
|
48 |
f.write(audio_file.read())
|
49 |
|
50 |
-
|
51 |
-
waveform = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)(waveform)
|
52 |
-
waveform = waveform.to(dtype=torch.float32)
|
53 |
|
54 |
# Simulate an adversarial attack by injecting random noise
|
55 |
-
|
56 |
-
|
57 |
|
58 |
-
inputs = processor(
|
59 |
-
|
60 |
-
attention_mask = inputs.attention_mask.to(device) if "attention_mask" in inputs else None
|
61 |
|
62 |
-
with torch.
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
66 |
|
67 |
if attack_strength > 0.1:
|
68 |
st.warning("⚠️ Adversarial attack detected! Transcription may be affected.")
|
69 |
|
70 |
st.success("📄 Secure Transcription:")
|
71 |
-
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 |
|
9 |
# ================================
|
10 |
# 1️⃣ Authenticate with Hugging Face Hub (Securely)
|
|
|
19 |
# ================================
|
20 |
# 2️⃣ Load Conformer Model & Processor
|
21 |
# ================================
|
22 |
+
MODEL_NAME = "deepl-project/conformer-finetunning"
|
23 |
processor = AutoProcessor.from_pretrained(MODEL_NAME)
|
24 |
+
model = AutoModelForCTC.from_pretrained(MODEL_NAME)
|
25 |
|
26 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
27 |
model.to(device)
|
|
|
48 |
with open(audio_path, "wb") as f:
|
49 |
f.write(audio_file.read())
|
50 |
|
51 |
+
speech, sr = librosa.load(audio_path, sr=16000)
|
|
|
|
|
52 |
|
53 |
# Simulate an adversarial attack by injecting random noise
|
54 |
+
adversarial_speech = speech + (attack_strength * np.random.randn(*speech.shape))
|
55 |
+
adversarial_speech = np.clip(adversarial_speech, -1.0, 1.0)
|
56 |
|
57 |
+
inputs = processor(adversarial_speech, sampling_rate=sr, return_tensors="pt", padding=True)
|
58 |
+
input_values = inputs.input_values.to(device)
|
|
|
59 |
|
60 |
+
with torch.no_grad():
|
61 |
+
logits = model(input_values).logits
|
62 |
+
|
63 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
64 |
+
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
|
65 |
|
66 |
if attack_strength > 0.1:
|
67 |
st.warning("⚠️ Adversarial attack detected! Transcription may be affected.")
|
68 |
|
69 |
st.success("📄 Secure Transcription:")
|
70 |
+
st.write(transcription[0])
|