feat: Import + Translate recipe images with OpenAI (#3974)

Co-authored-by: Johan Lindell <johan@lindell.me>
Co-authored-by: boc-the-git <3479092+boc-the-git@users.noreply.github.com>
This commit is contained in:
Michael Genson
2024-08-17 17:07:01 -05:00
committed by GitHub
parent 3d921cb677
commit 8a15f400e1
23 changed files with 924 additions and 241 deletions

View File

@@ -34,6 +34,7 @@ class AdminAboutController(BaseAdminController):
oidc_redirect=settings.OIDC_AUTO_REDIRECT,
oidc_provider_name=settings.OIDC_PROVIDER_NAME,
enable_openai=settings.OPENAI_ENABLED,
enable_openai_image_services=settings.OPENAI_ENABLED and settings.OPENAI_ENABLE_IMAGE_SERVICES,
)
@router.get("/statistics", response_model=AppStatistics)

View File

@@ -33,6 +33,7 @@ def get_app_info(session: Session = Depends(generate_session)):
oidc_redirect=settings.OIDC_AUTO_REDIRECT,
oidc_provider_name=settings.OIDC_PROVIDER_NAME,
enable_openai=settings.OPENAI_ENABLED,
enable_openai_image_services=settings.OPENAI_ENABLED and settings.OPENAI_ENABLE_IMAGE_SERVICES,
)

View File

@@ -254,6 +254,9 @@ class RecipeController(BaseRecipeController):
return "recipe_scrapers was unable to scrape this URL"
# ==================================================================================================================
# Other Create Operations
@router.post("/create-from-zip", status_code=201)
def create_recipe_from_zip(self, archive: UploadFile = File(...)):
"""Create recipe from archive"""
@@ -266,6 +269,31 @@ class RecipeController(BaseRecipeController):
return recipe.slug
@router.post("/create-from-image", status_code=201)
async def create_recipe_from_image(
self,
images: list[UploadFile] = File(...),
translate_language: str | None = Query(None, alias="translateLanguage"),
):
"""
Create a recipe from an image using OpenAI.
Optionally specify a language for it to translate the recipe to.
"""
if not (self.settings.OPENAI_ENABLED and self.settings.OPENAI_ENABLE_IMAGE_SERVICES):
raise HTTPException(
status_code=400,
detail=ErrorResponse.respond("OpenAI image services are not enabled"),
)
recipe = await self.service.create_from_images(images, translate_language)
self.publish_event(
event_type=EventTypes.recipe_created,
document_data=EventRecipeData(operation=EventOperation.create, recipe_slug=recipe.slug),
)
return recipe.slug
# ==================================================================================================================
# CRUD Operations