File size: 2,309 Bytes
4593d5d
 
 
 
 
 
 
 
 
 
 
 
 
 
9fff2e5
 
4593d5d
 
113622c
4593d5d
7a53eba
113622c
4593d5d
 
7a53eba
 
d8a7f48
 
4593d5d
113622c
9c28001
4593d5d
 
 
28ad0c2
4593d5d
 
 
 
 
 
cb6ff81
0ff50a3
d8a7f48
 
 
90c3f92
e16a35a
90c3f92
e16a35a
4593d5d
d8a7f48
 
 
 
 
 
4593d5d
 
1eff44d
28ad0c2
4593d5d
 
 
 
 
0ff50a3
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
64
65
66
67
import easyocr 
from gradio_client import Client, handle_file
import pandas as pd
import gradio as gr

clientImg = Client("dj-dawgs-ipd/IPD-Image-ViT-Finetune")
clientEngText = Client("dj-dawgs-ipd/IPD-Text-English-Finetune")
clientHingText = Client("dj-dawgs-ipd/IPD-Text-Hinglish")

profanity_df = pd.read_csv('Hinglish_Profanity_List.csv' , encoding = 'utf-8')
profanity_hn = profanity_df['profanity_hn']

def extract_text(image):
    reader = easyocr.Reader(['en'])
    data = [result[1] for result in reader.readtext(image)]
    return ' '.join([l for l in data])

def predict(image):
    
    imgResult = clientImg.predict(
        image=handle_file(image),
        api_name="/predict"
    )
    
    label , confidence = imgResult[0]['label'] , float(imgResult[1]['label'])

    if confidence > 0.95:
        return ["hate" , f"label: {label}, confidence: {confidence}"]
    else:
 
        ocr_text = extract_text(image).lower()
        
        engResult = clientEngText.predict(
		text=ocr_text,
		api_name="/predict"
        )
        
        hingResult = clientHingText.predict(
        		text=ocr_text,
        		api_name="/predict"
        )

        profanityFound = any(word in ocr_text.split() for word in profanity_hn)

        if profanityFound:
            return ["hate", "Profanity Found"]
        elif engResult[0] != "NEITHER" or engResult[1] < 0.5:
            return ["hate", f"Result: {engResult}, Text: {ocr_text}"]
        elif hingResult[0] != "NAG" or hingResult[1] < 0.5:
            return ["hate", f"Result: {hingResult}, Text: {ocr_text}"]
        else:
            return ["not_hate", "No hate found, yay!"]
        
        # if not profanityFound and engResult[0] == "NEITHER" and hingResult[0] == "NAG":
        #     return ["not_hate" , "No Hate Symbols Detected"]
        # else:
        #     return ["hate" , "No Hate Symbols Detected"]
        
iface = gr.Interface(fn=predict,
                     inputs = gr.Image(type='filepath'),
                     outputs=[gr.Label(label = "Class (hate or not_hate)") , gr.Label(label = "Explanation")],
                     title = "Hate Speech Detection in Image",
                     description = "Detect hateful symbols or text in Image"
        )
                     
if __name__ == "__main__":
    iface.launch()