Pytranslate6


Pytranslate6 has the new TR command.

Basically it translates any languages text into any other languages text.

This came up soon. source code here.

import requests
from gtts import gTTS
import os
import speech_recognition as sr

def recognize_russian_speech():
recognizer = sr.Recognizer()

# Adjust the device index or use `recognizer.listen()` for microphone input
with sr.Microphone(device_index=0) as source:
print("Скажи что-нибудь по-русски...")
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)

try:
readlanguage = input("What language are you speaking, ru-RU, for russian, de-DE for german, etc. :")
recognized_text = recognizer.recognize_google(audio, language=readlanguage)
print("You said:", recognized_text)
return recognized_text
except sr.UnknownValueError:
print("Sorry, we couldn't understand you. ")
return None
except sr.RequestError as e:
print("ERROR {0}".format(e))
return None

def translate_text(text, target_language="ru"):
url = "<a href="https://translate.googleapis.com/translate_a/single">https://translate.googleapis.com/translate_a/single</a>"
params = {
"client": "gtx",
"sl": "auto",
"tl": target_language,
"dt": "t",
"q": text
}

response = requests.get(url, params=params)
translation = response.json()[0][0][0]
return translation

def recognize_speech():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Say something...")
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)

try:
print("Recognizing speech...")
text = recognizer.recognize_google(audio)
print("You said:", text)
return text
except sr.UnknownValueError:
print("Sorry, could not understand audio.")
return None
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
return None

def main():
print("Welcome to the Translate Bot!")
print("")
print("Type 'exit' to quit.")
print("")

while True:
user_input = input("Enter 's' to translate your speech, or 't' to translate typed speech, 'r' to read translated speech, and 'srr' to listen to translated spoken speech : ")

if user_input.lower() == "s":
# Speech input
speech_text = recognize_speech()
if speech_text:
target_language = input("Enter the target language (e.g., 'ru' for Russian, 'de' for German, 'en' for English, etc.): ")
translated_text = translate_text(speech_text, target_language)
print("Translated text:", translated_text)
elif user_input.lower() == "t":
# Text input
text_input = input("Enter text to translate: ")
target_language = input("Enter the target language (e.g., 'ru' for Russian, 'de' for German, 'en' for English, etc.): ")
translated_text = translate_text(text_input, target_language)
print("Translated text:", translated_text)
elif user_input.lower() == "exit":
print("Goodbye!")
break
elif user_input.lower() == "Выход":
print("До свидания!")
break
elif user_input.lower() == "rr":
# Recognize Russian speech
recognized_text = recognize_russian_speech()
if recognized_text:
target_language = input("Enter the target language initials (e.g., 'ru' for Russian, 'de' for German, 'en' for English, etc.): ").lower()
translated_text = translate_text(recognized_text, target_language)
print("Translated text:", translated_text)
elif user_input.lower() == "exit":
print("Goodbye!")
break
elif user_input.lower() == "tr":
# Translation input
text_to_translate = input("Enter the text you want to translate: ")
source_language = input("Enter the source language (e.g., 'en' for English, 'ru' for Russian, 'de' for German, etc.): ")
target_language = input("Enter the target language (e.g., 'en' for English, 'ru' for Russian, 'de' for German, etc.): ")
translated_text = translate_text(text_to_translate, target_language)
print("Translated text:", translated_text)

elif user_input.lower() == "r":
# Text input
text_input = input("Enter text to translate: ")
target_language = input("Enter the target language (e.g., 'ru' for Russian, 'de' for German, 'en' for English, etc.): ")
translated_text = translate_text(text_input, target_language)
tts = gTTS(text=translated_text, lang=target_language)
tts.save("translated_text.mp3")
os.system("start translated_text.mp3") # This will play the audio using the default system player
elif user_input.lower() == "srr":
# Speech input
speech_text = recognize_speech()
if speech_text:
target_language = input("Enter the target language (e.g., 'ru' for Russian, 'de' for German, 'en' for English, etc.): ")
translated_text = translate_text(speech_text, target_language)
tts = gTTS(text=translated_text, lang=target_language)
tts.save("translated_text.mp3")
os.system("start translated_text.mp3") # This will play the audio using the default system player

else:
print("Invalid input, you have to use lowercase")

if __name__ == "__main__":
main()

Files

Pytranslate6.py 5.5 kB
Mar 03, 2024

Get Pytranslate

Leave a comment

Log in with itch.io to leave a comment.