translates user's speech into chinese
Drop files here
or click to upload
import streamlit as st
import sounddevice as sd
import numpy as np
from scipy.io.wavfile import write
import io
from googletrans import Translator
st.title("Speech to Chinese Translator")
# Initialize the translator
translator = Translator()
# Audio recording parameters
duration = st.slider("Recording duration (seconds)", 1, 10, 5)
sample_rate = 44100
if st.button("Record Speech"):
st.write("Recording...")
# Record audio
recording = sd.rec(int(duration * sample_rate),
samplerate=sample_rate,
channels=1)
sd.wait()
# Save the recording temporarily
audio_file = io.BytesIO()
write(audio_file, sample_rate, recording)
# Display the recorded audio
st.audio(audio_file)
# For demonstration purposes, we'll use a text input since
# speech-to-text requires additional APIs
text_input = st.text_input(
"Since this is a demo, please type what you said:",
"Hello, how are you?"
)
if text_input:
# Translate to Chinese
try:
translation = translator.translate(text_input, dest='zh-cn')
# Display results
st.write("Original Text:")
st.write(text_input)
st.write("Chinese Translation:")
st.write(translation.text)
st.write("Pronunciation (Pinyin):")
st.write(translation.pronunciation)
except Exception as e:
st.error(f"Translation error: {str(e)}")
st.markdown("""
### Note:
This is a simplified demo that:
1. Records your voice
2. Requires you to type what you said (in a real app, we'd use a Speech-to-Text API)
3. Translates the text to Chinese
For a production app, you would want to:
- Use a proper Speech-to-Text API (like Google Cloud Speech-to-Text)
- Handle more languages
- Improve error handling
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?