Update src/streamlit_app.py
Browse files- src/streamlit_app.py +4 -23
src/streamlit_app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
|
2 |
import os
|
3 |
import torch
|
4 |
import torchaudio
|
@@ -7,6 +6,9 @@ from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
|
7 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
8 |
import streamlit as st
|
9 |
|
|
|
|
|
|
|
10 |
whisper_processor = WhisperProcessor.from_pretrained("openai/whisper-tiny")
|
11 |
whisper_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny")
|
12 |
text_model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
|
@@ -28,25 +30,4 @@ def extract_text_features(text):
|
|
28 |
outputs = text_model(**inputs)
|
29 |
return outputs.logits.argmax(dim=1).item()
|
30 |
|
31 |
-
def
|
32 |
-
if audio_bytes:
|
33 |
-
transcription = transcribe(audio_bytes)
|
34 |
-
text_input = text if text else transcription
|
35 |
-
elif text:
|
36 |
-
text_input = text
|
37 |
-
else:
|
38 |
-
return "Please provide audio or text"
|
39 |
-
prediction = extract_text_features(text_input)
|
40 |
-
return "Hate Speech" if prediction == 1 else "Not Hate Speech"
|
41 |
-
|
42 |
-
st.title("Hate Speech Detection")
|
43 |
-
audio_file = st.file_uploader("Upload audio file", type=["wav", "mp3", "flac", "ogg", "opus"])
|
44 |
-
text_input = st.text_input("Or enter text")
|
45 |
-
|
46 |
-
if st.button("Predict"):
|
47 |
-
if audio_file is not None or text_input:
|
48 |
-
audio_bytes = audio_file.read() if audio_file else None
|
49 |
-
result = predict_hate_speech(audio_bytes, text_input)
|
50 |
-
st.success(result)
|
51 |
-
else:
|
52 |
-
st.warning("Please provide either audio or text input")
|
|
|
|
|
1 |
import os
|
2 |
import torch
|
3 |
import torchaudio
|
|
|
6 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
7 |
import streamlit as st
|
8 |
|
9 |
+
os.environ["TRANSFORMERS_CACHE"] = "/app/cache"
|
10 |
+
os.makedirs("/app/cache", exist_ok=True)
|
11 |
+
|
12 |
whisper_processor = WhisperProcessor.from_pretrained("openai/whisper-tiny")
|
13 |
whisper_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny")
|
14 |
text_model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
|
|
|
30 |
outputs = text_model(**inputs)
|
31 |
return outputs.logits.argmax(dim=1).item()
|
32 |
|
33 |
+
def predict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|