import gradio as gr def greet(name): return "Hello " + name + "!!" iface = gr.Interface(fn=greet, inputs="text", outputs="text") # Import the required libraries from gradio import Interface, gradio as gr # Define the translation function def translate_text_to_text(input_text, source_language, target_language, text_to_speech): # Initialize the Gradio client client = gr.Client("https://facebook-seamless-m4t.hf.space/") # Define the input parameters for text-to-text translation task = "T2TT (Text to Text translation)" audio_source = "text" input_speech = "" # Make a prediction for text-to-text translation using the Gradio client translation_result = client.predict( task, audio_source, input_speech, input_text, source_language, target_language, api_name="/run" ) # Extract the translated text from the result translated_text = translation_result["output_text"] # If text-to-speech is enabled, perform text-to-speech synthesis if text_to_speech: # Define the input parameters for text-to-speech synthesis task = "T2ST (Text to Speech translation)" audio_source = "text" input_speech = translated_text # Make a prediction for text-to-speech synthesis using the Gradio client text_to_speech_result = client.predict( task, audio_source, input_speech, "", target_language, # Use the target language for speech synthesis target_language, # Use the target language for speech synthesis api_name="/run" ) # Extract and return the synthesized speech audio synthesized_speech_audio = text_to_speech_result["output_audio"] return translated_text, synthesized_speech_audio # If text-to-speech is not enabled, return only the translated text return translated_text # Create a Gradio interface iface = Interface( fn=translate_text_to_text, inputs=[ gr.Textbox(label="Input Text"), gr.Dropdown(label="Source Language", choices=["English", "Spanish", "French", "German"], default="English"), gr.Dropdown(label="Target Language", choices=["English", "Spanish", "French", "German"], default="Spanish"), gr.Checkbox(label="Enable Text-to-Speech", default=False) ], outputs=[ gr.Textbox(label="Translated Text"), gr.Audio(label="Synthesized Speech", type="numpy", src="output[1]") ] # Use src="output[1]" to display the synthesized speech ) # Create a Gradio interface with a custom description custom_description = """

Text Translation and Text-to-Speech Application

Enabled with English, Spanish, French, German, Urdu and Hindi

Created by Syed Usman Ghani :: NED University of Engineering & Technology :: Python for Data Science Course :: Mid-term Project

""" iface = Interface( fn=translate_text_to_text, inputs=[ gr.Textbox(label="Input Text"), gr.Dropdown(label="Source Language", choices=["English", "Spanish", "French", "German", "Urdu", "Hindi"], default="English"), gr.Dropdown(label="Target Language", choices=["English", "Spanish", "French", "German", "Urdu", "Hindi"], default="Spanish"), gr.Checkbox(label="Enable Text-to-Speech", default=False) ], outputs=[ gr.Textbox(label="Translated Text"), gr.Audio(label="Synthesized Speech", type="numpy", src="output[1]") ], title="Text & Speech Translation App", # Set a title for your app description=custom_description, # Use the custom description HTML ) # Launch the Gradio interface iface.launch(share=True)