mojad121 commited on
Commit
709f046
·
verified ·
1 Parent(s): a8ec8f1

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +29 -25
src/streamlit_app.py CHANGED
@@ -1,14 +1,12 @@
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")
@@ -20,7 +18,11 @@ 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]
26
  return transcription
@@ -28,26 +30,28 @@ def transcribe(audio_path):
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.")
 
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
  os.environ["TRANSFORMERS_CACHE"] = "/app/.cache"
8
  os.environ["HF_HOME"] = "/app/.cache"
9
 
 
10
  def load_models():
11
  whisper_processor = WhisperProcessor.from_pretrained("openai/whisper-tiny")
12
  whisper_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny")
 
18
 
19
  def transcribe(audio_path):
20
  waveform, sample_rate = torchaudio.load(audio_path)
21
+ input_features = whisper_processor(
22
+ waveform.squeeze().numpy(),
23
+ sampling_rate=sample_rate,
24
+ return_tensors="pt"
25
+ ).input_features
26
  predicted_ids = whisper_model.generate(input_features)
27
  transcription = whisper_processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
28
  return transcription
 
30
  def extract_text_features(text):
31
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
32
  outputs = text_model(**inputs)
33
+ predicted_class = outputs.logits.argmax(dim=1).item()
34
+ return "Hate Speech" if predicted_class == 1 else "Not Hate Speech"
35
+
36
+ def predict(audio_file, text_input):
37
+ if not audio_file and not text_input:
38
+ return "Please provide either an audio file or some text."
39
+ if audio_file is not None:
40
+ audio_path = "temp_audio.wav"
41
+ with open(audio_path, "wb") as f:
42
+ f.write(audio_file.read())
43
+ transcribed_text = transcribe(audio_path)
44
+ prediction = extract_text_features(text_input or transcribed_text)
45
+ return f"Predicted: {prediction} \n\n(Transcribed: {transcribed_text})" if not text_input else f"Predicted: {prediction}"
46
+ elif text_input:
47
+ prediction = extract_text_features(text_input)
48
+ return f"Predicted: {prediction}"
49
+
50
+ st.title("Hate Speech Detector")
51
+
52
+ uploaded_audio = st.file_uploader("Upload Audio File (.mp3, .wav, .ogg, .flac, .opus)", type=["mp3", "wav", "ogg", "flac", "opus"])
53
+ text_input = st.text_input("Or enter text:")
54
 
 
 
 
55
  if st.button("Predict"):
56
+ result = predict(uploaded_audio, text_input)
57
+ st.success(result)