arham061 commited on
Commit
38c2fb5
1 Parent(s): 538482c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -26
app.py CHANGED
@@ -1,31 +1,18 @@
1
- import gradio as gr
2
- from transformers import BertTokenizer, BertForSequenceClassification
3
- import torch
4
-
5
- # Load the model and tokenizer from the folder in Hugging Face space
6
- model_folder = "FYP_Model" # Replace with your actual username and model name
7
- tokenizer = BertTokenizer.from_pretrained(model_folder)
8
- model = BertForSequenceClassification.from_pretrained(model_folder)
9
 
10
- def classify_audio(audio_file):
11
- # Read the audio file and tokenize it
12
- audio_content = audio_file.read()
13
- inputs = tokenizer(audio_content, return_tensors="pt", truncation=True)
14
 
15
- # Perform inference using the loaded model
16
- outputs = model(**inputs)
17
- logits = outputs.logits
18
- predicted_class = torch.argmax(logits).item()
 
 
19
 
20
- return f"Predicted class: {predicted_class}"
21
 
22
- # Gradio Interface
23
- iface = gr.Interface(
24
- fn=classify_audio,
25
- inputs=gr.Audio(type="file", label="Upload or Record Audio"),
26
- outputs=gr.Textbox(),
27
- live=True,
28
  )
29
-
30
- # Launch the Gradio interface
31
- iface.launch()
 
1
+ from transformers import pipeline
 
 
 
 
 
 
 
2
 
3
+ model_id = "arham061/distilhubert-finetuned-RHD_Dataset"
4
+ pipe = pipeline("audio-classification", model=model_id)
 
 
5
 
6
+ def classify_audio(filepath):
7
+ preds = pipe(filepath)
8
+ outputs = {}
9
+ for p in preds:
10
+ outputs[p["label"]] = p["score"]
11
+ return outputs
12
 
13
+ import gradio as gr
14
 
15
+ demo = gr.Interface(
16
+ fn=classify_audio, inputs=gr.Audio(type="filepath"), outputs="label"
 
 
 
 
17
  )
18
+ demo.launch(debug=True)