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