mojad121 commited on
Commit
b6888b8
·
verified ·
1 Parent(s): 981c966

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +10 -6
src/streamlit_app.py CHANGED
@@ -14,14 +14,16 @@ import streamlit as st
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("bert-base-uncased")
18
- tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
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
  input_features = whisper_processor(waveform.squeeze().numpy(), sampling_rate=sample_rate, return_tensors="pt").input_features
26
  predicted_ids = whisper_model.generate(input_features)
27
  transcription = whisper_processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
@@ -30,7 +32,8 @@ def transcribe(audio_path):
30
  def extract_text_features(text):
31
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
32
  outputs = text_model(**inputs)
33
- return outputs.logits.argmax(dim=1).item()
 
34
 
35
  def predict_hate_speech(audio_path=None, text=None):
36
  if text:
@@ -50,12 +53,13 @@ text_input = st.text_input("Optional text input")
50
 
51
  if st.button("Predict"):
52
  if audio_file is not None:
53
- with open("temp_audio", "wb") as f:
 
54
  f.write(audio_file.read())
55
- prediction = predict_hate_speech("temp_audio", text_input)
56
  st.success(prediction)
57
  elif text_input:
58
  prediction = predict_hate_speech(text=text_input)
59
  st.success(prediction)
60
  else:
61
- st.warning("Please provide at least audio or text input.")
 
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("Hate-speech-CNERG/dehatebert-mono-english")
18
+ tokenizer = AutoTokenizer.from_pretrained("Hate-speech-CNERG/dehatebert-mono-english")
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
  def extract_text_features(text):
33
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
34
  outputs = text_model(**inputs)
35
+ prediction = outputs.logits.argmax(dim=1).item()
36
+ return prediction
37
 
38
  def predict_hate_speech(audio_path=None, text=None):
39
  if text:
 
53
 
54
  if st.button("Predict"):
55
  if audio_file is not None:
56
+ temp_path = "temp_audio.wav"
57
+ with open(temp_path, "wb") as f:
58
  f.write(audio_file.read())
59
+ prediction = predict_hate_speech(temp_path, text_input)
60
  st.success(prediction)
61
  elif text_input:
62
  prediction = predict_hate_speech(text=text_input)
63
  st.success(prediction)
64
  else:
65
+ st.warning("Please provide at least audio or text input.")