mojad121 commited on
Commit
118e098
·
verified ·
1 Parent(s): 9d6250b

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +39 -17
src/streamlit_app.py CHANGED
@@ -1,33 +1,55 @@
1
- import os
2
  import torch
3
  import torchaudio
4
- import tempfile
5
  from transformers import WhisperProcessor, WhisperForConditionalGeneration
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")
15
- tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
16
 
17
- def transcribe(audio_bytes):
18
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
19
- tmp.write(audio_bytes)
20
- tmp_path = tmp.name
21
- waveform, sample_rate = torchaudio.load(tmp_path)
22
- input_features = whisper_processor(waveform.squeeze().numpy(), sampling_rate=sample_rate, return_tensors="pt").input_features
 
23
  predicted_ids = whisper_model.generate(input_features)
24
  transcription = whisper_processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
25
- os.remove(tmp_path)
26
  return transcription
27
 
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
 
 
 
 
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
+ def load_models():
9
+ whisper_processor = WhisperProcessor.from_pretrained("openai/whisper-tiny")
10
+ whisper_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny")
11
+ text_model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
12
+ tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
13
+ return whisper_processor, whisper_model, text_model, tokenizer
14
 
15
+ whisper_processor, whisper_model, text_model, tokenizer = load_models()
 
 
 
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
27
 
28
  def extract_text_features(text):
29
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
30
  outputs = text_model(**inputs)
31
+ predicted_class = outputs.logits.argmax(dim=1).item()
32
+ return "Hate Speech" if predicted_class == 1 else "Not Hate Speech"
33
+
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 is not None:
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
+ return f"Predicted: {prediction} \n\n(Transcribed: {transcribed_text})" if not text_input else f"Predicted: {prediction}"
44
+ elif text_input:
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)