Cryptic commited on
Commit
66cc2a4
·
1 Parent(s): ee3f375

Update app.py to handle audio transcription and summarization

Browse files
Files changed (1) hide show
  1. app.py +27 -25
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import streamlit as st
2
- import soundfile as sf
3
- import numpy as np
4
  from transformers import pipeline
 
 
5
 
6
  # Load Transcriber model optimized for CPU
7
  transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-tiny.en", device=-1)
@@ -9,32 +9,31 @@ transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-tin
9
  # Load Summary Model optimized for CPU
10
  summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", device=-1)
11
 
12
- # Streamlit UI to upload file
13
- st.title("Audio Summarization Tool")
14
- uploaded_file = st.file_uploader("Upload an audio file", type=["wav", "mp3", "flac"])
15
 
 
16
  if uploaded_file is not None:
17
- # Read the audio file with soundfile
18
- audio_data, sample_rate = sf.read(uploaded_file)
19
-
20
- # Transcribing audio
21
- lecture_text = transcriber(audio_data)["text"]
22
-
23
- # Preprocessing data
 
24
  num_words = len(lecture_text.split())
25
  max_length = min(num_words, 1024) # BART model max input length is 1024 tokens
26
- max_length = int(max_length * 0.75) # Convert max words to approx tokens
27
-
28
- if max_length > 1024:
29
- lecture_text = lecture_text[:int(1024 / 0.75)] # Truncate to fit the model's token limit
30
 
31
  # Summarization
32
- summary = summarizer(
33
- lecture_text,
34
- max_length=1024, # DistilBART max input length is 1024 tokens
35
- min_length=int(max_length * 0.1),
36
- truncation=True
37
- )
 
38
 
39
  # Clean up the summary text
40
  if not summary[0]["summary_text"].endswith((".", "!", "?")):
@@ -42,6 +41,9 @@ if uploaded_file is not None:
42
  if last_period_index != -1:
43
  summary[0]["summary_text"] = summary[0]["summary_text"][:last_period_index + 1]
44
 
45
- # Output
46
- st.write("### Summary:")
47
- st.write(summary[0]["summary_text"])
 
 
 
 
1
  import streamlit as st
 
 
2
  from transformers import pipeline
3
+ import soundfile as sf
4
+ import torch
5
 
6
  # Load Transcriber model optimized for CPU
7
  transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-tiny.en", device=-1)
 
9
  # Load Summary Model optimized for CPU
10
  summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", device=-1)
11
 
12
+ # Streamlit file uploader
13
+ uploaded_file = st.file_uploader("Upload an audio file", type=["wav"])
 
14
 
15
+ # Process the uploaded file
16
  if uploaded_file is not None:
17
+ # Save the uploaded file temporarily
18
+ audio_data, samplerate = sf.read(uploaded_file)
19
+
20
+ # Transcribing the audio
21
+ with st.spinner('Transcribing the audio...'):
22
+ lecture_text = transcriber(uploaded_file)["text"]
23
+
24
+ # Preprocessing text
25
  num_words = len(lecture_text.split())
26
  max_length = min(num_words, 1024) # BART model max input length is 1024 tokens
27
+ max_length = int(max_length * 0.75) # Approx token conversion
 
 
 
28
 
29
  # Summarization
30
+ with st.spinner('Summarizing the lecture...'):
31
+ summary = summarizer(
32
+ lecture_text,
33
+ max_length=1024, # DistilBART max input length
34
+ min_length=int(max_length * 0.1),
35
+ truncation=True
36
+ )
37
 
38
  # Clean up the summary text
39
  if not summary[0]["summary_text"].endswith((".", "!", "?")):
 
41
  if last_period_index != -1:
42
  summary[0]["summary_text"] = summary[0]["summary_text"][:last_period_index + 1]
43
 
44
+ # Output summary
45
+ st.write("\n### Summary:\n", summary[0]["summary_text"])
46
+
47
+ else:
48
+ st.warning("Please upload a valid audio file.")
49
+