Spaces:
Running
Running
Return to old model , neww one is to slowww
Browse files
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
|
6 |
|
7 |
-
#
|
|
|
|
|
|
|
|
|
8 |
ingredient_classifier = pipeline(
|
9 |
"image-classification",
|
10 |
-
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=
|
18 |
device=0 if torch.cuda.is_available() else -1
|
19 |
)
|
20 |
|
21 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
def generate_recipe(image: Image.Image):
|
23 |
try:
|
24 |
-
|
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 |
-
|
34 |
-
generated = recipe_generator(prompt, max_length=200)[0]["generated_text"]
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
directions_list = re.split(r"\.\s*", directions_raw) if directions_raw else []
|
44 |
|
45 |
-
|
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 |
-
|
58 |
|
59 |
-
#
|
60 |
interface = gr.Interface(
|
61 |
fn=generate_recipe,
|
62 |
inputs=gr.Image(type="pil", label="📷 Image de vos ingrédients"),
|
63 |
-
outputs=gr.
|
64 |
-
title="🥕 Générateur de Recettes",
|
65 |
-
description="Dépose une image d'ingrédients pour
|
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)
|