shukdevdatta123 commited on
Commit
3afe755
·
verified ·
1 Parent(s): afe4a62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -8
app.py CHANGED
@@ -6,12 +6,12 @@ from whispercpp import Whisper
6
  # Download whisper.cpp
7
  w = Whisper('tiny')
8
 
9
- def inference(audio):
10
- # Save audio to a file:
11
- with NamedTemporaryFile(suffix=".mp3") as temp:
12
- with open(f"{temp.name}", "wb") as f:
13
- f.write(audio.tobytes())
14
- result = w.transcribe(f"{temp.name}")
15
  text = w.extract_text(result)
16
  return text[0]
17
 
@@ -32,7 +32,7 @@ for message in st.session_state.messages:
32
  # React to user input
33
  if (prompt := st.chat_input("Your message")) or len(audio):
34
  # If it's coming from the audio recorder transcribe the message with whisper.cpp
35
- if len(audio)>0:
36
  prompt = inference(audio)
37
 
38
  # Display user message in chat message container
@@ -45,4 +45,4 @@ if (prompt := st.chat_input("Your message")) or len(audio):
45
  with st.chat_message("assistant"):
46
  st.markdown(response)
47
  # Add assistant response to chat history
48
- st.session_state.messages.append({"role": "assistant", "content": response})
 
6
  # Download whisper.cpp
7
  w = Whisper('tiny')
8
 
9
+ def inference(audio_data):
10
+ # Save the raw audio data to a temporary file
11
+ with NamedTemporaryFile(suffix=".wav", delete=False) as temp:
12
+ temp.write(audio_data) # write the raw audio bytes
13
+ temp.close() # Ensure the file is written and closed before passing it to Whisper
14
+ result = w.transcribe(temp.name)
15
  text = w.extract_text(result)
16
  return text[0]
17
 
 
32
  # React to user input
33
  if (prompt := st.chat_input("Your message")) or len(audio):
34
  # If it's coming from the audio recorder transcribe the message with whisper.cpp
35
+ if len(audio) > 0:
36
  prompt = inference(audio)
37
 
38
  # Display user message in chat message container
 
45
  with st.chat_message("assistant"):
46
  st.markdown(response)
47
  # Add assistant response to chat history
48
+ st.session_state.messages.append({"role": "assistant", "content": response})