from fastapi import FastAPI
from fastapi import UploadFile, File
import uvicorn
from fastapi.responses import StreamingResponse
import random
import os
from modelFin import modelFin 

from pydantic import BaseModel

app = FastAPI()

# @app.on_event("startup")
# def load_model():
#     global model
#     model = pickle.load(open("model_tree.pkl", "rb"))

@app.get('/index')
def hello_world(name:str):
    return f"Hello {name}!"

@app.post('/audio/predict')
def predict_audio(file:UploadFile = File(...)):

	#file_name = str(random.randint(0, 100000))
	#file.save(file_name)

    mod = modelFin()
    pre_key = modelFin.predict(file)

    received = file.dict()
    normal = received['children_playing']
    label2 = received['children_playing2']
    label3 = received['children_playing3']
    label4 = received['children_playing4']
    label5 = received['children_playing5']
    
    pred_name = modelFin.predict([[normal, label2, label3,
                                label4, label5]]).tolist()[0]
    return {'prediction': pred_name}


if __name__ == "__main__":
    uvicorn.run(app, port=8080, host='0.0.0.0')   


