ankz22 commited on
Commit
52f9526
·
1 Parent(s): dd7cd13
Files changed (1) hide show
  1. app.py +20 -49
app.py CHANGED
@@ -3,23 +3,23 @@ from transformers import pipeline
3
  from PIL import Image
4
  import torch
5
  from torchvision import transforms
6
- import re
7
 
8
  # MODELES
9
  INGREDIENT_MODEL_ID = "stchakman/Fridge_Items_Model"
10
- RECIPE_MODEL_ID = "flax-community/t5-recipe-generation"
11
 
12
  # PIPELINES
13
  ingredient_classifier = pipeline(
14
  "image-classification",
15
  model=INGREDIENT_MODEL_ID,
16
  device=0 if torch.cuda.is_available() else -1,
17
- top_k=5
18
  )
 
19
  recipe_generator = pipeline(
20
  "text2text-generation",
21
  model=RECIPE_MODEL_ID,
22
- device=0 if torch.cuda.is_available() else -1,
23
  )
24
 
25
  # AUGMENTATION
@@ -29,59 +29,30 @@ augment = transforms.Compose([
29
  transforms.ColorJitter(brightness=0.2, contrast=0.2),
30
  ])
31
 
32
- def parse_recipe_text(raw: str):
33
- """
34
- Parse le texte brut retourne par le modele en JSON { ingredients: [...], steps: [...] }.
35
- """
36
- raw = raw.replace("ingredients: ingredients:", "ingredients:")
37
- # On cherche la section ingredients et directions
38
- ing_match = re.search(r"ingredients:\s*(.*?)\s*directions:", raw, re.IGNORECASE | re.DOTALL)
39
- dir_match = re.search(r"directions:\s*(.+)$", raw, re.IGNORECASE | re.DOTALL)
40
-
41
- # ingredients
42
- ingredients = []
43
- if ing_match:
44
- ing_str = ing_match.group(1).strip()
45
- parts = re.findall(r"(\d+\s+[^\d,\.]+(?: [^\d,\.]+)*)", ing_str)
46
- if parts:
47
- ingredients = [p.strip() for p in parts]
48
- else:
49
- ingredients = [i.strip() for i in ing_str.split(",") if i.strip()]
50
-
51
- # etapes
52
- steps = []
53
- if dir_match:
54
- steps_str = dir_match.group(1).strip()
55
- for s in re.split(r"\.\s*", steps_str):
56
- s = s.strip()
57
- if s:
58
- steps.append(s.endswith(".") and s or s + ".")
59
 
60
- return {"ingredients": ingredients, "steps": steps}
61
-
62
- def generate_recipe_json(image: Image.Image):
63
- # Aug
64
  image_aug = augment(image)
 
 
65
  results = ingredient_classifier(image_aug)
66
  ingredients = [res["label"] for res in results]
67
  ingredient_str = ", ".join(ingredients)
68
- prompt = f"Ingredients: {ingredient_str}. Recipe:"
69
- raw = recipe_generator(prompt, max_new_tokens=256, do_sample=True)[0]["generated_text"]
70
 
71
- parsed = parse_recipe_text(raw)
72
- return {
73
- "detected_ingredients": ingredients,
74
- "recipe_text": raw,
75
- "ingredients_parsed": parsed["ingredients"],
76
- "steps": parsed["steps"]
77
- }
78
 
 
79
  interface = gr.Interface(
80
- fn=generate_recipe_json,
81
- inputs=gr.Image(type="pil", label="📷 Image de vos ingredients"),
82
- outputs="json",
83
- title="🧑‍🍳 Generateur de Recettes (JSON) 🧑‍🍳",
84
- description="Depose une image d'ingredients, recupere directement un JSON structure avec ingredients et etapes.",
85
  allow_flagging="never"
86
  )
87
 
 
3
  from PIL import Image
4
  import torch
5
  from torchvision import transforms
 
6
 
7
  # MODELES
8
  INGREDIENT_MODEL_ID = "stchakman/Fridge_Items_Model"
9
+ RECIPE_MODEL_ID = "flax-community/t5-recipe-generation"
10
 
11
  # PIPELINES
12
  ingredient_classifier = pipeline(
13
  "image-classification",
14
  model=INGREDIENT_MODEL_ID,
15
  device=0 if torch.cuda.is_available() else -1,
16
+ top_k=4
17
  )
18
+
19
  recipe_generator = pipeline(
20
  "text2text-generation",
21
  model=RECIPE_MODEL_ID,
22
+ device=0 if torch.cuda.is_available() else -1
23
  )
24
 
25
  # AUGMENTATION
 
29
  transforms.ColorJitter(brightness=0.2, contrast=0.2),
30
  ])
31
 
32
+ # FONCTION PRINCIPALE
33
+ def generate_recipe(image: Image.Image):
34
+ yield "🔄 Traitement de l'image... Veuillez patienter."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ # Augmentation
 
 
 
37
  image_aug = augment(image)
38
+
39
+ # Classification
40
  results = ingredient_classifier(image_aug)
41
  ingredients = [res["label"] for res in results]
42
  ingredient_str = ", ".join(ingredients)
 
 
43
 
44
+ yield f"🥕 Ingrédients détectés : {ingredient_str}\n\n🍳 Génération de la recette..."
45
+ prompt = f"Ingredients: {ingredient_str}. Recipe:"
46
+ recipe = recipe_generator(prompt, max_new_tokens=256, do_sample=True)[0]["generated_text"]
47
+ yield f"### 🥕 Ingrédients détectés :\n{ingredient_str}\n\n### 🍽️ Recette générée :\n{recipe}"
 
 
 
48
 
49
+ # INTERFACE
50
  interface = gr.Interface(
51
+ fn=generate_recipe,
52
+ inputs=gr.Image(type="pil", label="📷 Image de vos ingrédients"),
53
+ outputs=gr.Markdown(),
54
+ title="🥕 Générateur de Recettes 🧑‍🍳",
55
+ description="Dépose une image d'ingrédients pour obtenir une recette automatiquement générée à partir d'un modèle IA.",
56
  allow_flagging="never"
57
  )
58