ankz22 commited on
Commit
ca7c2fa
Β·
1 Parent(s): c3b16a7

Add better model

Browse files
Files changed (1) hide show
  1. app.py +49 -30
app.py CHANGED
@@ -2,13 +2,12 @@ import gradio as gr
2
  from transformers import pipeline
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,
@@ -16,47 +15,67 @@ ingredient_classifier = pipeline(
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
26
- augment = transforms.Compose([
27
- transforms.RandomHorizontalFlip(p=0.5),
28
- transforms.RandomRotation(10),
29
- transforms.ColorJitter(brightness=0.2, contrast=0.2),
30
- ])
31
-
32
- # FONCTION PRINCIPALE
33
  def generate_recipe(image: Image.Image):
34
  try:
35
- yield "πŸ”„ Traitement de l'image..."
 
 
 
 
 
 
 
 
 
 
36
 
37
- image_aug = image
 
 
 
38
 
39
- yield "πŸ“Έ Classification en cours..."
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
 
48
- yield f"### πŸ₯• IngrΓ©dients dΓ©tectΓ©s :\n{ingredient_str}\n\n### 🍽️ Recette gΓ©nΓ©rΓ©e :\n{recipe}"
 
 
 
 
 
 
 
49
 
50
  except Exception as e:
51
- yield f"❌ Une erreur est survenue : {str(e)}"
52
 
53
- # INTERFACE
 
 
54
  interface = gr.Interface(
55
  fn=generate_recipe,
56
- inputs=gr.Image(type="pil", label="πŸ“· Image de vos ingrΓ©dients"),
57
- outputs=gr.Markdown(),
58
- title="πŸ₯• GΓ©nΓ©rateur de Recettes πŸ§‘β€πŸ³",
59
- description="Dépose une image d'ingrédients pour obtenir une recette automatiquement générée à partir d'un modèle IA.",
60
  allow_flagging="never"
61
  )
62
 
 
2
  from transformers import pipeline
3
  from PIL import Image
4
  import torch
5
+ import re
6
 
7
+ # β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
8
+ # 1. CLASSIFICATION D’INGReDIENTS
9
+ # β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
10
  INGREDIENT_MODEL_ID = "stchakman/Fridge_Items_Model"
 
 
 
11
  ingredient_classifier = pipeline(
12
  "image-classification",
13
  model=INGREDIENT_MODEL_ID,
 
15
  top_k=4
16
  )
17
 
18
+ # β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
19
+ # 2. MODeLE DE GeNeRATION
20
+ # β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
21
  recipe_generator = pipeline(
22
  "text2text-generation",
23
+ model="flax-community/t5-recipe-generation",
24
  device=0 if torch.cuda.is_available() else -1
25
  )
26
 
27
+ # β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
28
+ # 3. FONCTION PRINCIPALE
29
+ # β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
 
 
 
 
 
30
  def generate_recipe(image: Image.Image):
31
  try:
32
+ # Detection ingredients
33
+ results = ingredient_classifier(image)
34
+ ingredients = [res["label"].lower() for res in results]
35
+ if not ingredients:
36
+ return {
37
+ "error": "Aucun ingredient detecte.",
38
+ "ingredients_detected": []
39
+ }
40
+
41
+ prompt = ", ".join(ingredients)
42
+ generated = recipe_generator(prompt, max_length=200)[0]["generated_text"]
43
 
44
+ # extraction par expressions regulieres
45
+ title_match = re.search(r"title\s*:\s*(.+?)(ingredients\s*:|$)", generated, re.IGNORECASE | re.DOTALL)
46
+ ingredients_match = re.search(r"ingredients\s*:\s*(.+?)(directions\s*:|$)", generated, re.IGNORECASE | re.DOTALL)
47
+ directions_match = re.search(r"directions\s*:\s*(.+)", generated, re.IGNORECASE | re.DOTALL)
48
 
49
+ title = title_match.group(1).strip() if title_match else None
50
+ ingredients_list = ingredients_match.group(1).strip().split('\n') if ingredients_match else []
51
+ directions_raw = directions_match.group(1).strip() if directions_match else None
52
+ directions_list = re.split(r"\.\s*", directions_raw) if directions_raw else []
53
 
54
+ # nettoyage des listes
55
+ ingredients_list = [line.strip("- ").strip() for line in ingredients_list if line.strip()]
56
+ directions_list = [step.strip("- ").strip() for step in directions_list if step.strip()]
57
 
58
+ return {
59
+ "ingredients_detected": ingredients,
60
+ "generated": {
61
+ "title": title,
62
+ "ingredients": ingredients_list,
63
+ "instructions": directions_list
64
+ }
65
+ }
66
 
67
  except Exception as e:
68
+ return {"error": str(e)}
69
 
70
+ # β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
71
+ # 4. INTERFACE GRADIO
72
+ # β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
73
  interface = gr.Interface(
74
  fn=generate_recipe,
75
+ inputs=gr.Image(type="pil", label="πŸ“· Image de vos ingredients"),
76
+ outputs=gr.JSON(),
77
+ title="πŸ₯• Generateur de Recettes",
78
+ description="Deposez une image d'ingredients pour generer une recette automatiquement (resultat JSON structure).",
79
  allow_flagging="never"
80
  )
81