File size: 1,297 Bytes
d9dd7e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff9511c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
import numpy as np
import keras
import httpx
import librosa

from utils import (
    extract_features,
    pad_or_trim,
)

def test_get_answer(audio_file_path: str):
    url = "http://127.0.0.1:8000/process-audio"
    headers = {
        "accept": "application/json",
    }

    with open(audio_file_path, "rb") as audio_file:
        files = {
            "audio": ("test.mp3", audio_file, "audio/mp3")
        }
        response = httpx.post(url, headers=headers, files=files)
    print("Status Code:", response.status_code)
    print("Response JSON:", response.json())


audio_file_path = "test_audio.mp3"
if not os.path.exists(audio_file_path):
    raise FileNotFoundError(f"Audio file not found at {audio_file_path}")

audio_data, sample_rate = librosa.load(audio_file_path)

features = extract_features(audio_data, sample_rate) 

target_shape = (32, 200) 
features = pad_or_trim(features, target_shape[1])


features = np.expand_dims(features, axis=0)

filepath = os.path.abspath("cnn_1_v6_final_model.h5")
if not os.path.exists(filepath):
    raise FileNotFoundError(f"Model file not found at {filepath}")

model = keras.models.load_model(filepath, compile=False)


prediction = model.predict(features)
print(f"Prediction: {prediction.tolist()}")

test_get_answer(audio_file_path)