File size: 3,979 Bytes
e84b5e6 be33810 e84b5e6 be33810 e84b5e6 be33810 e84b5e6 |
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
'''
Created By Lewis Kamau Kimaru
Sema fastapi backend
August 2023
'''
from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
import gradio as gr
import ctranslate2
import sentencepiece as spm
import fasttext
import uvicorn
from pyngrok import ngrok
import nest_asyncio
import os
app = FastAPI()
# Set your ngrok authtoken
#ngrok.set_auth_token("2UAhCqf5zP0cCgJzeadNANkbIqx_7ZJvhkDSNWccqMX2hyxXP")
#ngrok.set_auth_token("2S6xeFEoSVFWr2egtDRcqgeUtSx_2juefHFkEW6nGbpRHS37W")
#ngrok.set_auth_token("2UAmdjHdAFV9x84TdyEknIfNhYk_4Ye8n4YK7ZhfCMob3yPBh")
#ngrok.set_auth_token("2UAqm26HuWiWvQjzK58xYufSGpy_6tStKSyLLyR9f7pcezh6R")
ngrok.set_auth_token("2UGQqzZoI3bx7SSk8H4wuFC3iaC_2WniWyNAsW5fd2rFyKVq1")
fasttext.FastText.eprint = lambda x: None
# Load the model and tokenizer ..... only once!
beam_size = 1 # change to a smaller value for faster inference
device = "cpu" # or "cuda"
# Language Prediction model
print("\nimporting Language Prediction model")
lang_model_file = "modules/lid218e.bin"
lang_model_full_path = os.path.join(os.path.dirname(__file__), lang_model_file)
lang_model = fasttext.load_model(lang_model_full_path)
# Load the source SentencePiece model
print("\nimporting SentencePiece model")
sp_model_file = "modules/spm.model"
sp_model_full_path = os.path.join(os.path.dirname(__file__), sp_model_file)
sp = spm.SentencePieceProcessor()
sp.load(sp_model_full_path)
# Import The Translator model
print("\nimporting Translator model")
ct_model_file = "modules/sematrans-3.3B"
ct_model_full_path = os.path.join(os.path.dirname(__file__), ct_model_file)
translator = ctranslate2.Translator(ct_model_full_path, device)
print('\nDone importing models\n')
def translate_text(userinput: str, target_lang: str):
source_sents = [userinput]
source_sents = [sent.strip() for sent in source_sents]
target_prefix = [[target_lang]] * len(source_sents)
# Predict the source language
predictions = lang_model.predict(source_sents[0], k=1)
source_lang = predictions[0][0].replace('__label__', '')
# Subword the source sentences
source_sents_subworded = sp.encode(source_sents, out_type=str)
source_sents_subworded = [[source_lang] + sent + ["</s>"] for sent in source_sents_subworded]
# Translate the source sentences
translations = translator.translate_batch(
source_sents_subworded,
batch_type="tokens",
max_batch_size=2024,
beam_size=beam_size,
target_prefix=target_prefix,
)
translations = [translation[0]['tokens'] for translation in translations]
# Desubword the target sentences
translations_desubword = sp.decode(translations)
translations_desubword = [sent[len(target_lang):] for sent in translations_desubword]
# Return the source language and the translated text
return source_lang, translations_desubword
@app.get("/")
def read_root():
return {"message": "Welcome to the Sema Translation API! \nThis API was created by Lewsi Kamau Kimaru"}
@app.post("/translate/")
async def translate_endpoint(request: Request):
data = await request.json()
userinput = data.get("userinput")
target_lang = data.get("target_lang")
print(f"\n Target Language; {target_lang}, User Input: {userinput}\n")
if not userinput or not target_lang:
raise HTTPException(status_code=422, detail="Both 'userinput' and 'target_lang' are required.")
source_lang, translated_text = translate_text(userinput, target_lang)
print(f"\nsource_language: {source_lang}, Translated Text: {translated_text}\n\n")
return {
"source_language": source_lang,
"translated_text": translated_text[0],
}
ngrok_tunnel = ngrok.connect(7860)
public_url = ngrok_tunnel.public_url
print('\nPublic URL✅:', public_url)
nest_asyncio.apply()
print("\nAPI starting .......\n")
#uvicorn.run(app, port=7860)
|