Ngadou commited on
Commit
c42e8bc
1 Parent(s): 4ce3169

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -16
app.py CHANGED
@@ -1,34 +1,45 @@
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)
 
1
  import gradio as gr
2
  import time
3
+ import openai
4
+ import json
5
+ import os
6
 
7
+ openai.api_key = os.environ.get('OPENAI_KEY')
 
 
 
 
 
8
 
9
  def classify_audio(audio):
10
  # Transcribe the audio to text
11
+ audio_transcript = asr_pipeline(audio)["text"]
12
+ audio_transcript = audio_transcript.lower()
13
+
14
+ messages = [
15
+ {"role": "system", "content": "Is this chat a scam, spam or is safe? Only answer in JSON format with 'classification': '' as string and 'reasons': '' as the most plausible reasons why. The reason should be explaning to the potential victim why the conversation is probably a scam"},
16
+ {"role": "user", "content": audio_transcript},
17
+ ]
18
+
19
+ # Call the OpenAI API to generate a response
20
+ response = openai.ChatCompletion.create(
21
+ model="gpt-4", # Replace with the actual GPT-4 model ID
22
+ messages=messages
23
+ )
24
 
25
+ # Extract the generated text
26
+ text = response.choices[0].message['content']
27
+ text = json.loads(text)
28
 
29
+ # Get the decision and reasons from the JSON dictionary
30
+ decision = text["classification"]
31
+ reasons = text["reasons"]
32
 
33
  # Return the transcription and the prediction as a dictionary
34
+ return audio_transcript, decision, reasons
35
 
36
  gr.Interface(
37
+ fn=classify_audio,
38
  inputs=gr.inputs.Audio(source="upload", type="filepath"),
39
  outputs=[
40
  gr.outputs.Textbox(label="Transcription"),
41
  gr.outputs.Textbox(label="Classification"),
42
+ gr.outputs.Textbox(label="Reason"),
43
  ],
44
  live=True
45
+ ).launch(share=True, debug=True)