shubhamjaiswar commited on
Commit
78825f3
·
verified ·
1 Parent(s): 951800d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -102
app.py CHANGED
@@ -1,102 +1,103 @@
1
- from gradio_client import Client, handle_file
2
- import pandas as pd
3
- import gradio as gr
4
- from vosk import Model, KaldiRecognizer
5
- import json
6
- import wave
7
-
8
- clientEngText = Client("dj-dawgs-ipd/IPD-Text-English-Finetune")
9
- clientHingText = Client("dj-dawgs-ipd/IPD-Text-Hinglish")
10
- clientAud = Client("dj-dawgs-ipd/IPD_Audio_HuBERT")
11
-
12
- profanity_df = pd.read_csv('Hinglish_Profanity_List.csv', encoding='utf-8')
13
- profanity_hn = profanity_df['profanity_hn']
14
- vosk_model = Model(lang="en-us")
15
-
16
-
17
- # import whisper
18
- # def stt_whisper(file_path):
19
- # model = whisper.load_model("base")
20
- # try:
21
- # result = model.transcribe(file_path)
22
- # return result["text"]
23
- # except Exception as e:
24
- # print(e)
25
- # return ""
26
-
27
-
28
- def stt_vosk(file_path):
29
- try:
30
- wf = wave.open(file_path, "rb")
31
- rec = KaldiRecognizer(vosk_model, wf.getframerate())
32
- rec.SetWords(True)
33
- rec.SetPartialWords(True)
34
- while True:
35
- data = wf.readframes(4000)
36
- if len(data) == 0:
37
- break
38
- rec.AcceptWaveform(data)
39
- data = json.loads(rec.FinalResult())
40
- return data["text"]
41
- except:
42
- return ""
43
-
44
-
45
- def extract_text(audio_path):
46
- return stt_vosk(audio_path).lower()
47
-
48
-
49
- def predict_hate_speech(audio_path):
50
-
51
- audResult = clientAud.predict(
52
- audio_path=handle_file(audio_path),
53
- api_name="/predict"
54
- )
55
- audResult = json.loads(audResult.replace("'", '"'))
56
-
57
- stt_text = extract_text(audio_path)
58
-
59
- engResult = clientEngText.predict(
60
- text=stt_text[:200],
61
- api_name="/predict"
62
- )
63
-
64
- hingResult = clientHingText.predict(
65
- text=stt_text[:200],
66
- api_name="/predict"
67
- )
68
-
69
- profanityFound = any(word in stt_text.split() for word in profanity_hn)
70
- threshold = 0.6
71
- isHate = (engResult[0] != "NEITHER" and engResult[1] > threshold) or (
72
- hingResult[0] != "NAG" and hingResult[1] > threshold) or (
73
- audResult['Classification'] == 'Hate Speech\n' and audResult['Confidence'] > threshold)
74
-
75
- engConf = engResult[1] if engResult[0] != "NEITHER" else (1 - engResult[1])
76
- hingConf = hingResult[1] if hingResult[0] != "NEITHER" else (1 - hingResult[1])
77
- audConf = audResult['Confidence'] if audResult['Classification'] == 'Hate Speech\n' else (1 - audResult['Confidence'])
78
-
79
- confidence = (engConf + hingConf + audConf) / 3
80
- # print(profanityFound, engResult, hingResult, audResult)
81
- if profanityFound:
82
- return ["hate", f"Result: Profanity Found", f"Text: {stt_text}"]
83
- elif isHate:
84
- return ["hate", f"Confidence: {confidence}", f"Text: {stt_text}"]
85
-
86
- return ["not_hate", "No hate found, yay!"]
87
-
88
-
89
- iface = gr.Interface(
90
- fn=predict_hate_speech,
91
- inputs=gr.Audio(type="filepath", label="Upload Audio"),
92
- outputs=gr.Textbox(label="Hate Speech Analysis"),
93
- title="Hate Speech Audio Pipeline",
94
- description="Upload an audio file to detect potential hate speech content.",
95
- examples=[
96
- ["hate_video_3_3_snippet2.wav"]
97
- ],
98
- allow_flagging="manual"
99
- )
100
-
101
- if __name__ == "__main__":
102
- iface.launch()
 
 
1
+ from gradio_client import Client, handle_file
2
+ import pandas as pd
3
+ import gradio as gr
4
+ from vosk import Model, KaldiRecognizer
5
+ import json
6
+ import wave
7
+
8
+ clientEngText = Client("dj-dawgs-ipd/IPD-Text-English-Finetune")
9
+ clientHingText = Client("dj-dawgs-ipd/IPD-Text-Hinglish")
10
+ clientAud = Client("dj-dawgs-ipd/IPD_Audio_HuBERT")
11
+
12
+ profanity_df = pd.read_csv('Hinglish_Profanity_List.csv', encoding='utf-8')
13
+ profanity_hn = profanity_df['profanity_hn']
14
+ vosk_model = Model(lang="en-us")
15
+
16
+
17
+ # import whisper
18
+ # def stt_whisper(file_path):
19
+ # model = whisper.load_model("base")
20
+ # try:
21
+ # result = model.transcribe(file_path)
22
+ # return result["text"]
23
+ # except Exception as e:
24
+ # print(e)
25
+ # return ""
26
+
27
+
28
+ def stt_vosk(file_path):
29
+ try:
30
+ wf = wave.open(file_path, "rb")
31
+ rec = KaldiRecognizer(vosk_model, wf.getframerate())
32
+ rec.SetWords(True)
33
+ rec.SetPartialWords(True)
34
+ while True:
35
+ data = wf.readframes(4000)
36
+ if len(data) == 0:
37
+ break
38
+ rec.AcceptWaveform(data)
39
+ data = json.loads(rec.FinalResult())
40
+ return data["text"]
41
+ except:
42
+ return ""
43
+
44
+
45
+ def extract_text(audio_path):
46
+ return stt_vosk(audio_path).lower()
47
+
48
+
49
+ def predict_hate_speech(audio_path):
50
+
51
+ audResult = clientAud.predict(
52
+ audio_path=handle_file(audio_path),
53
+ api_name="/predict"
54
+ )
55
+ audResult = json.loads(audResult.replace("'", '"'))
56
+
57
+ stt_text = extract_text(audio_path)
58
+
59
+ engResult = clientEngText.predict(
60
+ text=stt_text[:200],
61
+ api_name="/predict"
62
+ )
63
+
64
+ hingResult = clientHingText.predict(
65
+ text=stt_text[:200],
66
+ api_name="/predict"
67
+ )
68
+
69
+ profanityFound = any(word in stt_text.split() for word in profanity_hn)
70
+ threshold = 0.6
71
+ isHate = (engResult[0] != "NEITHER" and engResult[1] > threshold) or (
72
+ hingResult[0] != "NAG" and hingResult[1] > threshold) or (
73
+ audResult['Classification'] == 'Hate Speech\n' and audResult['Confidence'] > threshold)
74
+
75
+ engConf = engResult[1] if engResult[0] != "NEITHER" else (1 - engResult[1])
76
+ hingConf = hingResult[1] if hingResult[0] != "NEITHER" else (1 - hingResult[1])
77
+ audConf = audResult['Confidence'] if audResult['Classification'] == 'Hate Speech\n' else (1 - audResult['Confidence'])
78
+
79
+ confidence = (engConf + hingConf + audConf) / 3
80
+ # print(profanityFound, engResult, hingResult, audResult)
81
+ if profanityFound:
82
+ return ["hate", f"Result: Profanity Found", f"Text: {stt_text}"]
83
+ elif isHate:
84
+ return ["hate", f"Confidence: {confidence}", f"Text: {stt_text}"]
85
+
86
+ return ["not_hate", "No hate found, yay!"]
87
+
88
+
89
+ iface = gr.Interface(
90
+ fn=predict_hate_speech,
91
+ inputs=gr.Audio(type="filepath", label="Upload Audio"),
92
+ outputs=gr.Textbox(label="Hate Speech Analysis"),
93
+ title="Hate Speech Audio Pipeline",
94
+ description="Upload an audio file to detect potential hate speech content.",
95
+ examples=[
96
+ ["hate_1.wav"],
97
+ ["hate_2.wav"]
98
+ ],
99
+ allow_flagging="manual"
100
+ )
101
+
102
+ if __name__ == "__main__":
103
+ iface.launch()