File size: 1,586 Bytes
9e9e397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import model
def detect_hate_speech(sample):
    vect = cv.transform(sample).toarray()
    pred_prob = np.max(model.predict_proba(vect))
    pred = model.predict(vect)
    return pred, pred_prob


def detect(sample):
    hatespeech = detect_hate_speech(sample)
    mp = "Try again"
    if hatespeech[0] == 0:
        mp = "Hate Speech Detected"
    elif hatespeech[0] == 1:
        mp = "Offensive Language Detected"
    elif hatespeech[0] == 2:
        mp = "NO Hate Speech Detected"
    return mp, hatespeech[1], hatespeech[0]


def voice_file(file):
    speech_config = speechsdk.SpeechConfig(subscription=api_key, endpoint=endpoint)
    speech_config.speech_recognition_language = "en-US"
    speech_config.set_profanity(speechsdk.ProfanityOption.Raw)
    audio_config = speechsdk.audio.AudioConfig(filename=file)
    speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
    speech_recognition_result = speech_recognizer.recognize_once_async().get()
    if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech:
        print(speech_recognition_result.text)
        return speech_recognition_result.text
    elif speech_recognition_result.text == "":
        return "$empty$"
    elif speech_recognition_result.reason == speechsdk.ResultReason.NoMatch:
        return speech_recognition_result.no_match_details
    elif speech_recognition_result.reason == speechsdk.ResultReason.Canceled:
        cancellation_details = speech_recognition_result.cancellation_details
        return cancellation_details.reason