Spaces:
Build error
Build error
updated app.py
Browse files
app.py
CHANGED
@@ -1,88 +1,23 @@
|
|
1 |
-
import torch
|
2 |
-
import pickle
|
3 |
-
import whisper
|
4 |
import streamlit as st
|
5 |
-
import
|
6 |
-
import numpy as np
|
7 |
-
|
8 |
from io import BytesIO
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
SAMPLING_RATE = 16000
|
16 |
-
CHUNK_LENGTH_S = 20 # 30 seconds per chunk
|
17 |
|
18 |
-
# Load
|
19 |
-
|
20 |
-
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small").to(device)
|
21 |
|
22 |
# Title of the app
|
23 |
-
st.title("Audio Player with Live Transcription")
|
24 |
-
|
25 |
-
# Sidebar for file uploader and submit button
|
26 |
-
st.sidebar.header("Upload Audio Files")
|
27 |
-
uploaded_files = st.sidebar.file_uploader("Choose audio files", type=["mp3", "wav"], accept_multiple_files=True)
|
28 |
-
submit_button = st.sidebar.button("Submit")
|
29 |
-
|
30 |
-
# Session state to hold data
|
31 |
-
if 'audio_files' not in st.session_state:
|
32 |
-
st.session_state.audio_files = []
|
33 |
-
st.session_state.transcriptions = {}
|
34 |
-
st.session_state.translations = {}
|
35 |
-
st.session_state.detected_languages = []
|
36 |
-
st.session_state.waveforms = []
|
37 |
-
|
38 |
-
|
39 |
-
def detect_language(audio_file):
|
40 |
-
whisper_model = whisper.load_model("small")
|
41 |
-
trimmed_audio = whisper.pad_or_trim(audio_file.squeeze())
|
42 |
-
mel = whisper.log_mel_spectrogram(trimmed_audio).to(whisper_model.device)
|
43 |
-
_, probs = whisper_model.detect_language(mel)
|
44 |
-
detected_lang = max(probs[0], key=probs[0].get)
|
45 |
-
print(f"Detected language: {detected_lang}")
|
46 |
-
return detected_lang
|
47 |
|
|
|
48 |
|
49 |
-
def
|
50 |
-
|
51 |
-
|
52 |
-
chunks = [waveform[:, i:i + chunk_length] for i in range(0, input_length, chunk_length)]
|
53 |
-
|
54 |
-
results = []
|
55 |
-
for chunk in chunks:
|
56 |
-
# import pdb;pdb.set_trace()
|
57 |
-
input_features = processor(chunk[0], sampling_rate=sampling_rate, return_tensors="pt").input_features.to(device)
|
58 |
-
|
59 |
-
with torch.no_grad():
|
60 |
-
if task == "translate":
|
61 |
-
forced_decoder_ids = processor.get_decoder_prompt_ids(language=language, task="translate")
|
62 |
-
generated_ids = model.generate(input_features, forced_decoder_ids=forced_decoder_ids)
|
63 |
-
else:
|
64 |
-
generated_ids = model.generate(input_features)
|
65 |
-
|
66 |
-
transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
67 |
-
results.extend(transcription)
|
68 |
-
|
69 |
-
return " ".join(results)
|
70 |
-
|
71 |
-
|
72 |
-
# Process uploaded files
|
73 |
-
if submit_button and uploaded_files is not None:
|
74 |
-
st.session_state.audio_files = uploaded_files
|
75 |
-
st.session_state.detected_languages = []
|
76 |
-
st.session_state.waveforms = []
|
77 |
-
|
78 |
-
for uploaded_file in uploaded_files:
|
79 |
-
waveform, sampling_rate = ta.load(BytesIO(uploaded_file.read()))
|
80 |
-
if sampling_rate != SAMPLING_RATE:
|
81 |
-
waveform = ta.functional.resample(waveform, orig_freq=sampling_rate, new_freq=SAMPLING_RATE)
|
82 |
-
|
83 |
-
st.session_state.waveforms.append(waveform)
|
84 |
-
detected_language = detect_language(waveform)
|
85 |
-
st.session_state.detected_languages.append(detected_language)
|
86 |
|
87 |
# Display uploaded files and options
|
88 |
if 'audio_files' in st.session_state and st.session_state.audio_files:
|
@@ -91,7 +26,7 @@ if 'audio_files' in st.session_state and st.session_state.audio_files:
|
|
91 |
|
92 |
with col1:
|
93 |
st.write(f"**File name**: {uploaded_file.name}")
|
94 |
-
st.audio(
|
95 |
st.write(f"**Detected Language**: {st.session_state.detected_languages[i]}")
|
96 |
|
97 |
with col2:
|
@@ -103,6 +38,10 @@ if 'audio_files' in st.session_state and st.session_state.audio_files:
|
|
103 |
if st.session_state.transcriptions.get(i):
|
104 |
st.write("**Transcription**:")
|
105 |
st.write(st.session_state.transcriptions[i])
|
|
|
|
|
|
|
|
|
106 |
|
107 |
if st.button(f"Translate {uploaded_file.name}"):
|
108 |
with st.spinner("Translating..."):
|
@@ -116,4 +55,6 @@ if 'audio_files' in st.session_state and st.session_state.audio_files:
|
|
116 |
|
117 |
if st.session_state.translations.get(i):
|
118 |
st.write("**Translation**:")
|
119 |
-
st.write(st.session_state.translations[i])
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import pickle
|
|
|
|
|
3 |
from io import BytesIO
|
4 |
+
import pyperclip
|
5 |
+
from audio_processing import detect_language, process_long_audio, load_and_resample_audio
|
6 |
+
from model_utils import load_models
|
7 |
+
from config import SAMPLING_RATE
|
8 |
+
from llm_utils import generate_answer, summarize_transcript
|
|
|
|
|
|
|
9 |
|
10 |
+
# Load models at startup
|
11 |
+
load_models()
|
|
|
12 |
|
13 |
# Title of the app
|
14 |
+
st.title("Audio Player with Live Transcription and Q&A")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# ... (previous code remains the same)
|
17 |
|
18 |
+
def copy_to_clipboard(text):
|
19 |
+
pyperclip.copy(text)
|
20 |
+
st.success("Copied to clipboard!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
# Display uploaded files and options
|
23 |
if 'audio_files' in st.session_state and st.session_state.audio_files:
|
|
|
26 |
|
27 |
with col1:
|
28 |
st.write(f"**File name**: {uploaded_file.name}")
|
29 |
+
st.audio(uploaded_file, format=uploaded_file.type)
|
30 |
st.write(f"**Detected Language**: {st.session_state.detected_languages[i]}")
|
31 |
|
32 |
with col2:
|
|
|
38 |
if st.session_state.transcriptions.get(i):
|
39 |
st.write("**Transcription**:")
|
40 |
st.write(st.session_state.transcriptions[i])
|
41 |
+
if st.button("Copy Transcription", key=f"copy_transcription_{i}"):
|
42 |
+
copy_to_clipboard(st.session_state.transcriptions[i])
|
43 |
+
|
44 |
+
# ... (summarization and Q&A code remains the same)
|
45 |
|
46 |
if st.button(f"Translate {uploaded_file.name}"):
|
47 |
with st.spinner("Translating..."):
|
|
|
55 |
|
56 |
if st.session_state.translations.get(i):
|
57 |
st.write("**Translation**:")
|
58 |
+
st.write(st.session_state.translations[i])
|
59 |
+
if st.button("Copy Translation", key=f"copy_translation_{i}"):
|
60 |
+
copy_to_clipboard(st.session_state.translations[i])
|