omarelsayeed/FromImageAndTextLanguageDetector
0
1import gradio as gr2from fastai.vision.all import *3from PIL import ImageDraw , ImageFont4model = load_learner('lang_model.pkl')5categories = model.dls.vocab6 7def process_input(text):8 width = 2559 height = 25510 n=4011 splitted_string = [text[i:i+n] for i in range(0, len(text), n)]12 text = ' \n '.join(splitted_string)13 img = Image.new('L', (width, height), color='white')14 imgDraw = ImageDraw.Draw(img)15 imgDraw.text((10, 10), text, fill=(0) , font=ImageFont.truetype("arial-unicode-ms.ttf", 10 ,layout_engine=ImageFont.LAYOUT_RAQM ,encoding='utf-8'))16 return img17 18def classify_img(im,text):19 if text.strip() != '':20 img = process_input(text)21 pred , idx , probs = model.predict(PILImage(process_input(text)))22 return dict(zip(categories , map(float , probs)))23 else : 24 pred , idx , probs = model.predict(im)25 return dict(zip(categories , map(float , probs)))26 27text_input = gr.inputs.Textbox(lines=3 , placeholder="You can only put in a text or an img , else you will get an error! , if there's text we ignore the image")28label = gr.outputs.Label()29examples = [['dasd.jpg',''] , ['hindi.jpg' , ''] , ['arabic.png' ,'']]30image = gr.inputs.Image(shape=(194,194) , image_mode = "L")31intf = gr.Interface(fn = classify_img , inputs = [image,text_input ] , outputs = label , examples = examples)32intf.launch(inline = False)