-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
59 lines (50 loc) · 1.58 KB
/
main.py
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
from fastapi import FastAPI
from pydantic import BaseModel
import pandas as pd
import json
app = FastAPI()
class Request(BaseModel):
RecipeID: int
Servings: int
def find_recipe(dat,id,serves):
if (id not in dat["RecipeID"].values)and ("Serves_"+str(serves) not in dat.columns):
return {
"Error":"One or More Entries are Invalid",
"RecipeID": "Invalid",
"Servings": "Invalid"
}
if ("Serves_"+str(serves) not in dat.columns):
return {
"Error":"One or More Entries are Invalid",
"RecipeID": id,
"Servings": "Invalid"
}
elif (id not in dat["RecipeID"].values):
return {
"Error":"One or More Entries are Invalid",
"RecipeID": "Invalid",
"Servings": serves
}
item=dat.loc[dat["RecipeID"]==id]
serve=item["Serves_"+str(serves)]
js={
"RecipeID":int(item["RecipeID"][id-1]),
"Name":item["Name"][id-1],
"Recipe_Meta":json.loads(item["Recipe_Meta"][id-1]),
"Interest_Tags":json.loads(item["Interest_Tags"][id-1]),
"Diet_Tags":json.loads(item["Diet_Tags"][id-1]),
"Cooking_Meta":json.loads(item["Cooking_Meta"][id-1]),
"Serve_"+str(serves):json.loads(serve[id-1])
}
return js
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/hello/{name}")
async def say_hello(name: str):
return {"message": f"Hello {name}"}
@app.post("/recipe")
async def get_recipe(request: Request):
data=pd.read_csv('recipedata.csv')
res=find_recipe(data,request.RecipeID,request.Servings)
return res