ankz22 commited on
Commit
1f28202
·
1 Parent(s): 90a4b4f

Return to old model , neww one is to slowww

Browse files
Files changed (1) hide show
  1. app.py +32 -39
app.py CHANGED
@@ -2,70 +2,63 @@ import gradio as gr
2
  from transformers import pipeline
3
  from PIL import Image
4
  import torch
5
- import re
6
 
7
- # 1. Pipelines
 
 
 
 
8
  ingredient_classifier = pipeline(
9
  "image-classification",
10
- model="stchakman/Fridge_Items_Model",
11
  device=0 if torch.cuda.is_available() else -1,
12
  top_k=4
13
  )
14
 
15
  recipe_generator = pipeline(
16
  "text2text-generation",
17
- model="flax-community/t5-recipe-generation",
18
  device=0 if torch.cuda.is_available() else -1
19
  )
20
 
21
- # 2. Fonction bien définie en amont
 
 
 
 
 
 
 
22
  def generate_recipe(image: Image.Image):
23
  try:
24
- results = ingredient_classifier(image)
25
- ingredients = [res["label"].lower() for res in results]
26
-
27
- if not ingredients:
28
- return {
29
- "error": "Aucun ingrédient détecté.",
30
- "ingredients_detected": []
31
- }
32
 
33
- prompt = ", ".join(ingredients)
34
- generated = recipe_generator(prompt, max_length=200)[0]["generated_text"]
35
 
36
- title_match = re.search(r"title\s*:\s*(.+?)(ingredients\s*:|$)", generated, re.IGNORECASE | re.DOTALL)
37
- ingredients_match = re.search(r"ingredients\s*:\s*(.+?)(directions\s*:|$)", generated, re.IGNORECASE | re.DOTALL)
38
- directions_match = re.search(r"directions\s*:\s*(.+)", generated, re.IGNORECASE | re.DOTALL)
 
39
 
40
- title = title_match.group(1).strip() if title_match else None
41
- ingredients_list = ingredients_match.group(1).strip().split('\n') if ingredients_match else []
42
- directions_raw = directions_match.group(1).strip() if directions_match else None
43
- directions_list = re.split(r"\.\s*", directions_raw) if directions_raw else []
44
 
45
- ingredients_list = [line.strip("- ").strip() for line in ingredients_list if line.strip()]
46
- directions_list = [step.strip("- ").strip() for step in directions_list if step.strip()]
47
 
48
- return {
49
- "ingredients_detected": ingredients,
50
- "generated": {
51
- "title": title,
52
- "ingredients": ingredients_list,
53
- "instructions": directions_list
54
- }
55
- }
56
  except Exception as e:
57
- return {"error": str(e)}
58
 
59
- # 3. Interface Gradio (PAS de `share=True` sur Hugging Face !)
60
  interface = gr.Interface(
61
  fn=generate_recipe,
62
  inputs=gr.Image(type="pil", label="📷 Image de vos ingrédients"),
63
- outputs=gr.JSON(),
64
- title="🥕 Générateur de Recettes",
65
- description="Dépose une image d'ingrédients pour générer une recette automatiquement.",
66
  allow_flagging="never"
67
  )
68
 
69
- # 4. Lancement
70
  if __name__ == "__main__":
71
- interface.launch()
 
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,
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
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
 
 
63
  if __name__ == "__main__":
64
+ interface.launch(share=True)