Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
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 |
+
yield "🔄 Traitement de l'image... Veuillez patienter."
|
35 |
+
|
36 |
+
# Augmentation
|
37 |
+
image_aug = augment(image)
|
38 |
+
|
39 |
+
# Classification
|
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 |
+
yield f"### 🥕 Ingrédients détectés :\n{ingredient_str}\n\n### 🍽️ Recette générée :\n{recipe}"
|
48 |
+
|
49 |
+
# INTERFACE
|
50 |
+
interface = gr.Interface(
|
51 |
+
fn=generate_recipe,
|
52 |
+
inputs=gr.Image(type="pil", label="📷 Image de vos ingrédients"),
|
53 |
+
outputs=gr.Markdown(),
|
54 |
+
title="🥕 Générateur de Recettes 🧑🍳",
|
55 |
+
description="Dépose une image d'ingrédients pour obtenir une recette automatiquement générée à partir d'un modèle IA.",
|
56 |
+
allow_flagging="never"
|
57 |
+
)
|
58 |
+
|
59 |
+
if __name__ == "__main__":
|
60 |
+
interface.launch(share=True)
|