File size: 2,366 Bytes
c0ce7f5
 
 
 
01d4bf9
c0ce7f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c666ad4
c0ce7f5
 
c666ad4
 
 
 
 
 
c0ce7f5
 
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
65
66
67
68
69
70
71
72
73
74
75
76
from fastapi import FastAPI
import pickle
from pydantic import BaseModel
import streamlit as st
import requests

app = FastAPI()

pickle_in = open("classifier.pkl", "rb")
classifier = pickle.load(pickle_in)


class Classe(BaseModel):
    Sepal_Length: float
    Sepal_Width: float
    Petal_Length: float
    Petal_Width: float


@app.get("/")
def index():
    return {"hello": "FastAPI"}


@app.get('/{name}')
def get_name(name: str):
    return {'message': f'hello, {name}'}


@app.post('/predict')
def predict_species(data: Classe):
    Sepal_Length = data.Sepal_Length
    Sepal_Width = data.Sepal_Width
    Petal_Length = data.Petal_Length
    Petal_Width = data.Petal_Width

    prediction = classifier.predict([[Sepal_Length, Sepal_Width, Petal_Length, Petal_Width]])

    if prediction[0] == 0:
        species = "setosa"
    elif prediction[0] == 1:
        species = "virginica"
    elif prediction[0] == 2:
        species = "versicolor"
    else:
        species = "unknown"

    return {'prediction': species}


if __name__ == "__main__":
    import uvicorn
    import subprocess

    uvicorn_proc = subprocess.Popen(["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    st.title("Iris Species Prediction")
    st.subheader("Enter the following parameters:")
    sepal_length = st.number_input("Sepal Length", min_value=0.0, max_value=10.0, step=0.1, value=5.0)
    sepal_width = st.number_input("Sepal Width", min_value=0.0, max_value=10.0, step=0.1, value=3.5)
    petal_length = st.number_input("Petal Length", min_value=0.0, max_value=10.0, step=0.1, value=1.4)
    petal_width = st.number_input("Petal Width", min_value=0.0, max_value=10.0, step=0.1, value=0.2)

    submit = st.button("Predict")
    if submit:
        payload = {"Sepal_Length": sepal_length, "Sepal_Width": sepal_width, "Petal_Length": petal_length, "Petal_Width": petal_width}
        prediction = st.empty()
        with st.spinner("Predicting..."):
            response = requests.post("http://localhost:8000/predict", json=payload)
            if response.status_code == 200:
                prediction_result = response.json()
                prediction.success(f"Prediction: {prediction_result['prediction']}")
            else:
                prediction.error("Prediction failed.")

    uvicorn_proc.kill()