File size: 1,758 Bytes
2f9c8ed
 
 
 
 
 
 
48f8298
2f9c8ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75ed733
 
2f9c8ed
 
 
 
 
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
import traceback
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List, Optional
import pandas as pd
from model import recommend, output_recommended_recipes

dataset=pd.read_csv('./Data/dataset.csv')

app = FastAPI()

class Params(BaseModel):
    n_neighbors: int = 5
    return_distance: bool = False

class PredictionIn(BaseModel):
    nutrition_input: List[float]
    ingredients: List[str] = []
    params: Optional[Params] = Params()

class Recipe(BaseModel):
    Name: str
    CookTime: str
    PrepTime: str
    TotalTime: str
    RecipeIngredientParts: List[str]
    Calories: float
    FatContent: float
    SaturatedFatContent: float
    CholesterolContent: float
    SodiumContent: float
    CarbohydrateContent: float
    FiberContent: float
    SugarContent: float
    ProteinContent: float
    RecipeInstructions: List[str]

class PredictionOut(BaseModel):
    output: Optional[List[Recipe]] = None

@app.get("/")
def home():
    return {"health_check": "OK"}
@app.post("/predict/")
def update_item(prediction_input: PredictionIn):
    try:
        print("Starting recommendation process")
        recommendation_dataframe = recommend(
            dataset,
            prediction_input.nutrition_input,
            prediction_input.ingredients,
            prediction_input.params.dict()
        )
        print("Recommendation process completed")

        if recommendation_dataframe is None:
            print("No recommendations found")
            return {"output": None}
        
        return recommendation_dataframe.to_json()

    except Exception as err:
        print("An error occurred:")
        print(traceback.format_exc())
        raise HTTPException(status_code=500, detail=str(err))