huimanho commited on
Commit
cd7ce5f
·
verified ·
1 Parent(s): aa82b15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -22
app.py CHANGED
@@ -1,33 +1,24 @@
1
  import streamlit as st
2
- import sounddevice as sd
3
  import numpy as np
4
  import torch
5
  from transformers import pipeline
 
6
 
7
  # Load the pipelines
8
  asr_pipe = pipeline("automatic-speech-recognition", model="alvanlii/whisper-small-cantonese")
9
  translation_pipe = pipeline("translation", model="raptorkwok/cantonese-chinese-translation")
10
  tts_pipe = pipeline("text-to-speech", model="myshell-ai/MeloTTS-Chinese")
11
 
12
- # Function to record audio
13
- def record_audio(duration=5, fs=16000):
14
- st.write("Recording...")
15
- audio = sd.rec(int(duration * fs), samplerate=fs, channels=1, dtype='float32')
16
- sd.wait()
17
- st.write("Recording complete.")
18
- return audio.flatten()
19
-
20
- # Function to play audio
21
- def play_audio(audio, fs=16000):
22
- sd.play(audio, fs)
23
- sd.wait()
24
-
25
  # Streamlit UI
26
  st.title("Cantonese to Chinese Translator")
27
- st.write("Click the button below to record your Cantonese speech.")
28
 
29
- if st.button("Record"):
30
- audio = record_audio()
 
 
 
 
31
 
32
  # Recognize Cantonese speech
33
  audio_input = torch.tensor(audio)
@@ -43,8 +34,4 @@ if st.button("Record"):
43
  tts_output = tts_pipe(chinese_text)
44
 
45
  # Play back the Chinese output
46
- st.write("Playing back the Chinese translation...")
47
- play_audio(tts_output['audio'])
48
-
49
- # Run the app using the command:
50
- # streamlit run app.py
 
1
  import streamlit as st
 
2
  import numpy as np
3
  import torch
4
  from transformers import pipeline
5
+ import librosa
6
 
7
  # Load the pipelines
8
  asr_pipe = pipeline("automatic-speech-recognition", model="alvanlii/whisper-small-cantonese")
9
  translation_pipe = pipeline("translation", model="raptorkwok/cantonese-chinese-translation")
10
  tts_pipe = pipeline("text-to-speech", model="myshell-ai/MeloTTS-Chinese")
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  # Streamlit UI
13
  st.title("Cantonese to Chinese Translator")
14
+ st.write("Upload your Cantonese audio file (WAV format) below.")
15
 
16
+ # File upload
17
+ uploaded_file = st.file_uploader("Choose a WAV file", type="wav")
18
+
19
+ if uploaded_file is not None:
20
+ # Load the audio file
21
+ audio, sr = librosa.load(uploaded_file, sr=16000)
22
 
23
  # Recognize Cantonese speech
24
  audio_input = torch.tensor(audio)
 
34
  tts_output = tts_pipe(chinese_text)
35
 
36
  # Play back the Chinese output
37
+ st.audio(tts_output['audio'], format='audio/wav')