Ngadou commited on
Commit
396fa06
1 Parent(s): dbee56f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import time
3
+ from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
4
+
5
+ # ASR pipeline
6
+ asr_pipeline = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")
7
+
8
+ # Load classifier model and tokenizer
9
+ classifier_model = AutoModelForSequenceClassification.from_pretrained("Ngadou/bert-sms-spam-dectector")
10
+ classifier_tokenizer = AutoTokenizer.from_pretrained("Ngadou/bert-sms-spam-dectector")
11
+
12
+ def classify_audio(audio):
13
+ # Transcribe the audio to text
14
+ text = asr_pipeline(audio)["text"]
15
+
16
+ # Tokenize the text and feed it to the model
17
+ inputs = classifier_tokenizer.encode_plus(text, return_tensors="pt")
18
+ outputs = classifier_model(**inputs)
19
+
20
+ # Get the prediction (0 = ham, 1 = spam)
21
+ prediction = outputs.logits.argmax(dim=1).item()
22
+
23
+ # Return the transcription and the prediction as a dictionary
24
+ return text, "Scam" if prediction == 1 else "Safe Message"
25
+
26
+ gr.Interface(
27
+ fn=classify_audio,
28
+ inputs=gr.inputs.Audio(source="upload", type="filepath"),
29
+ outputs=[
30
+ gr.outputs.Textbox(label="Transcription"),
31
+ gr.outputs.Textbox(label="Classification"),
32
+ ],
33
+ live=True
34
+ ).launch(share=True)