Spaces:
Sleeping
Sleeping
Add better model
Browse files
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 |
-
|
6 |
|
7 |
-
#
|
|
|
|
|
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=
|
22 |
device=0 if torch.cuda.is_available() else -1
|
23 |
)
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
except Exception as e:
|
51 |
-
|
52 |
|
53 |
-
#
|
|
|
|
|
54 |
interface = gr.Interface(
|
55 |
fn=generate_recipe,
|
56 |
-
inputs=gr.Image(type="pil", label="π· Image de vos
|
57 |
-
outputs=gr.
|
58 |
-
title="π₯
|
59 |
-
description="
|
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 |
|