40 lines
1.4 KiB
Python
40 lines
1.4 KiB
Python
import dotenv
|
|
from fastapi import FastAPI, File, Form, HTTPException
|
|
from typing import Annotated
|
|
|
|
from src.model import text_recipe_analysis, img_recipe_analysis, find_replacement
|
|
|
|
app = FastAPI(debug=True, title="GoodRecipeAPI", version="1.0")
|
|
|
|
key = dotenv.get_key(".env", "MISTRAL_API_KEY")
|
|
args = {"model": "mistral-small-2506", "api_key": key}
|
|
|
|
@app.get("/")
|
|
async def home():
|
|
return {"message": "ok"}
|
|
|
|
#TODO Check prompt injection
|
|
|
|
@app.post("/analysis/text")
|
|
async def text_analysis(recipe: Annotated[str, Form()]):
|
|
try:
|
|
return text_recipe_analysis(recipe, **args)
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"An error occured trying to analyze the recipe: {e}")
|
|
|
|
@app.post("/analysis/img")
|
|
async def img_analysis(recipe: Annotated[bytes, File()]):
|
|
try:
|
|
return img_recipe_analysis(recipe, **args)
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"An error occured trying to analyze the image: {e}")
|
|
|
|
|
|
@app.get("/replacements")
|
|
async def replace(ingredients: str):
|
|
if ';' not in ingredients or ingredients[-1] != ';':
|
|
raise HTTPException(status_code=500, detail=f"Ingredients should be separated by ';' and end with ';'")
|
|
ingredients = ingredients.split(";")[:-1] # last ingredient is empty
|
|
|
|
return find_replacement(ingredients, **args)
|