ankz22 commited on
Commit
d2454ee
·
1 Parent(s): b2bd950

Inital commit

Browse files
Files changed (2) hide show
  1. app.py +46 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ 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
+ RECIPE_MODEL_ID = "flax-community/t5-recipe-generation"
16
+ recipe_generator = pipeline("text2text-generation", model=RECIPE_MODEL_ID)
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, max_length=300, do_sample=True)[0]["generated_text"]
33
+
34
+ return f"### Ingredients detectes :\n{ingredient_str}\n\n### Recette generee :\n{recipe}"
35
+
36
+ interface = gr.Interface(
37
+ fn=generate_recipe,
38
+ inputs=gr.Image(type="pil"),
39
+ outputs=gr.Markdown(),
40
+ title="🥕 Generateur de recettes 🧑‍🍳",
41
+ description="Envoyer une image d'ingredients pour recevoir une recette",
42
+ allow_flagging="never"
43
+ )
44
+
45
+ if __name__ == "__main__":
46
+ interface.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ transformers
3
+ torch
4
+ torchvision
5
+ pillow