Beijuka commited on
Commit
4480dae
·
verified ·
1 Parent(s): f9b7a32

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from model import ECAPA_gender
4
+
5
+ model = ECAPA_gender.from_pretrained("Beijuka/voice-gender-classifier")
6
+ model.eval()
7
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
+ model.to(device)
9
+
10
+ def predict_gender_confidence(audio_file):
11
+ if audio_file is None:
12
+ return "No audio provided"
13
+
14
+ try:
15
+ # Load audio
16
+ audio = model.load_audio(audio_file.name if hasattr(audio_file, "name") else audio_file)
17
+ audio = audio.to(device)
18
+
19
+ # Forward pass
20
+ with torch.no_grad():
21
+ logits = model.forward(audio)
22
+ probs = torch.softmax(logits, dim=1).cpu().numpy()[0]
23
+ pred_idx = logits.argmax(dim=1).item()
24
+ gender_pred = model.pred2gender[pred_idx].capitalize()
25
+ confidence = probs[pred_idx] * 100
26
+
27
+ return f"{gender_pred} — {confidence:.1f}% confidence"
28
+
29
+ except Exception as e:
30
+ return f"Error: {e}"
31
+
32
+ iface = gr.Interface(
33
+ fn=predict_gender_confidence,
34
+ inputs=gr.Audio(type="filepath", label="Upload audio file", sources=["upload"]),
35
+ outputs=gr.Textbox(label="Predicted Gender with Confidence"),
36
+ title="Voice Gender Classifier",
37
+ description="Upload an audio file and the model predicts speaker gender with confidence.",
38
+ allow_flagging="never"
39
+ )
40
+
41
+ iface.launch(share=True)