Spaces:
Runtime error
Runtime error
Amulya M commited on
Commit ·
9fd7148
1
Parent(s): aa4671d
- app.py +52 -0
- function.py +37 -0
- hate_speech_data.csv +0 -0
- model.py +24 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# from pydub import AudioSegment
|
| 2 |
+
import function
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
st.title('New Post')
|
| 7 |
+
|
| 8 |
+
option = st.selectbox(
|
| 9 |
+
'Select the Media type',
|
| 10 |
+
('Choose', 'Text', 'Sound File', 'Voice'))
|
| 11 |
+
|
| 12 |
+
if option == 'Text':
|
| 13 |
+
pred = st.text_area('Text Area', height=70, max_chars=1000, help='Text area to enter the text',
|
| 14 |
+
placeholder='Enter the text')
|
| 15 |
+
col1, col2 = st.columns(2)
|
| 16 |
+
with col1:
|
| 17 |
+
a = st.button("Detect", key='ab', help='Click to check', disabled=False)
|
| 18 |
+
with col2:
|
| 19 |
+
b = st.button("Post", key='bb', help='Click to Post')
|
| 20 |
+
col3, col4 = st.columns(2)
|
| 21 |
+
de = function.detect([pred])
|
| 22 |
+
if a:
|
| 23 |
+
st.markdown(de[0])
|
| 24 |
+
if b:
|
| 25 |
+
if de[0] == "Offensive Language Detected":
|
| 26 |
+
st.write("Offensive Language Detected. Change the post and retry.")
|
| 27 |
+
else:
|
| 28 |
+
st.write("Posted Successfully.")
|
| 29 |
+
|
| 30 |
+
if option == 'Sound File':
|
| 31 |
+
fileObject = st.file_uploader(label="Please upload your file")
|
| 32 |
+
if fileObject:
|
| 33 |
+
col5, col6, col7 = st.columns(3)
|
| 34 |
+
with col5:
|
| 35 |
+
d = st.button("Preview", help="Click to preview")
|
| 36 |
+
with col6:
|
| 37 |
+
e = st.button("Detect")
|
| 38 |
+
with col7:
|
| 39 |
+
f = st.button("Post")
|
| 40 |
+
fg = function.detect([function.voice_file(fileObject.name)])
|
| 41 |
+
if d:
|
| 42 |
+
audio_file = open(fileObject.name, 'rb')
|
| 43 |
+
audio_bytes = audio_file.read()
|
| 44 |
+
st.audio(audio_bytes, format='audio/ogg')
|
| 45 |
+
if e:
|
| 46 |
+
st.write(fg[0])
|
| 47 |
+
|
| 48 |
+
if f:
|
| 49 |
+
if fg[0] == "Offensive Language Detected":
|
| 50 |
+
st.write("Offensive Language Detected. Change the post and retry.")
|
| 51 |
+
else:
|
| 52 |
+
st.write("Posted Successfully.")
|
function.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import model
|
| 2 |
+
def detect_hate_speech(sample):
|
| 3 |
+
vect = cv.transform(sample).toarray()
|
| 4 |
+
pred_prob = np.max(model.predict_proba(vect))
|
| 5 |
+
pred = model.predict(vect)
|
| 6 |
+
return pred, pred_prob
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def detect(sample):
|
| 10 |
+
hatespeech = detect_hate_speech(sample)
|
| 11 |
+
mp = "Try again"
|
| 12 |
+
if hatespeech[0] == 0:
|
| 13 |
+
mp = "Hate Speech Detected"
|
| 14 |
+
elif hatespeech[0] == 1:
|
| 15 |
+
mp = "Offensive Language Detected"
|
| 16 |
+
elif hatespeech[0] == 2:
|
| 17 |
+
mp = "NO Hate Speech Detected"
|
| 18 |
+
return mp, hatespeech[1], hatespeech[0]
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def voice_file(file):
|
| 22 |
+
speech_config = speechsdk.SpeechConfig(subscription=api_key, endpoint=endpoint)
|
| 23 |
+
speech_config.speech_recognition_language = "en-US"
|
| 24 |
+
speech_config.set_profanity(speechsdk.ProfanityOption.Raw)
|
| 25 |
+
audio_config = speechsdk.audio.AudioConfig(filename=file)
|
| 26 |
+
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
|
| 27 |
+
speech_recognition_result = speech_recognizer.recognize_once_async().get()
|
| 28 |
+
if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech:
|
| 29 |
+
print(speech_recognition_result.text)
|
| 30 |
+
return speech_recognition_result.text
|
| 31 |
+
elif speech_recognition_result.text == "":
|
| 32 |
+
return "$empty$"
|
| 33 |
+
elif speech_recognition_result.reason == speechsdk.ResultReason.NoMatch:
|
| 34 |
+
return speech_recognition_result.no_match_details
|
| 35 |
+
elif speech_recognition_result.reason == speechsdk.ResultReason.Canceled:
|
| 36 |
+
cancellation_details = speech_recognition_result.cancellation_details
|
| 37 |
+
return cancellation_details.reason
|
hate_speech_data.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
model.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
| 4 |
+
from sklearn.model_selection import train_test_split
|
| 5 |
+
|
| 6 |
+
# Speech to Text
|
| 7 |
+
api_key = '9283c8042f454c4d8638fca0484512ab'
|
| 8 |
+
endpoint = 'https://centralindia.api.cognitive.microsoft.com/sts/v1.0/issuetoken'
|
| 9 |
+
import azure.cognitiveservices.speech as speechsdk
|
| 10 |
+
# To ignore Warnings
|
| 11 |
+
import warnings
|
| 12 |
+
|
| 13 |
+
data = pd.read_csv('hate_speech_detection.csv')
|
| 14 |
+
X = data['raw_data']
|
| 15 |
+
y = data['class']
|
| 16 |
+
cv = CountVectorizer()
|
| 17 |
+
X_ = cv.fit_transform(X)
|
| 18 |
+
X_ = X_.toarray()
|
| 19 |
+
X_train, X_test, y_train, y_test = train_test_split(X_, y, test_size=0.4, random_state=True)
|
| 20 |
+
|
| 21 |
+
model = LogisticRegression(max_iter=1000)
|
| 22 |
+
model.fit(X_train,y_train)
|
| 23 |
+
y_pred = model.predict(X_test)
|
| 24 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
scikit-learn
|
| 3 |
+
azure-cognitiveservices-speech
|