huimanho commited on
Commit
fdf092c
·
verified ·
1 Parent(s): 097808c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
34
+ result = asr_pipe(audio_input)
35
+ cantonese_text = result['text']
36
+ st.write(f"Cantonese Text: {cantonese_text}")
37
+
38
+ # Translate Cantonese to Chinese
39
+ chinese_text = translation_pipe(cantonese_text)[0]['translation_text']
40
+ st.write(f"Chinese Text: {chinese_text}")
41
+
42
+ # Convert Chinese text to speech
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