English to French - French to English based on Meta & HuggingFace Chat Bot
import requests
import sounddevice as sd
import base64
from transformers import pipeline
Initialize the translation pipeline
translator_en_to_fr = pipeline("translation_en_to_fr")
translator_fr_to_en = pipeline("translation_fr_to_en")
def translate_text(text, language):
"""
Translate text from one language to another.
Args:
text (str): The text to translate.
language (str): The language pair to use (e.g. "en_to_fr" or "fr_to_en").
Returns:
str: The translated text.
"""
if language == "en_to_fr":
return translator_en_to_fr(text)[0]['translation_text']
elif language == "fr_to_en":
return translator_fr_to_en(text)[0]['translation_text']
else:
raise ValueError("Invalid language pair")
def generate_audio(text, speaker_id, api_url, api_token):
"""
Generate audio for a given text using the Hugging Face API.
Args:
text (str): The text to generate audio for.
speaker_id (str): The speaker ID to use.
api_url (str): The URL of the Hugging Face API.
api_token (str): The API token to use.
Returns:
tuple: A tuple containing the audio data and sampling rate.
"""
try:
if not text or not speaker_id:
raise ValueError("Text and speaker ID cannot be empty")
headers = {
"Authorization": f"Bearer {api_token}",
}
payload = {
"inputs": text,
"parameters": {
"speaker_id": speaker_id
}
}
response = requests.post(api_url, headers=headers, json=payload)
response.raise_for_status()
audio = response.json().get("audio")
sampling_rate = response.json().get("sampling_rate")
return audio, sampling_rate
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
return None
except ValueError as e:
print(f"Error: {e}")
return None
def play_audio(audio, sampling_rate):
"""
Play audio using the sounddevice library.
Args:
audio (bytes): The audio data to play.
sampling_rate (int): The sampling rate of the audio.
"""
try:
if not audio or not sampling_rate:
raise ValueError("Audio and sampling rate cannot be empty")
sd.play(audio, sampling_rate)
sd.wait()
except sd.PortAudioError as e:
print(f"Error: {e}")
except Exception as e:
print(f"Error: {e}")
def main():
english_text = "Hello, my name is Eric Carnell Bearden and I like to talk."
french_text = "Bonjour, je m'appelle Eric Carnell Bearden et j'aime parler."
api_token = "YOUR_API_TOKEN" # Replace with your actual API token
speaker_id = "[spkr_63]"
api_url = "https://api-inference.huggingface.co/models/11mlabs/indri-0.1-350m-tts"
# English to French translation section
print("English to French translation:")
translated_french_text = translate_text(english_text, "en_to_fr")
print(f"English to French: {english_text} -> {translated_french_text}")
# Generate audio for English and French text
print("\nGenerating audio for English and French text:")
audio_en, sampling_rate_en = generate_audio(english_text, speaker_id, api_url, api_token)
if audio_en and sampling_rate_en:
play_audio(audio_en, sampling_rate_en)
audio_fr, sampling_rate_fr = generate_audio(translated_french_text, speaker_id, api_url, api_token)
if audio_fr and sampling_rate_fr:
play_audio(audio_fr, sampling_rate_fr)
# French to English translation section
print("\nFrench to English translation:")
translated_english_text = translate_text(french_text, "fr_to_en")