Spaces:
Sleeping
Sleeping
Add better model
Browse files
app.py
CHANGED
@@ -4,44 +4,35 @@ 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=
|
14 |
device=0 if torch.cuda.is_available() else -1,
|
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
|
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)
|
@@ -51,7 +42,6 @@ def generate_recipe(image: Image.Image):
|
|
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 |
|
@@ -63,21 +53,19 @@ def generate_recipe(image: Image.Image):
|
|
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
|
76 |
outputs=gr.JSON(),
|
77 |
-
title="π₯
|
78 |
-
description="
|
79 |
allow_flagging="never"
|
80 |
)
|
81 |
|
|
|
82 |
if __name__ == "__main__":
|
83 |
-
interface.launch(
|
|
|
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)
|
|
|
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 |
|
|
|
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()
|