siddhantuniyal commited on
Commit
41e6737
1 Parent(s): 1af7581

feat: change response format to JSON

Browse files
Files changed (1) hide show
  1. app.py +43 -18
app.py CHANGED
@@ -22,14 +22,17 @@ def predict(image):
22
  api_name="/predict"
23
  )
24
 
25
- label , confidence = imgResult[0]['label'] , float(imgResult[1]['label'])
26
 
27
- if label == 'finger_gun_to_the_head' and confidence > 0.98:
28
- return ["hate" , f"label: {label}, confidence: {confidence}"]
29
- elif label != 'finger_gun_to_the_head' and confidence > 0.95:
30
- return ["hate" , f"label: {label}, confidence: {confidence}"]
 
 
 
 
31
  else:
32
-
33
  ocr_text = extract_text(image).lower()
34
 
35
  engResult = clientEngText.predict(
@@ -42,25 +45,47 @@ def predict(image):
42
  api_name="/predict"
43
  )
44
 
45
- profanityFound = any(word in ocr_text.split() for word in profanity_hn)
 
 
 
 
 
 
 
 
 
46
 
47
- if profanityFound:
48
- return ["hate", "Profanity Found"]
49
  elif engResult[0] != "NEITHER" and engResult[1] > 0.5:
50
- return ["hate", f"Result: {engResult}, Text: {ocr_text}"]
 
 
 
 
 
 
 
51
  elif hingResult[0] != "NAG" and hingResult[1] > 0.5:
52
- return ["hate", f"Result: {hingResult}, Text: {ocr_text}"]
 
 
 
 
 
 
 
53
  else:
54
- return ["not_hate", "No hate found, yay!"]
55
-
56
- # if not profanityFound and engResult[0] == "NEITHER" and hingResult[0] == "NAG":
57
- # return ["not_hate" , "No Hate Symbols Detected"]
58
- # else:
59
- # return ["hate" , "No Hate Symbols Detected"]
 
60
 
61
  iface = gr.Interface(fn=predict,
62
  inputs = gr.Image(type='filepath'),
63
- outputs=[gr.Label(label = "Class (hate or not_hate)") , gr.Label(label = "Explanation")],
64
  title = "Hate Speech Detection in Image",
65
  description = "Detect hateful symbols or text in Image"
66
  )
 
22
  api_name="/predict"
23
  )
24
 
25
+ label , confidence = imgResult[0]['label'] , float(imgResult[1]['label'])
26
 
27
+ if (label == 'finger_gun_to_the_head' and confidence > 0.98) or (label != 'finger_gun_to_the_head' and confidence > 0.95):
28
+ return {
29
+ "prediction" : "hate",
30
+ "language" : None,
31
+ "label" : label,
32
+ "confidence" : confidence,
33
+ "hate_text" : None
34
+ }
35
  else:
 
36
  ocr_text = extract_text(image).lower()
37
 
38
  engResult = clientEngText.predict(
 
45
  api_name="/predict"
46
  )
47
 
48
+ profanityFound = [word for word in profanity_hn if word in ocr_text[:200].split()]
49
+
50
+ if len(profanityFound) > 0:
51
+ return {
52
+ "prediction" : "hate",
53
+ "language" : "Hindi",
54
+ "label" : "Profanity Found",
55
+ "confidence" : None,
56
+ "hate_text" : profanityFound
57
+ }
58
 
 
 
59
  elif engResult[0] != "NEITHER" and engResult[1] > 0.5:
60
+ return {
61
+ "prediction" : "hate",
62
+ "language" : "English",
63
+ "label" : engResult[0],
64
+ "confidence" : engResult[1],
65
+ "hate_text" : ocr_text[:200]
66
+ }
67
+
68
  elif hingResult[0] != "NAG" and hingResult[1] > 0.5:
69
+ return {
70
+ "prediction" : "hate",
71
+ "language" : "Hinglish",
72
+ "label" : hingResult[0],
73
+ "confidence" : hingResult[1],
74
+ "hate_text" : ocr_text[:200]
75
+ }
76
+
77
  else:
78
+ return {
79
+ "prediction" : "not_hate",
80
+ "language" : None,
81
+ "label" : "No hate found, yay!",
82
+ "confidence" : None,
83
+ "hate_text" : None
84
+ }
85
 
86
  iface = gr.Interface(fn=predict,
87
  inputs = gr.Image(type='filepath'),
88
+ outputs=gr.JSON(),
89
  title = "Hate Speech Detection in Image",
90
  description = "Detect hateful symbols or text in Image"
91
  )