Delete inference.py
Browse files- inference.py +0 -37
inference.py
DELETED
|
@@ -1,37 +0,0 @@
|
|
| 1 |
-
# inference.py
|
| 2 |
-
import torch
|
| 3 |
-
import numpy as np
|
| 4 |
-
import librosa
|
| 5 |
-
from transformers import Wav2Vec2FeatureExtractor, Wav2Vec2ForSequenceClassification
|
| 6 |
-
|
| 7 |
-
# Load model and feature extractor
|
| 8 |
-
model_name = "path_or_hub_id_of_your_finetuned_model" # e.g. "username/wav2vec2-accuracy-classifier"
|
| 9 |
-
model = Wav2Vec2ForSequenceClassification.from_pretrained(model_name)
|
| 10 |
-
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(model_name)
|
| 11 |
-
|
| 12 |
-
# Put model in eval mode
|
| 13 |
-
model.eval()
|
| 14 |
-
|
| 15 |
-
def predict_accuracy_level(audio_path: str):
|
| 16 |
-
# 1. Load raw audio using librosa (or similar)
|
| 17 |
-
speech, sr = librosa.load(audio_path, sr=16000) # match your model’s sample rate
|
| 18 |
-
|
| 19 |
-
# 2. Extract features
|
| 20 |
-
inputs = feature_extractor(
|
| 21 |
-
speech,
|
| 22 |
-
sampling_rate=16000,
|
| 23 |
-
return_tensors="pt",
|
| 24 |
-
padding=True
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
# 3. Forward pass
|
| 28 |
-
with torch.no_grad():
|
| 29 |
-
outputs = model(**inputs)
|
| 30 |
-
logits = outputs.logits
|
| 31 |
-
predicted_id = torch.argmax(logits, dim=-1).item()
|
| 32 |
-
|
| 33 |
-
# 4. Convert predicted_id to your accuracy scale
|
| 34 |
-
# If 0..7 is your model’s internal label, you might map it back to 3..10
|
| 35 |
-
# Or keep it as 0..7, whichever you prefer
|
| 36 |
-
accuracy_level = predicted_id + 3 # example if your model outputs 0..7
|
| 37 |
-
return accuracy_level
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|