ankz22's picture
add json reponse
7ddf71b
raw
history blame
2.86 kB
import gradio as gr
from transformers import pipeline
from PIL import Image
import torch
from torchvision import transforms
import re
# MODELES
INGREDIENT_MODEL_ID = "stchakman/Fridge_Items_Model"
RECIPE_MODEL_ID = "flax-community/t5-recipe-generation"
# PIPELINES
ingredient_classifier = pipeline(
"image-classification",
model=INGREDIENT_MODEL_ID,
device=0 if torch.cuda.is_available() else -1,
top_k=5
)
recipe_generator = pipeline(
"text2text-generation",
model=RECIPE_MODEL_ID,
device=0 if torch.cuda.is_available() else -1,
)
# AUGMENTATION
augment = transforms.Compose([
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomRotation(10),
transforms.ColorJitter(brightness=0.2, contrast=0.2),
])
def parse_recipe_text(raw: str):
"""
Parse le texte brut retourne par le modele en JSON { ingredients: [...], steps: [...] }.
"""
raw = raw.replace("ingredients: ingredients:", "ingredients:")
# On cherche la section ingredients et directions
ing_match = re.search(r"ingredients:\s*(.*?)\s*directions:", raw, re.IGNORECASE | re.DOTALL)
dir_match = re.search(r"directions:\s*(.+)$", raw, re.IGNORECASE | re.DOTALL)
# ingredients
ingredients = []
if ing_match:
ing_str = ing_match.group(1).strip()
parts = re.findall(r"(\d+\s+[^\d,\.]+(?: [^\d,\.]+)*)", ing_str)
if parts:
ingredients = [p.strip() for p in parts]
else:
ingredients = [i.strip() for i in ing_str.split(",") if i.strip()]
# etapes
steps = []
if dir_match:
steps_str = dir_match.group(1).strip()
for s in re.split(r"\.\s*", steps_str):
s = s.strip()
if s:
steps.append(s.endswith(".") and s or s + ".")
return {"ingredients": ingredients, "steps": steps}
def generate_recipe_json(image: Image.Image):
# Aug
image_aug = augment(image)
results = ingredient_classifier(image_aug)
ingredients = [res["label"] for res in results]
ingredient_str = ", ".join(ingredients)
prompt = f"Ingredients: {ingredient_str}. Recipe:"
raw = recipe_generator(prompt, max_new_tokens=256, do_sample=True)[0]["generated_text"]
parsed = parse_recipe_text(raw)
return {
"detected_ingredients": ingredients,
"recipe_text": raw,
"ingredients_parsed": parsed["ingredients"],
"steps": parsed["steps"]
}
interface = gr.Interface(
fn=generate_recipe_json,
inputs=gr.Image(type="pil", label="πŸ“· Image de vos ingredients"),
outputs="json",
title="πŸ§‘β€πŸ³ Generateur de Recettes (JSON) πŸ§‘β€πŸ³",
description="Depose une image d'ingredients, recupere directement un JSON structure avec ingredients et etapes.",
allow_flagging="never"
)
if __name__ == "__main__":
interface.launch(share=True)