Update src/streamlit_app.py
Browse files- src/streamlit_app.py +20 -32
src/streamlit_app.py
CHANGED
@@ -1,29 +1,25 @@
|
|
1 |
-
import os
|
2 |
-
os.environ["TRANSFORMERS_CACHE"] = "/app/.cache/huggingface"
|
3 |
-
os.environ["HF_HOME"] = "/app/.cache/huggingface"
|
4 |
-
os.environ["XDG_CACHE_HOME"] = "/app/.cache"
|
5 |
-
os.environ["XDG_CONFIG_HOME"] = "/app/.streamlit"
|
6 |
-
|
7 |
import torch
|
8 |
import torchaudio
|
9 |
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
10 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
11 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
@st.cache_resource
|
14 |
def load_models():
|
15 |
whisper_processor = WhisperProcessor.from_pretrained("openai/whisper-tiny")
|
16 |
whisper_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny")
|
17 |
-
text_model = AutoModelForSequenceClassification.from_pretrained("
|
18 |
-
tokenizer = AutoTokenizer.from_pretrained("
|
19 |
return whisper_processor, whisper_model, text_model, tokenizer
|
20 |
|
21 |
whisper_processor, whisper_model, text_model, tokenizer = load_models()
|
22 |
|
23 |
def transcribe(audio_path):
|
24 |
waveform, sample_rate = torchaudio.load(audio_path)
|
25 |
-
if waveform.shape[0] > 1:
|
26 |
-
waveform = waveform.mean(dim=0, keepdim=True)
|
27 |
input_features = whisper_processor(waveform.squeeze().numpy(), sampling_rate=sample_rate, return_tensors="pt").input_features
|
28 |
predicted_ids = whisper_model.generate(input_features)
|
29 |
transcription = whisper_processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
|
@@ -32,34 +28,26 @@ def transcribe(audio_path):
|
|
32 |
def extract_text_features(text):
|
33 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
34 |
outputs = text_model(**inputs)
|
35 |
-
|
36 |
-
return prediction
|
37 |
-
|
38 |
-
def predict_hate_speech(audio_path=None, text=None):
|
39 |
-
if text:
|
40 |
-
text_input = text
|
41 |
-
elif audio_path:
|
42 |
-
transcription = transcribe(audio_path)
|
43 |
-
text_input = transcription
|
44 |
-
else:
|
45 |
-
return "Please provide either audio or text input."
|
46 |
|
|
|
|
|
|
|
47 |
prediction = extract_text_features(text_input)
|
48 |
return "Hate Speech" if prediction == 1 else "Not Hate Speech"
|
49 |
|
50 |
st.title("Hate Speech Detector with Audio and Text")
|
51 |
audio_file = st.file_uploader("Upload an audio file (wav, mp3, flac, ogg, opus)", type=["wav", "mp3", "flac", "ogg", "opus"])
|
52 |
text_input = st.text_input("Optional text input")
|
53 |
-
|
54 |
if st.button("Predict"):
|
55 |
-
if audio_file is not None:
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
prediction = predict_hate_speech(
|
63 |
st.success(prediction)
|
64 |
else:
|
65 |
-
st.warning("Please provide
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
import torchaudio
|
3 |
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
5 |
import streamlit as st
|
6 |
+
import os
|
7 |
+
|
8 |
+
os.environ["TRANSFORMERS_CACHE"] = "/app/.cache"
|
9 |
+
os.environ["HF_HOME"] = "/app/.cache"
|
10 |
+
|
11 |
|
|
|
12 |
def load_models():
|
13 |
whisper_processor = WhisperProcessor.from_pretrained("openai/whisper-tiny")
|
14 |
whisper_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny")
|
15 |
+
text_model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
17 |
return whisper_processor, whisper_model, text_model, tokenizer
|
18 |
|
19 |
whisper_processor, whisper_model, text_model, tokenizer = load_models()
|
20 |
|
21 |
def transcribe(audio_path):
|
22 |
waveform, sample_rate = torchaudio.load(audio_path)
|
|
|
|
|
23 |
input_features = whisper_processor(waveform.squeeze().numpy(), sampling_rate=sample_rate, return_tensors="pt").input_features
|
24 |
predicted_ids = whisper_model.generate(input_features)
|
25 |
transcription = whisper_processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
|
|
|
28 |
def extract_text_features(text):
|
29 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
30 |
outputs = text_model(**inputs)
|
31 |
+
return outputs.logits.argmax(dim=1).item()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
def predict_hate_speech(audio_path, text):
|
34 |
+
transcription = transcribe(audio_path)
|
35 |
+
text_input = text if text else transcription
|
36 |
prediction = extract_text_features(text_input)
|
37 |
return "Hate Speech" if prediction == 1 else "Not Hate Speech"
|
38 |
|
39 |
st.title("Hate Speech Detector with Audio and Text")
|
40 |
audio_file = st.file_uploader("Upload an audio file (wav, mp3, flac, ogg, opus)", type=["wav", "mp3", "flac", "ogg", "opus"])
|
41 |
text_input = st.text_input("Optional text input")
|
|
|
42 |
if st.button("Predict"):
|
43 |
+
if audio_file is not None or text_input:
|
44 |
+
audio_path = None
|
45 |
+
if audio_file is not None:
|
46 |
+
with open("temp_audio_input", "wb") as f:
|
47 |
+
f.write(audio_file.read())
|
48 |
+
audio_path = "temp_audio_input"
|
49 |
+
|
50 |
+
prediction = predict_hate_speech(audio_path, text_input) if audio_path else extract_text_features(text_input)
|
51 |
st.success(prediction)
|
52 |
else:
|
53 |
+
st.warning("Please provide either audio or text input.")
|