File size: 1,271 Bytes
f352ab2
 
 
 
 
661fd4d
f352ab2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
661fd4d
 
 
 
 
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
58
59
60
61
62
63
64
# 
import sys
sys.path.append('..')

import os
from fastapi import FastAPI
from pydantic import BaseModel
import wget

# torch
import torch

# utils
from preprocess import process_from_filename, 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}")

# /predict input
# class Data(BaseModel):
#     wav: 


app = FastAPI()

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

@app.get("/urlpredict")
def url_predict(url: str):
    filename = wget.download(url)
    wav = process_from_filename(filename)
    print(f"\ntest {wav.shape}\n")
    
    model_prediction = model_predict(wav)
    return model_prediction["predicition_index"]

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

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

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

    return {
        "output": output,
        "prediction_index": prediction,
    }