File size: 1,258 Bytes
92b63f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import requests
import pandas as pd

app = FastAPI()

# Define the structure of the incoming data
class InputData(BaseModel):
    dataframe_records: list[dict]  # List of dictionaries (like rows in a DataFrame)

# Define endpoint to receive data and make a prediction
@app.post("/predict")
async def make_prediction(input_data: InputData):
    # URL for MLflow's prediction server
    mlflow_url = "http://127.0.0.1:8000/invocations"
    headers = {"Content-Type": "application/json"}

    # Prepare the JSON data to send to MLflow
    json_data = {
        "dataframe_records": input_data.dataframe_records
    }

    try:
        # Send data to MLflow and get prediction
        response = requests.post(mlflow_url, headers=headers, json=json_data)
        response.raise_for_status()  # Raise an error for a failed request
        return response.json()  # Return MLflow's prediction result

    except requests.exceptions.HTTPError as err:
        raise HTTPException(status_code=response.status_code, detail=str(err))
    except requests.exceptions.RequestException as e:
        raise HTTPException(status_code=500, detail=str(e))


# uvicorn backend.fastapi_app:app --port 8001