-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
41 lines (34 loc) · 1.18 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
import itertools
from fastapi import Body, FastAPI, Request
from fastapi.responses import RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from ciqual.nutrients import ciqual_ingredients, prepare_product
from product import get_product
from recipe_estimator import estimate_recipe
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.mount("/static", StaticFiles(directory="./static",html=True), name="static")
@app.get("/")
async def redirect():
return RedirectResponse("static")
@app.get("/ciqual/{name}")
async def ciqual(name):
search_terms = name.casefold().split()
return list(itertools.islice(filter(lambda i: (all(search_term in i['alim_nom_eng'].casefold() for search_term in search_terms)), ciqual_ingredients.values()),20))
@app.get("/product/{id}")
async def product(id):
product = get_product(id)
return product
@app.post("/api/v3/estimate_recipe")
async def recipe(request: Request):
product = await request.json()
prepare_product(product)
estimate_recipe(product)
return product