File size: 2,016 Bytes
f762839
 
c248fad
43afcbf
cb17d2c
9d20783
f762839
 
 
b010e73
f762839
 
 
 
a8051c2
f762839
 
 
a8051c2
f762839
 
664dd28
f762839
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f6e32c
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
import gradio as gr
import easyocr
import transformers




reader=easyocr.Reader(['en'])
# this needs to run only once to load the model into memory
result=reader.readtext('https://huggingface.co/spaces/KAPtechies/Translation/blob/main/WhatsApp%20Image%202023-09-23%20at%208.03.28%20AM.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)