Update app.py
Browse files
app.py
CHANGED
|
@@ -8,16 +8,18 @@ import torch
|
|
| 8 |
import torchaudio
|
| 9 |
from datetime import timedelta
|
| 10 |
from pyannote.audio import Pipeline
|
| 11 |
-
from huggingface_hub import login
|
| 12 |
from pydub import AudioSegment
|
| 13 |
|
| 14 |
# --- Configuration & Tokens ---
|
|
|
|
| 15 |
HARDCODED_HF_TOKEN = "PASTE_YOUR_HF_TOKEN_HERE"
|
| 16 |
HARDCODED_GEMINI_KEY = ""
|
| 17 |
|
| 18 |
ENV_HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 19 |
ENV_GEMINI_KEY = os.environ.get("GEMINI_API_KEY", "")
|
| 20 |
|
|
|
|
| 21 |
ACTIVE_HF_TOKEN = ENV_HF_TOKEN if ENV_HF_TOKEN else HARDCODED_HF_TOKEN
|
| 22 |
ACTIVE_GEMINI_KEY = ENV_GEMINI_KEY if ENV_GEMINI_KEY else HARDCODED_GEMINI_KEY
|
| 23 |
|
|
@@ -102,18 +104,20 @@ with st.sidebar:
|
|
| 102 |
uploaded_file = st.file_uploader("Upload Video/Audio Clip", type=["mp4", "m4a", "wav", "mp3", "mov"])
|
| 103 |
|
| 104 |
if uploaded_file:
|
|
|
|
| 105 |
if "transcript" not in st.session_state:
|
| 106 |
if st.button("Step 1: Transcribe & Diarize"):
|
| 107 |
if not ACTIVE_HF_TOKEN or "PASTE_YOUR_HF_TOKEN" in ACTIVE_HF_TOKEN:
|
| 108 |
st.error("Please provide a valid Hugging Face Token.")
|
| 109 |
else:
|
| 110 |
-
with st.spinner("Processing..."):
|
|
|
|
| 111 |
with open("temp_input", "wb") as f:
|
| 112 |
f.write(uploaded_file.getbuffer())
|
| 113 |
|
| 114 |
st.write("🎵 **Preprocessing Audio...**")
|
| 115 |
try:
|
| 116 |
-
# Use PyDub
|
| 117 |
audio = AudioSegment.from_file("temp_input")
|
| 118 |
audio = audio.set_channels(1)
|
| 119 |
audio = audio.set_frame_rate(16000)
|
|
@@ -126,16 +130,22 @@ if uploaded_file:
|
|
| 126 |
st.write("🗣️ **Running Speaker Diarization...**")
|
| 127 |
diarization = None
|
| 128 |
try:
|
| 129 |
-
#
|
| 130 |
login(token=ACTIVE_HF_TOKEN)
|
| 131 |
|
| 132 |
-
#
|
| 133 |
-
#
|
| 134 |
-
|
| 135 |
-
"pyannote/speaker-diarization
|
| 136 |
-
|
|
|
|
|
|
|
| 137 |
)
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
if torch.cuda.is_available():
|
| 140 |
st.write("🚀 Using GPU for Diarization")
|
| 141 |
pipeline.to(torch.device("cuda"))
|
|
@@ -159,8 +169,8 @@ if uploaded_file:
|
|
| 159 |
speaker_turns = []
|
| 160 |
|
| 161 |
if diarization:
|
| 162 |
-
# 2.1.1 returns a proper Annotation object directly
|
| 163 |
try:
|
|
|
|
| 164 |
for turn, _, speaker_id in diarization.itertracks(yield_label=True):
|
| 165 |
speaker_turns.append({"start": turn.start, "end": turn.end, "speaker": speaker_id})
|
| 166 |
|
|
@@ -169,7 +179,7 @@ if uploaded_file:
|
|
| 169 |
else:
|
| 170 |
st.warning("⚠️ Pipeline ran but returned no tracks.")
|
| 171 |
except AttributeError:
|
| 172 |
-
st.error("Could not iterate tracks.")
|
| 173 |
|
| 174 |
for segment in result['segments']:
|
| 175 |
mid_time = (segment['start'] + segment['end']) / 2
|
|
|
|
| 8 |
import torchaudio
|
| 9 |
from datetime import timedelta
|
| 10 |
from pyannote.audio import Pipeline
|
| 11 |
+
from huggingface_hub import login, hf_hub_download
|
| 12 |
from pydub import AudioSegment
|
| 13 |
|
| 14 |
# --- Configuration & Tokens ---
|
| 15 |
+
# Hardcode tokens here if you want to avoid UI input
|
| 16 |
HARDCODED_HF_TOKEN = "PASTE_YOUR_HF_TOKEN_HERE"
|
| 17 |
HARDCODED_GEMINI_KEY = ""
|
| 18 |
|
| 19 |
ENV_HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 20 |
ENV_GEMINI_KEY = os.environ.get("GEMINI_API_KEY", "")
|
| 21 |
|
| 22 |
+
# Determine active keys
|
| 23 |
ACTIVE_HF_TOKEN = ENV_HF_TOKEN if ENV_HF_TOKEN else HARDCODED_HF_TOKEN
|
| 24 |
ACTIVE_GEMINI_KEY = ENV_GEMINI_KEY if ENV_GEMINI_KEY else HARDCODED_GEMINI_KEY
|
| 25 |
|
|
|
|
| 104 |
uploaded_file = st.file_uploader("Upload Video/Audio Clip", type=["mp4", "m4a", "wav", "mp3", "mov"])
|
| 105 |
|
| 106 |
if uploaded_file:
|
| 107 |
+
# --- Step 1: Technical Processing ---
|
| 108 |
if "transcript" not in st.session_state:
|
| 109 |
if st.button("Step 1: Transcribe & Diarize"):
|
| 110 |
if not ACTIVE_HF_TOKEN or "PASTE_YOUR_HF_TOKEN" in ACTIVE_HF_TOKEN:
|
| 111 |
st.error("Please provide a valid Hugging Face Token.")
|
| 112 |
else:
|
| 113 |
+
with st.spinner("Processing... This may take a moment."):
|
| 114 |
+
# Save local temp file
|
| 115 |
with open("temp_input", "wb") as f:
|
| 116 |
f.write(uploaded_file.getbuffer())
|
| 117 |
|
| 118 |
st.write("🎵 **Preprocessing Audio...**")
|
| 119 |
try:
|
| 120 |
+
# Use PyDub to convert to WAV (Mono, 16kHz)
|
| 121 |
audio = AudioSegment.from_file("temp_input")
|
| 122 |
audio = audio.set_channels(1)
|
| 123 |
audio = audio.set_frame_rate(16000)
|
|
|
|
| 130 |
st.write("🗣️ **Running Speaker Diarization...**")
|
| 131 |
diarization = None
|
| 132 |
try:
|
| 133 |
+
# Log in globally
|
| 134 |
login(token=ACTIVE_HF_TOKEN)
|
| 135 |
|
| 136 |
+
# Use manual config download with explicit 'token' argument
|
| 137 |
+
# This fixes the hf_hub_download error
|
| 138 |
+
config_path = hf_hub_download(
|
| 139 |
+
repo_id="pyannote/speaker-diarization",
|
| 140 |
+
revision="2.1",
|
| 141 |
+
filename="config.yaml",
|
| 142 |
+
token=ACTIVE_HF_TOKEN
|
| 143 |
)
|
| 144 |
|
| 145 |
+
# Load pipeline from the manually downloaded config
|
| 146 |
+
# We do NOT pass a token here because the config file is local
|
| 147 |
+
pipeline = Pipeline.from_pretrained(config_path)
|
| 148 |
+
|
| 149 |
if torch.cuda.is_available():
|
| 150 |
st.write("🚀 Using GPU for Diarization")
|
| 151 |
pipeline.to(torch.device("cuda"))
|
|
|
|
| 169 |
speaker_turns = []
|
| 170 |
|
| 171 |
if diarization:
|
|
|
|
| 172 |
try:
|
| 173 |
+
# 2.1.1 returns a proper Annotation object directly
|
| 174 |
for turn, _, speaker_id in diarization.itertracks(yield_label=True):
|
| 175 |
speaker_turns.append({"start": turn.start, "end": turn.end, "speaker": speaker_id})
|
| 176 |
|
|
|
|
| 179 |
else:
|
| 180 |
st.warning("⚠️ Pipeline ran but returned no tracks.")
|
| 181 |
except AttributeError:
|
| 182 |
+
st.error("Could not iterate tracks. Output object format mismatch.")
|
| 183 |
|
| 184 |
for segment in result['segments']:
|
| 185 |
mid_time = (segment['start'] + segment['end']) / 2
|