Spaces:
No application file
No application file
File size: 3,816 Bytes
db0d527 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | 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 = """
<div style="text-align:center;">
<h1>Text Translation and Text-to-Speech Application</h1>
<h2>Enabled with English, Spanish, French, German, Urdu and Hindi</h2>
<p>Created by Syed Usman Ghani :: NED University of Engineering & Technology :: Python for Data Science Course :: Mid-term Project</p>
</div>
"""
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) |