omarelsayeed's picture
Update app.py
963e38a
import gradio as gr
from fastai.vision.all import *
from PIL import ImageDraw , ImageFont
model = load_learner('lang_model.pkl')
categories = model.dls.vocab
def process_input(text):
width = 255
height = 255
n=40
splitted_string = [text[i:i+n] for i in range(0, len(text), n)]
text = ' \n '.join(splitted_string)
img = Image.new('L', (width, height), color='white')
imgDraw = ImageDraw.Draw(img)
imgDraw.text((10, 10), text, fill=(0) , font=ImageFont.truetype("arial-unicode-ms.ttf", 10 ,layout_engine=ImageFont.LAYOUT_RAQM ,encoding='utf-8'))
return img
def classify_img(im,text):
if text.strip() != '':
img = process_input(text)
pred , idx , probs = model.predict(PILImage(process_input(text)))
return dict(zip(categories , map(float , probs)))
else :
pred , idx , probs = model.predict(im)
return dict(zip(categories , map(float , probs)))
text_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")
label = gr.outputs.Label()
examples = [['dasd.jpg',''] , ['hindi.jpg' , ''] , ['arabic.png' ,'']]
image = gr.inputs.Image(shape=(194,194) , image_mode = "L")
intf = gr.Interface(fn = classify_img , inputs = [image,text_input ] , outputs = label , examples = examples)
intf.launch(inline = False)