omarelsayeed commited on
Commit
81ff5e6
·
1 Parent(s): d212562

add app file

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py CHANGED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from fastai.vision.all import *
3
+ from PIL import ImageDraw , ImageFont
4
+ model = load_learner('lang_model.pkl')
5
+ categories = model.dls.vocab
6
+
7
+ def process_input(text):
8
+ width = 255
9
+ height = 255
10
+ n=40
11
+ 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 img
17
+
18
+ def 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
+
27
+ 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")
28
+ label = gr.outputs.Label()
29
+ examples = [['dasd.jpg',''] , ['hindi.jpg' , ''] , ['arabic.png' ,'']]
30
+ image = gr.inputs.Image(shape=(194,194) , image_mode = "L")
31
+ intf = gr.Interface(fn = classify_img , inputs = [image,text_input ] , outputs = label , examples = examples)
32
+ intf.launch(share = True)