File size: 1,140 Bytes
f352ab2
 
 
 
 
661fd4d
f352ab2
 
 
 
 
81d87a8
f352ab2
 
 
 
 
 
 
 
 
661fd4d
 
 
 
f352ab2
 
 
 
81d87a8
f352ab2
 
81d87a8
 
 
 
f352ab2
 
 
 
 
 
 
 
 
81d87a8
f352ab2
 
 
 
 
 
81d87a8
f352ab2
 
81d87a8
f352ab2
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
52
53
54
55
56
57
# 
import sys
sys.path.append('..')

import os
from fastapi import FastAPI

# torch
import torch

# utils
from preprocess import process_from_filename, process_from_url, process_raw_wav
from cnn import CNNetwork

# load model
model = CNNetwork()
state_dict = torch.load("../models/void_demo.pth")
model.load_state_dict(state_dict)

print(f"Model loaded! \n {model}")

app = FastAPI()

@app.get("/")
async def root():
    return { "message": "Hello World" }

@app.get("/urlpredict")
def url_predict(url: str):
    wav = process_from_url(url)
    
    model_prediction = model_predict(wav)
    return {
        "message": "Voice Identified!",
        "data": model_prediction,
    }

@app.put("/predict")
def predict(wav):
    print(f"wav {wav}")
    # return wav
    wav = process_raw_wav(wav)
    model_prediction = model_predict(wav)

    return {
        "message": "Voice Identified!",
        "data": model_prediction,
    }

def model_predict(wav):
    model_input = wav.unsqueeze(0)
    output = model(model_input)
    prediction_index = torch.argmax(output, 1).item()

    return {
        "prediction_index": prediction_index,
    }