Update src/streamlit_app.py
Browse files- src/streamlit_app.py +19 -13
src/streamlit_app.py
CHANGED
@@ -1,23 +1,30 @@
|
|
1 |
import torch
|
2 |
import torchaudio
|
3 |
import os
|
4 |
-
import streamlit as st
|
5 |
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
6 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
|
7 |
|
8 |
os.environ["TRANSFORMERS_CACHE"] = "/app/.cache/huggingface"
|
9 |
os.environ["HF_HOME"] = "/app/.cache/huggingface"
|
10 |
os.environ["TORCH_HOME"] = "/app/.cache/torch"
|
11 |
hf_token = os.getenv("HateSpeechMujtabatoken")
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
whisper_processor
|
14 |
-
whisper_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny", token=hf_token)
|
15 |
-
text_model = AutoModelForSequenceClassification.from_pretrained("GroNLP/hateBERT", token=hf_token)
|
16 |
-
tokenizer = AutoTokenizer.from_pretrained("GroNLP/hateBERT", token=hf_token)
|
17 |
|
18 |
def transcribe(audio_path):
|
19 |
waveform, sample_rate = torchaudio.load(audio_path)
|
20 |
-
input_features = whisper_processor(
|
|
|
|
|
|
|
|
|
21 |
predicted_ids = whisper_model.generate(input_features)
|
22 |
transcription = whisper_processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
|
23 |
return transcription
|
@@ -31,23 +38,22 @@ def extract_text_features(text):
|
|
31 |
def predict(audio_file, text_input):
|
32 |
if not audio_file and not text_input:
|
33 |
return "Please provide either an audio file or some text."
|
34 |
-
if audio_file:
|
35 |
audio_path = "temp_audio.wav"
|
36 |
with open(audio_path, "wb") as f:
|
37 |
f.write(audio_file.read())
|
38 |
transcribed_text = transcribe(audio_path)
|
39 |
prediction = extract_text_features(text_input or transcribed_text)
|
40 |
-
if text_input:
|
41 |
-
|
42 |
-
else:
|
43 |
-
return f"Predicted: {prediction} \n\n(Transcribed: {transcribed_text})"
|
44 |
-
else:
|
45 |
prediction = extract_text_features(text_input)
|
46 |
return f"Predicted: {prediction}"
|
47 |
|
48 |
st.title("Hate Speech Detector")
|
|
|
49 |
uploaded_audio = st.file_uploader("Upload Audio File (.mp3, .wav, .ogg, .flac, .opus)", type=["mp3", "wav", "ogg", "flac", "opus"])
|
50 |
text_input = st.text_input("Or enter text:")
|
|
|
51 |
if st.button("Predict"):
|
52 |
result = predict(uploaded_audio, text_input)
|
53 |
-
st.success(result)
|
|
|
1 |
import torch
|
2 |
import torchaudio
|
3 |
import os
|
|
|
4 |
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
5 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
6 |
+
import streamlit as st
|
7 |
|
8 |
os.environ["TRANSFORMERS_CACHE"] = "/app/.cache/huggingface"
|
9 |
os.environ["HF_HOME"] = "/app/.cache/huggingface"
|
10 |
os.environ["TORCH_HOME"] = "/app/.cache/torch"
|
11 |
hf_token = os.getenv("HateSpeechMujtabatoken")
|
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(
|
24 |
+
waveform.squeeze().numpy(),
|
25 |
+
sampling_rate=sample_rate,
|
26 |
+
return_tensors="pt"
|
27 |
+
).input_features
|
28 |
predicted_ids = whisper_model.generate(input_features)
|
29 |
transcription = whisper_processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
|
30 |
return transcription
|
|
|
38 |
def predict(audio_file, text_input):
|
39 |
if not audio_file and not text_input:
|
40 |
return "Please provide either an audio file or some text."
|
41 |
+
if audio_file is not None:
|
42 |
audio_path = "temp_audio.wav"
|
43 |
with open(audio_path, "wb") as f:
|
44 |
f.write(audio_file.read())
|
45 |
transcribed_text = transcribe(audio_path)
|
46 |
prediction = extract_text_features(text_input or transcribed_text)
|
47 |
+
return f"Predicted: {prediction} \n\n(Transcribed: {transcribed_text})" if not text_input else f"Predicted: {prediction}"
|
48 |
+
elif text_input:
|
|
|
|
|
|
|
49 |
prediction = extract_text_features(text_input)
|
50 |
return f"Predicted: {prediction}"
|
51 |
|
52 |
st.title("Hate Speech Detector")
|
53 |
+
|
54 |
uploaded_audio = st.file_uploader("Upload Audio File (.mp3, .wav, .ogg, .flac, .opus)", type=["mp3", "wav", "ogg", "flac", "opus"])
|
55 |
text_input = st.text_input("Or enter text:")
|
56 |
+
|
57 |
if st.button("Predict"):
|
58 |
result = predict(uploaded_audio, text_input)
|
59 |
+
st.success(result)
|