Spaces:
Sleeping
Sleeping
Correct trash
Browse files
app.py
CHANGED
@@ -4,43 +4,62 @@ from PIL import Image
|
|
4 |
import torch
|
5 |
from torchvision import transforms
|
6 |
|
|
|
7 |
INGREDIENT_MODEL_ID = "stchakman/Fridge_Items_Model"
|
|
|
|
|
|
|
8 |
ingredient_classifier = pipeline(
|
9 |
"image-classification",
|
10 |
model=INGREDIENT_MODEL_ID,
|
11 |
device=0 if torch.cuda.is_available() else -1,
|
12 |
-
top_k=5
|
13 |
)
|
14 |
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
17 |
|
|
|
18 |
augment = transforms.Compose([
|
19 |
transforms.RandomHorizontalFlip(p=0.5),
|
20 |
transforms.RandomRotation(10),
|
21 |
transforms.ColorJitter(brightness=0.2, contrast=0.2),
|
22 |
])
|
23 |
|
|
|
24 |
def generate_recipe(image: Image.Image):
|
|
|
|
|
|
|
|
|
25 |
image_aug = augment(image)
|
|
|
|
|
26 |
results = ingredient_classifier(image_aug)
|
27 |
ingredients = [res["label"] for res in results]
|
28 |
-
|
29 |
ingredient_str = ", ".join(ingredients)
|
30 |
|
|
|
|
|
|
|
31 |
prompt = f"Ingredients: {ingredient_str}. Recipe:"
|
32 |
-
recipe = recipe_generator(prompt,
|
33 |
-
|
34 |
-
|
|
|
35 |
|
|
|
36 |
interface = gr.Interface(
|
37 |
fn=generate_recipe,
|
38 |
-
inputs=gr.Image(type="pil"),
|
39 |
outputs=gr.Markdown(),
|
40 |
-
title="🥕
|
41 |
-
description="
|
42 |
allow_flagging="never"
|
43 |
)
|
44 |
|
45 |
if __name__ == "__main__":
|
46 |
-
interface.launch()
|
|
|
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=5
|
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 |
+
# Affichage "Processing..." temporaire
|
35 |
+
yield "🔄 Traitement de l'image... Veuillez patienter."
|
36 |
+
|
37 |
+
# Augmentation
|
38 |
image_aug = augment(image)
|
39 |
+
|
40 |
+
# Classification
|
41 |
results = ingredient_classifier(image_aug)
|
42 |
ingredients = [res["label"] for res in results]
|
|
|
43 |
ingredient_str = ", ".join(ingredients)
|
44 |
|
45 |
+
yield f"🥕 Ingrédients détectés : {ingredient_str}\n\n🍳 Génération de la recette..."
|
46 |
+
|
47 |
+
# Génération recette
|
48 |
prompt = f"Ingredients: {ingredient_str}. Recipe:"
|
49 |
+
recipe = recipe_generator(prompt, max_new_tokens=256, do_sample=True)[0]["generated_text"]
|
50 |
+
|
51 |
+
# Affichage final
|
52 |
+
yield f"### 🥕 Ingrédients détectés :\n{ingredient_str}\n\n### 🍽️ Recette générée :\n{recipe}"
|
53 |
|
54 |
+
# INTERFACE
|
55 |
interface = gr.Interface(
|
56 |
fn=generate_recipe,
|
57 |
+
inputs=gr.Image(type="pil", label="📷 Image de vos ingrédients"),
|
58 |
outputs=gr.Markdown(),
|
59 |
+
title="🥕 Générateur de Recettes 🧑🍳",
|
60 |
+
description="Dépose une image d'ingrédients pour obtenir une recette automatiquement générée à partir d'un modèle IA.",
|
61 |
allow_flagging="never"
|
62 |
)
|
63 |
|
64 |
if __name__ == "__main__":
|
65 |
+
interface.launch(share=True) # Mets share=True pour générer le lien Gradio public
|