File size: 3,441 Bytes
d2454ee
 
 
 
ca7c2fa
d2454ee
ca7c2fa
 
 
d2454ee
 
 
 
 
52f9526
d2454ee
52f9526
ca7c2fa
 
 
417a04e
 
ca7c2fa
52f9526
417a04e
d2454ee
ca7c2fa
 
 
52f9526
c3b16a7
ca7c2fa
 
 
 
 
 
 
 
 
 
 
417a04e
ca7c2fa
 
 
 
52f9526
ca7c2fa
 
 
 
417a04e
ca7c2fa
 
 
c3b16a7
ca7c2fa
 
 
 
 
 
 
 
c3b16a7
 
ca7c2fa
d2454ee
ca7c2fa
 
 
d2454ee
52f9526
ca7c2fa
 
 
 
d2454ee
 
 
 
7ddf71b
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import gradio as gr
from transformers import pipeline
from PIL import Image
import torch
import re

# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
# 1. CLASSIFICATION D’INGReDIENTS
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
INGREDIENT_MODEL_ID = "stchakman/Fridge_Items_Model"
ingredient_classifier = pipeline(
    "image-classification",
    model=INGREDIENT_MODEL_ID,
    device=0 if torch.cuda.is_available() else -1,
    top_k=4
)

# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
# 2. MODeLE DE GeNeRATION
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
recipe_generator = pipeline(
    "text2text-generation",
    model="flax-community/t5-recipe-generation",
    device=0 if torch.cuda.is_available() else -1
)

# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
# 3. FONCTION PRINCIPALE
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
def generate_recipe(image: Image.Image):
    try:
        # Detection ingredients
        results = ingredient_classifier(image)
        ingredients = [res["label"].lower() for res in results]
        if not ingredients:
            return {
                "error": "Aucun ingredient detecte.",
                "ingredients_detected": []
            }

        prompt = ", ".join(ingredients)
        generated = recipe_generator(prompt, max_length=200)[0]["generated_text"]

        # extraction par expressions regulieres
        title_match = re.search(r"title\s*:\s*(.+?)(ingredients\s*:|$)", generated, re.IGNORECASE | re.DOTALL)
        ingredients_match = re.search(r"ingredients\s*:\s*(.+?)(directions\s*:|$)", generated, re.IGNORECASE | re.DOTALL)
        directions_match = re.search(r"directions\s*:\s*(.+)", generated, re.IGNORECASE | re.DOTALL)

        title = title_match.group(1).strip() if title_match else None
        ingredients_list = ingredients_match.group(1).strip().split('\n') if ingredients_match else []
        directions_raw = directions_match.group(1).strip() if directions_match else None
        directions_list = re.split(r"\.\s*", directions_raw) if directions_raw else []

        # nettoyage des listes
        ingredients_list = [line.strip("- ").strip() for line in ingredients_list if line.strip()]
        directions_list = [step.strip("- ").strip() for step in directions_list if step.strip()]

        return {
            "ingredients_detected": ingredients,
            "generated": {
                "title": title,
                "ingredients": ingredients_list,
                "instructions": directions_list
            }
        }

    except Exception as e:
        return {"error": str(e)}

# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
# 4. INTERFACE GRADIO
# β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
interface = gr.Interface(
    fn=generate_recipe,
    inputs=gr.Image(type="pil", label="πŸ“· Image de vos ingredients"),
    outputs=gr.JSON(),
    title="πŸ₯• Generateur de Recettes",
    description="Deposez une image d'ingredients pour generer une recette automatiquement (resultat JSON structure).",
    allow_flagging="never"
)

if __name__ == "__main__":
    interface.launch(share=True)