Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import speech_recognition as sr | |
| from deep_translator import GoogleTranslator | |
| from gtts import gTTS | |
| import os | |
| # Recognize and translate audio to Chinese | |
| def recognize_and_translate(audio_file_path): | |
| # Use the file path directly from Gradio input | |
| recognizer = sr.Recognizer() | |
| with sr.AudioFile(audio_file_path) as source: | |
| audio_data = recognizer.record(source) | |
| try: | |
| text = recognizer.recognize_google(audio_data) | |
| except sr.UnknownValueError: | |
| return "Could not understand the audio." | |
| except sr.RequestError: | |
| return "Network error. Please try again." | |
| # Translate the recognized text to Chinese | |
| translated_text = GoogleTranslator(source="en", target="zh-CN").translate(text) | |
| return f"Recognized: {text}\nTranslated to Chinese: {translated_text}" | |
| # Translate Chinese input to English and convert to speech | |
| def translate_to_english(chinese_text): | |
| # Translate Chinese text to English | |
| translated_text = GoogleTranslator(source="zh-CN", target="en").translate(chinese_text) | |
| # Convert the translated text to speech | |
| tts = gTTS(text=translated_text, lang="en", slow=False) | |
| tts.save("output.mp3") | |
| return translated_text, "output.mp3" | |
| # Create Gradio interfaces | |
| interface1 = gr.Interface( | |
| fn=recognize_and_translate, | |
| inputs=gr.Audio(type="filepath"), | |
| outputs="text", | |
| title="Speech Recognition and Translation to Chinese", | |
| ) | |
| interface2 = gr.Interface( | |
| fn=translate_to_english, | |
| inputs="text", | |
| outputs=["text", "audio"], | |
| title="Translate Chinese to English and Play", | |
| ) | |
| # Combine both interfaces into a tabbed layout | |
| app = gr.TabbedInterface([interface1, interface2], ["Speech to Chinese", "Chinese to English"]) | |
| # Launch the app | |
| if __name__ == "__main__": | |
| app.launch() | |