Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor, pipeline
|
| 3 |
+
import torch
|
| 4 |
+
import librosa
|
| 5 |
+
|
| 6 |
+
# Load models
|
| 7 |
+
@st.cache_resource # Cache the models for faster reloads
|
| 8 |
+
def load_models():
|
| 9 |
+
processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h")
|
| 10 |
+
model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h")
|
| 11 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 12 |
+
return processor, model, summarizer
|
| 13 |
+
|
| 14 |
+
processor, model, summarizer = load_models()
|
| 15 |
+
|
| 16 |
+
# Function to convert audio to text
|
| 17 |
+
def audio_to_text(audio_path):
|
| 18 |
+
speech, _ = librosa.load(audio_path, sr=16000)
|
| 19 |
+
input_values = processor(speech, return_tensors="pt", sampling_rate=16000).input_values
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
logits = model(input_values).logits
|
| 22 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
| 23 |
+
transcription = processor.decode(predicted_ids[0])
|
| 24 |
+
return transcription
|
| 25 |
+
|
| 26 |
+
# Function to summarize text
|
| 27 |
+
def summarize_text(text):
|
| 28 |
+
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
|
| 29 |
+
return summary[0]['summary_text']
|
| 30 |
+
|
| 31 |
+
# Streamlit app
|
| 32 |
+
def main():
|
| 33 |
+
st.title("Audio Summarization App")
|
| 34 |
+
st.write("Upload an audio file to get a summary of its content.")
|
| 35 |
+
|
| 36 |
+
# File uploader
|
| 37 |
+
audio_file = st.file_uploader("Upload Audio File", type=["wav", "mp3", "ogg"])
|
| 38 |
+
|
| 39 |
+
if audio_file is not None:
|
| 40 |
+
st.audio(audio_file, format="audio/wav")
|
| 41 |
+
|
| 42 |
+
# Process the audio file
|
| 43 |
+
if st.button("Generate Summary"):
|
| 44 |
+
with st.spinner("Processing audio..."):
|
| 45 |
+
# Convert audio to text
|
| 46 |
+
text = audio_to_text(audio_file)
|
| 47 |
+
st.subheader("Transcribed Text:")
|
| 48 |
+
st.write(text)
|
| 49 |
+
|
| 50 |
+
# Summarize the text
|
| 51 |
+
summary = summarize_text(text)
|
| 52 |
+
st.subheader("Summary:")
|
| 53 |
+
st.write(summary)
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
main()
|