Update app.py
Browse files
app.py
CHANGED
|
@@ -144,7 +144,7 @@ if uploaded_file:
|
|
| 144 |
])
|
| 145 |
|
| 146 |
# 1. Diarization
|
| 147 |
-
st.write("🗣️ **Running Speaker Diarization...**")
|
| 148 |
diarization = None
|
| 149 |
try:
|
| 150 |
# Load Config Manually
|
|
@@ -155,37 +155,49 @@ if uploaded_file:
|
|
| 155 |
)
|
| 156 |
pipeline = Pipeline.from_pretrained(config_path)
|
| 157 |
|
| 158 |
-
# Use CPU for stability (avoid CUDA kernel issues on T4 for segmentation)
|
| 159 |
-
# While slower, it guarantees results.
|
| 160 |
-
st.write("⚠️ Using CPU for Diarization (Stability Mode)")
|
| 161 |
-
pipeline.to(torch.device("cpu"))
|
| 162 |
-
|
| 163 |
# Load Audio into Memory
|
| 164 |
waveform, sample_rate = torchaudio.load("temp_audio.wav")
|
| 165 |
|
| 166 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
diarization_output = pipeline({"waveform": waveform, "sample_rate": sample_rate})
|
| 168 |
|
| 169 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
if isinstance(diarization_output, tuple):
|
| 171 |
diarization = diarization_output[0]
|
| 172 |
else:
|
| 173 |
diarization = diarization_output
|
| 174 |
|
| 175 |
-
#
|
| 176 |
if not hasattr(diarization, "itertracks"):
|
| 177 |
if hasattr(diarization_output, "annotation"):
|
| 178 |
diarization = diarization_output.annotation
|
| 179 |
-
elif hasattr(diarization_output, "get"):
|
| 180 |
-
diarization = diarization_output.get("annotation", diarization_output)
|
| 181 |
|
| 182 |
except Exception as e:
|
| 183 |
st.error(f"Diarization Error: {e}")
|
| 184 |
diarization = None
|
| 185 |
|
| 186 |
# 2. Whisper Transcription
|
| 187 |
-
st.write("📝 **Transcribing with Whisper...**")
|
| 188 |
-
# Use GPU for Whisper (it works reliably there)
|
| 189 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 190 |
model = whisper.load_model("medium", device=device)
|
| 191 |
result = model.transcribe("temp_audio.wav", word_timestamps=True)
|
|
|
|
| 144 |
])
|
| 145 |
|
| 146 |
# 1. Diarization
|
| 147 |
+
st.write("🗣️ **Running Speaker Diarization (GPU Mode)...**")
|
| 148 |
diarization = None
|
| 149 |
try:
|
| 150 |
# Load Config Manually
|
|
|
|
| 155 |
)
|
| 156 |
pipeline = Pipeline.from_pretrained(config_path)
|
| 157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
# Load Audio into Memory
|
| 159 |
waveform, sample_rate = torchaudio.load("temp_audio.wav")
|
| 160 |
|
| 161 |
+
# Verify Audio is not silent
|
| 162 |
+
max_amp = waveform.abs().max().item()
|
| 163 |
+
print(f"DEBUG: Audio Loaded. Shape: {waveform.shape}, Max Amp: {max_amp}")
|
| 164 |
+
if max_amp == 0:
|
| 165 |
+
st.error("Audio is silent! Check FFmpeg conversion.")
|
| 166 |
+
|
| 167 |
+
# Move Pipeline AND Data to GPU
|
| 168 |
+
if torch.cuda.is_available():
|
| 169 |
+
device = torch.device("cuda")
|
| 170 |
+
st.write("🚀 GPU Detected & Active")
|
| 171 |
+
pipeline.to(device)
|
| 172 |
+
waveform = waveform.to(device)
|
| 173 |
+
else:
|
| 174 |
+
st.warning("⚠️ No GPU detected. Running slow on CPU.")
|
| 175 |
+
|
| 176 |
+
# Run Pipeline on Tensor
|
| 177 |
+
# Passing dictionary ensures pyannote doesn't try to reload from disk
|
| 178 |
diarization_output = pipeline({"waveform": waveform, "sample_rate": sample_rate})
|
| 179 |
|
| 180 |
+
# Debug Structure
|
| 181 |
+
print(f"DEBUG: Output Type: {type(diarization_output)}")
|
| 182 |
+
print(f"DEBUG: Output Dir: {dir(diarization_output)}")
|
| 183 |
+
|
| 184 |
+
# Robust Unwrap
|
| 185 |
if isinstance(diarization_output, tuple):
|
| 186 |
diarization = diarization_output[0]
|
| 187 |
else:
|
| 188 |
diarization = diarization_output
|
| 189 |
|
| 190 |
+
# Final Extraction Attempt
|
| 191 |
if not hasattr(diarization, "itertracks"):
|
| 192 |
if hasattr(diarization_output, "annotation"):
|
| 193 |
diarization = diarization_output.annotation
|
|
|
|
|
|
|
| 194 |
|
| 195 |
except Exception as e:
|
| 196 |
st.error(f"Diarization Error: {e}")
|
| 197 |
diarization = None
|
| 198 |
|
| 199 |
# 2. Whisper Transcription
|
| 200 |
+
st.write("📝 **Transcribing with Whisper (GPU Mode)...**")
|
|
|
|
| 201 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 202 |
model = whisper.load_model("medium", device=device)
|
| 203 |
result = model.transcribe("temp_audio.wav", word_timestamps=True)
|