File size: 2,749 Bytes
f75f918 981c966 36b8f27 da94893 a4a299e b6888b8 a4a299e 36b8f27 b6888b8 36b8f27 b6888b8 36b8f27 981c966 36b8f27 981c966 36b8f27 981c966 36b8f27 b6888b8 36b8f27 b6888b8 981c966 36b8f27 b6888b8 |
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import os
os.environ["TRANSFORMERS_CACHE"] = "/app/.cache/huggingface"
os.environ["HF_HOME"] = "/app/.cache/huggingface"
os.environ["XDG_CACHE_HOME"] = "/app/.cache"
os.environ["XDG_CONFIG_HOME"] = "/app/.streamlit"
import torch
import torchaudio
from transformers import WhisperProcessor, WhisperForConditionalGeneration
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import streamlit as st
@st.cache_resource
def load_models():
whisper_processor = WhisperProcessor.from_pretrained("openai/whisper-tiny")
whisper_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny")
text_model = AutoModelForSequenceClassification.from_pretrained("Hate-speech-CNERG/dehatebert-mono-english")
tokenizer = AutoTokenizer.from_pretrained("Hate-speech-CNERG/dehatebert-mono-english")
return whisper_processor, whisper_model, text_model, tokenizer
whisper_processor, whisper_model, text_model, tokenizer = load_models()
def transcribe(audio_path):
waveform, sample_rate = torchaudio.load(audio_path)
if waveform.shape[0] > 1:
waveform = waveform.mean(dim=0, keepdim=True)
input_features = whisper_processor(waveform.squeeze().numpy(), sampling_rate=sample_rate, return_tensors="pt").input_features
predicted_ids = whisper_model.generate(input_features)
transcription = whisper_processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
return transcription
def extract_text_features(text):
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
outputs = text_model(**inputs)
prediction = outputs.logits.argmax(dim=1).item()
return prediction
def predict_hate_speech(audio_path=None, text=None):
if text:
text_input = text
elif audio_path:
transcription = transcribe(audio_path)
text_input = transcription
else:
return "Please provide either audio or text input."
prediction = extract_text_features(text_input)
return "Hate Speech" if prediction == 1 else "Not Hate Speech"
st.title("Hate Speech Detector with Audio and Text")
audio_file = st.file_uploader("Upload an audio file (wav, mp3, flac, ogg, opus)", type=["wav", "mp3", "flac", "ogg", "opus"])
text_input = st.text_input("Optional text input")
if st.button("Predict"):
if audio_file is not None:
temp_path = "temp_audio.wav"
with open(temp_path, "wb") as f:
f.write(audio_file.read())
prediction = predict_hate_speech(temp_path, text_input)
st.success(prediction)
elif text_input:
prediction = predict_hate_speech(text=text_input)
st.success(prediction)
else:
st.warning("Please provide at least audio or text input.")
|