shukdevdatta123 commited on
Commit
4a48709
·
verified ·
1 Parent(s): 5cd8459

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -7
app.py CHANGED
@@ -1,22 +1,24 @@
1
- #
2
  import streamlit as st
3
  from tempfile import NamedTemporaryFile
4
  from audiorecorder import audiorecorder
5
  from whispercpp import Whisper
 
 
6
 
7
- # Download whisper.cpp
8
  w = Whisper('tiny')
9
 
10
- def inference(audio_data):
11
- # Save the raw audio data to a temporary file
12
  with NamedTemporaryFile(suffix=".wav", delete=False) as temp:
13
- temp.write(audio_data) # write the raw audio bytes
 
14
  temp.close() # Ensure the file is written and closed before passing it to Whisper
15
  result = w.transcribe(temp.name)
16
  text = w.extract_text(result)
17
  return text[0]
18
 
19
- # Streamlit
20
  with st.sidebar:
21
  audio = audiorecorder("Click to send voice message", "Recording... Click when you're done", key="recorder")
22
  st.title("Echo Bot with Whisper")
@@ -34,7 +36,9 @@ for message in st.session_state.messages:
34
  if (prompt := st.chat_input("Your message")) or len(audio):
35
  # If it's coming from the audio recorder transcribe the message with whisper.cpp
36
  if len(audio) > 0:
37
- prompt = inference(audio)
 
 
38
 
39
  # Display user message in chat message container
40
  st.chat_message("user").markdown(prompt)
 
 
1
  import streamlit as st
2
  from tempfile import NamedTemporaryFile
3
  from audiorecorder import audiorecorder
4
  from whispercpp import Whisper
5
+ from pydub import AudioSegment
6
+ import io
7
 
8
+ # Initialize whisper.cpp
9
  w = Whisper('tiny')
10
 
11
+ def inference(audio_segment):
12
+ # Convert AudioSegment to WAV format in memory
13
  with NamedTemporaryFile(suffix=".wav", delete=False) as temp:
14
+ # Export AudioSegment to raw bytes in WAV format
15
+ audio_segment.export(temp.name, format="wav")
16
  temp.close() # Ensure the file is written and closed before passing it to Whisper
17
  result = w.transcribe(temp.name)
18
  text = w.extract_text(result)
19
  return text[0]
20
 
21
+ # Streamlit UI setup
22
  with st.sidebar:
23
  audio = audiorecorder("Click to send voice message", "Recording... Click when you're done", key="recorder")
24
  st.title("Echo Bot with Whisper")
 
36
  if (prompt := st.chat_input("Your message")) or len(audio):
37
  # If it's coming from the audio recorder transcribe the message with whisper.cpp
38
  if len(audio) > 0:
39
+ # Convert audio (from audiorecorder) to AudioSegment
40
+ audio_segment = AudioSegment.from_file(io.BytesIO(audio), format="wav")
41
+ prompt = inference(audio_segment)
42
 
43
  # Display user message in chat message container
44
  st.chat_message("user").markdown(prompt)