Last active
March 12, 2023 08:45
-
-
Save wiseaidev/aacae9c0284ac832c733567845690308 to your computer and use it in GitHub Desktop.
A simple fastapi endpoint that allows users to translate text into the language of aliens, but with the added twist that the translations are based on the language of an extraterrestrial species. it also supports for input text in other languages by using a language detection `langdetect`, and supports for audio translation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fastapi import FastAPI | |
from langdetect import detect | |
from gtts import gTTS | |
app = FastAPI() | |
translations = { | |
"Vortian": { | |
"A": "Z", | |
"B": "Y", | |
"C": "X", | |
"D": "W", | |
"E": "V", | |
"F": "U", | |
"G": "T", | |
"H": "S", | |
"I": "R", | |
"J": "Q", | |
"K": "P", | |
"L": "O", | |
"M": "N", | |
"N": "M", | |
"O": "L", | |
"P": "K", | |
"Q": "J", | |
"R": "I", | |
"S": "H", | |
"T": "G", | |
"U": "F", | |
"V": "E", | |
"W": "D", | |
"X": "C", | |
"Y": "B", | |
"Z": "A", | |
}, | |
"Xenomorph": { | |
"A": "C", | |
"B": "F", | |
"C": "E", | |
"D": "H", | |
"E": "G", | |
"F": "B", | |
"G": "A", | |
"H": "D", | |
"I": "K", | |
"J": "N", | |
"K": "M", | |
"L": "P", | |
"M": "O", | |
"N": "J", | |
"O": "I", | |
"P": "L", | |
"Q": "T", | |
"R": "W", | |
"S": "Z", | |
"T": "Q", | |
"U": "X", | |
"V": "U", | |
"W": "R", | |
"X": "V", | |
"Y": "B", | |
"Z": "S", | |
}, | |
"Klingon": { | |
"A": "0", | |
"B": "1", | |
"C": "2", | |
"D": "3", | |
"E": "4", | |
"F": "5", | |
"G": "6", | |
"H": "7", | |
"I": "8", | |
"J": "9", | |
"K": "a", | |
"L": "b", | |
"M": "c", | |
"N": "d", | |
"O": "e", | |
"P": "f", | |
"Q": "g", | |
"R": "h", | |
"S": "i", | |
"T": "j", | |
"U": "k", | |
"V": "l", | |
"W": "m", | |
"X": "n", | |
"Y": "o", | |
"Z": "p", | |
}, | |
} | |
@app.post("/translate") | |
async def translate(input_text: str, input_language: str): | |
detected_language = detect(input_text) | |
if detected_language == "en": | |
if input_language == "Vortian": | |
translation_dict = translations["Vortian"] | |
elif input_language == "Xenomorph": | |
translation_dict = translations["Xenomorph"] | |
elif input_language == "Klingon": | |
translation_dict = translations["Klingon"] | |
else: | |
return {"error": "Unsupported input language"} | |
else: | |
return {"error": "Unsupported input language"} | |
translated_text = "" | |
for c in input_text.lower(): | |
if c in translation_dict: | |
translated_text += translation_dict[c] | |
else: | |
translated_text += c | |
tts = gTTS(translated_text) | |
tts.save("translation.mp3") | |
return {"translated_text": translated_text, "audio_file": "translation.mp3"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment