Translation / app.py
KAPtechies's picture
Update app.py
f762839
raw
history blame
1.95 kB
import gradio as gr
import easyocr
import transformer
reader=easyocr.Reader(['en'])
# this needs to run only once to load the model into memory
result=reader.readtext('/content/WhatsApp Image 2023-09-23 at 8.03.28 AM.jpeg',detail=0)
news=" ".join(result)
from transformers import AutoTokenizer
tokenizer=AutoTokenizer.from_pretrained("facebook/mbart-large-50-one-to-many-mmt",use_fast=False)
from transformers import MBartForConditionalGeneration
# download and save model
model=MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-50-one-to-many-mmt")
input_text=[news]
# convert sentences to tensors
model_inputs=tokenizer(input_text,return_tensors="pt",padding=True,truncation=True)
# translate from English to Hindi
generated_tokens=model.generate(
**model_inputs,
forced_bos_token_id=tokenizer.lang_code_to_id["hi_IN"]
)
translation=tokenizer.batch_decode(generated_tokens,skip_special_tokens=True)
translation
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("facebook/mbart-large-50-one-to-many-mmt", use_fast=False)
from transformers import MBartForConditionalGeneration
# download and save model
model = MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-50-one-to-many-mmt")
def translator(img):
reader = easyocr.Reader(['en'])
result = reader.readtext(img,detail = 0)
news= " ".join(result)
input_text = [news]
# convert sentences to tensors
model_inputs = tokenizer(input_text, return_tensors="pt", padding=True, truncation=True)
# translate from English to Hindi
generated_tokens = model.generate(
**model_inputs,
forced_bos_token_id=tokenizer.lang_code_to_id["hi_IN"]
)
translation = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)
return translation
demo = gr.Interface(fn=translator, inputs=gr.Image(), outputs="text")
demo.launch(inline=False)