Spaces:
Sleeping
Sleeping
siddhantuniyal
commited on
Commit
•
4593d5d
1
Parent(s):
8dacbdb
Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,55 @@
|
|
1 |
-
import easyocr
|
2 |
-
from gradio_client import Client, handle_file
|
3 |
-
import pandas as pd
|
4 |
-
import gradio as gr
|
5 |
-
|
6 |
-
clientImg = Client("dj-dawgs-ipd/IPD-Image-ViT-Finetune")
|
7 |
-
clientEngText = Client("dj-dawgs-ipd/IPD-Text-English-Finetune")
|
8 |
-
clientHingText = Client("dj-dawgs-ipd/IPD-Text-Hinglish")
|
9 |
-
|
10 |
-
profanity_df = pd.read_csv('Hinglish_Profanity_List.csv' , encoding = 'utf-8')
|
11 |
-
profanity_hn = profanity_df['profanity_hn']
|
12 |
-
|
13 |
-
def extract_text(image):
|
14 |
-
reader = easyocr.Reader(['en'])
|
15 |
-
data = [result[1] for result in reader.readtext(image)]
|
16 |
-
return ' '.join([l for l in data])
|
17 |
-
|
18 |
-
def predict(image):
|
19 |
-
|
20 |
-
imgResult = clientImg.predict(
|
21 |
-
image
|
22 |
-
api_name="/predict"
|
23 |
-
)
|
24 |
-
|
25 |
-
if float(imgResult[1]['label']) > 0.95:
|
26 |
-
return ["hate" , imgResult[0]]
|
27 |
-
else:
|
28 |
-
|
29 |
-
ocr_text = extract_text(image)
|
30 |
-
|
31 |
-
engResult = clientEngText.predict(
|
32 |
-
text=ocr_text,
|
33 |
-
api_name="/classify_text"
|
34 |
-
)
|
35 |
-
|
36 |
-
hingResult = clientHingText.predict(
|
37 |
-
text=ocr_text,
|
38 |
-
api_name="/predict"
|
39 |
-
)
|
40 |
-
|
41 |
-
if engResult[0] == "NEITHER" or hingResult[0] == "NAG":
|
42 |
-
return ["not_hate" , None]
|
43 |
-
else:
|
44 |
-
return ["hate" , None]
|
45 |
-
|
46 |
-
iface = gr.Interface(fn=predict,
|
47 |
-
inputs = gr.Image(type='pil'),
|
48 |
-
outputs=[gr.Label(label = "Class") , gr.Label(label = "Hate Symbol(if any)")],
|
49 |
-
title = "Hate Speech Detection in Image",
|
50 |
-
description = "Detect hateful symbols or text in Image"
|
51 |
-
)
|
52 |
-
|
53 |
-
if __name__ == "__main__":
|
54 |
-
iface.launch()
|
55 |
-
|
|
|
1 |
+
import easyocr
|
2 |
+
from gradio_client import Client, handle_file
|
3 |
+
import pandas as pd
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
clientImg = Client("dj-dawgs-ipd/IPD-Image-ViT-Finetune")
|
7 |
+
clientEngText = Client("dj-dawgs-ipd/IPD-Text-English-Finetune")
|
8 |
+
clientHingText = Client("dj-dawgs-ipd/IPD-Text-Hinglish")
|
9 |
+
|
10 |
+
profanity_df = pd.read_csv('Hinglish_Profanity_List.csv' , encoding = 'utf-8')
|
11 |
+
profanity_hn = profanity_df['profanity_hn']
|
12 |
+
|
13 |
+
def extract_text(image):
|
14 |
+
reader = easyocr.Reader(['en'])
|
15 |
+
data = [result[1] for result in reader.readtext(image)]
|
16 |
+
return ' '.join([l for l in data])
|
17 |
+
|
18 |
+
def predict(image):
|
19 |
+
|
20 |
+
imgResult = clientImg.predict(
|
21 |
+
image,
|
22 |
+
api_name="/predict"
|
23 |
+
)
|
24 |
+
|
25 |
+
if float(imgResult[1]['label']) > 0.95:
|
26 |
+
return ["hate" , imgResult[0]]
|
27 |
+
else:
|
28 |
+
|
29 |
+
ocr_text = extract_text(image)
|
30 |
+
|
31 |
+
engResult = clientEngText.predict(
|
32 |
+
text=ocr_text,
|
33 |
+
api_name="/classify_text"
|
34 |
+
)
|
35 |
+
|
36 |
+
hingResult = clientHingText.predict(
|
37 |
+
text=ocr_text,
|
38 |
+
api_name="/predict"
|
39 |
+
)
|
40 |
+
|
41 |
+
if engResult[0] == "NEITHER" or hingResult[0] == "NAG":
|
42 |
+
return ["not_hate" , None]
|
43 |
+
else:
|
44 |
+
return ["hate" , None]
|
45 |
+
|
46 |
+
iface = gr.Interface(fn=predict,
|
47 |
+
inputs = gr.Image(type='pil'),
|
48 |
+
outputs=[gr.Label(label = "Class") , gr.Label(label = "Hate Symbol(if any)")],
|
49 |
+
title = "Hate Speech Detection in Image",
|
50 |
+
description = "Detect hateful symbols or text in Image"
|
51 |
+
)
|
52 |
+
|
53 |
+
if __name__ == "__main__":
|
54 |
+
iface.launch()
|
55 |
+
|