Upload 5 files
Browse files- .gitattributes +1 -0
- Food_Recipe.csv +3 -0
- README.md +14 -0
- app.py +210 -0
- gitattributes +36 -0
- requirements.txt +8 -0
.gitattributes
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
Food_Recipe.csv filter=lfs diff=lfs merge=lfs -text
|
Food_Recipe.csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:03a20c91ce5b5e3faf0ebecd233bb38e586099e29a7091e08a2f4ade0d7b7009
|
3 |
+
size 16251035
|
README.md
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Personal Recipe Generator
|
3 |
+
emoji: 🐢
|
4 |
+
colorFrom: purple
|
5 |
+
colorTo: pink
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 5.9.1
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: apache-2.0
|
11 |
+
short_description: a bot that generates recipes for you
|
12 |
+
---
|
13 |
+
|
14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
import gradio as gr
|
4 |
+
from tensorflow.keras.applications import MobileNetV2
|
5 |
+
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
6 |
+
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions
|
7 |
+
from fuzzywuzzy import fuzz
|
8 |
+
from transformers import pipeline
|
9 |
+
import requests
|
10 |
+
from PIL import Image
|
11 |
+
from io import BytesIO
|
12 |
+
|
13 |
+
# Load models using pipeline for recipe generation
|
14 |
+
models = {
|
15 |
+
"Flan-T5 Small": pipeline("text2text-generation", model="BhavaishKumar112/flan-t5-small"),
|
16 |
+
"GPT-Neo 125M": pipeline("text-generation", model="BhavaishKumar112/gpt-neo-125M"),
|
17 |
+
"Final GPT-2 Trained": pipeline("text-generation", model="BhavaishKumar112/finalgpt2trained")
|
18 |
+
}
|
19 |
+
|
20 |
+
# Supported cuisines for recipe generation
|
21 |
+
cuisines = ["Thai", "Indian", "Chinese", "Italian"]
|
22 |
+
|
23 |
+
# Load the dataset for image classification and recipe search
|
24 |
+
dataset_path = "Food_Recipe.csv" # Update with your dataset path
|
25 |
+
data_df = pd.read_csv(dataset_path)
|
26 |
+
|
27 |
+
# Load MobileNetV2 pre-trained model for image classification
|
28 |
+
mobilenet_model = MobileNetV2(weights="imagenet")
|
29 |
+
|
30 |
+
# Function to preprocess images
|
31 |
+
def preprocess_image(image_path, target_size=(224, 224)):
|
32 |
+
image = load_img(image_path, target_size=target_size)
|
33 |
+
image_array = img_to_array(image)
|
34 |
+
image_array = np.expand_dims(image_array, axis=0)
|
35 |
+
return preprocess_input(image_array)
|
36 |
+
|
37 |
+
# Function to classify an image
|
38 |
+
def classify_image(image):
|
39 |
+
try:
|
40 |
+
image_array = preprocess_image(image)
|
41 |
+
predictions = mobilenet_model.predict(image_array)
|
42 |
+
decoded_predictions = decode_predictions(predictions, top=3)[0]
|
43 |
+
return decoded_predictions
|
44 |
+
except Exception as e:
|
45 |
+
print(f"Error during classification: {e}")
|
46 |
+
return []
|
47 |
+
|
48 |
+
# Map classification to recipe using fuzzy matching
|
49 |
+
def map_to_recipe(classification_results):
|
50 |
+
for result in classification_results:
|
51 |
+
best_match = None
|
52 |
+
best_score = 0
|
53 |
+
for index, row in data_df.iterrows():
|
54 |
+
score = fuzz.partial_ratio(result[1].lower(), row["name"].lower())
|
55 |
+
if score > best_score:
|
56 |
+
best_score = score
|
57 |
+
best_match = row
|
58 |
+
if best_score >= 70:
|
59 |
+
return best_match
|
60 |
+
return None
|
61 |
+
|
62 |
+
# Generate recipe summary
|
63 |
+
def generate_summary(recipe):
|
64 |
+
ingredients = recipe.get("ingredients_name", "No ingredients provided")
|
65 |
+
time_to_cook = recipe.get("time_to_cook", "Time to cook not provided")
|
66 |
+
instructions = recipe.get("instructions", "No instructions provided")
|
67 |
+
return f"Ingredients: {ingredients}\n\nTime to Cook: {time_to_cook}\n\nInstructions: {instructions}"
|
68 |
+
|
69 |
+
# Function to handle image input and return recipe details
|
70 |
+
def get_recipe_details(image):
|
71 |
+
classification_results = classify_image(image)
|
72 |
+
if not classification_results:
|
73 |
+
return "Error: No classification results found for the image."
|
74 |
+
recipe = map_to_recipe(classification_results)
|
75 |
+
if recipe is not None:
|
76 |
+
return generate_summary(recipe)
|
77 |
+
else:
|
78 |
+
return "No matching recipe found for this image."
|
79 |
+
|
80 |
+
# Function for recipe generation (as before)
|
81 |
+
def generate_recipe(input_text, selected_model, selected_cuisine):
|
82 |
+
prompt = (
|
83 |
+
f"Generate a detailed and structured {selected_cuisine} recipe for {input_text}. "
|
84 |
+
f"Include all the necessary details such as ingredients under an 'Ingredients' heading "
|
85 |
+
f"and steps under a 'Recipe' heading. Ensure the response is concise and well-organized."
|
86 |
+
)
|
87 |
+
model = models[selected_model]
|
88 |
+
output = model(prompt, max_length=500, num_return_sequences=1)[0]['generated_text']
|
89 |
+
return output
|
90 |
+
|
91 |
+
# Function to fetch and display the image for a recipe name
|
92 |
+
def fetch_recipe_image(recipe_name):
|
93 |
+
matching_row = data_df[data_df['name'].str.contains(recipe_name, case=False, na=False)]
|
94 |
+
if not matching_row.empty:
|
95 |
+
image_url = matching_row.iloc[0]['image_url']
|
96 |
+
try:
|
97 |
+
response = requests.get(image_url)
|
98 |
+
img = Image.open(BytesIO(response.content))
|
99 |
+
return img
|
100 |
+
except Exception as e:
|
101 |
+
return f"Error fetching image: {e}"
|
102 |
+
else:
|
103 |
+
return "No matching recipe found. Please check the recipe name."
|
104 |
+
|
105 |
+
# Gradio interface with updated vibrant colors and higher contrast for better readability
|
106 |
+
def main():
|
107 |
+
with gr.Blocks(css="""
|
108 |
+
body {
|
109 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
110 |
+
background-color: #1c1c1c; /* Dark background for high contrast */
|
111 |
+
margin: 0;
|
112 |
+
padding: 0;
|
113 |
+
color: #e0e0e0; /* Light text for contrast */
|
114 |
+
}
|
115 |
+
.chat-container {
|
116 |
+
max-width: 800px;
|
117 |
+
margin: 30px auto;
|
118 |
+
padding: 20px;
|
119 |
+
background: #333333; /* Dark gray background */
|
120 |
+
border-radius: 16px;
|
121 |
+
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
|
122 |
+
}
|
123 |
+
.chat-header {
|
124 |
+
text-align: center;
|
125 |
+
font-size: 32px;
|
126 |
+
font-weight: bold;
|
127 |
+
color: #ff9800; /* Orange for visibility */
|
128 |
+
margin-bottom: 20px;
|
129 |
+
}
|
130 |
+
.chat-input {
|
131 |
+
width: 100%;
|
132 |
+
padding: 14px;
|
133 |
+
font-size: 16px;
|
134 |
+
border-radius: 12px;
|
135 |
+
border: 1px solid #ff9800; /* Orange border */
|
136 |
+
margin-bottom: 15px;
|
137 |
+
background-color: #424242; /* Dark input field */
|
138 |
+
color: #e0e0e0; /* Light text */
|
139 |
+
}
|
140 |
+
.chat-button {
|
141 |
+
background-color: #ff9800;
|
142 |
+
color: white;
|
143 |
+
border: none;
|
144 |
+
padding: 12px 24px;
|
145 |
+
font-size: 16px;
|
146 |
+
border-radius: 12px;
|
147 |
+
cursor: pointer;
|
148 |
+
}
|
149 |
+
.chat-button:hover {
|
150 |
+
background-color: #e65100; /* Darker orange for hover */
|
151 |
+
}
|
152 |
+
.chat-output {
|
153 |
+
padding: 15px;
|
154 |
+
background: #424242; /* Dark gray background for output */
|
155 |
+
border-radius: 10px;
|
156 |
+
border: 1px solid #616161; /* Light gray border */
|
157 |
+
color: #e0e0e0; /* Light text */
|
158 |
+
white-space: pre-wrap;
|
159 |
+
min-height: 120px;
|
160 |
+
}
|
161 |
+
.tab-title {
|
162 |
+
font-weight: bold;
|
163 |
+
font-size: 22px;
|
164 |
+
color: #ff9800; /* Orange text for tab title */
|
165 |
+
}
|
166 |
+
.tab-button {
|
167 |
+
background-color: #616161;
|
168 |
+
color: #ff9800;
|
169 |
+
border: 1px solid #ff9800;
|
170 |
+
padding: 12px;
|
171 |
+
border-radius: 12px;
|
172 |
+
}
|
173 |
+
.tab-button:hover {
|
174 |
+
background-color: #ff5722; /* Bright orange for tab button hover */
|
175 |
+
}
|
176 |
+
.icon {
|
177 |
+
font-size: 20px;
|
178 |
+
margin-right: 10px;
|
179 |
+
}
|
180 |
+
.gradio-container {
|
181 |
+
margin-top: 20px;
|
182 |
+
}
|
183 |
+
""") as app:
|
184 |
+
|
185 |
+
with gr.Tab("Recipe Generator"):
|
186 |
+
gr.HTML("<div class='chat-container'><div class='chat-header'><i class='icon'>🍽</i>Recipe Generator</div><p class='tab-title'>Enter a recipe name or ingredients, select a cuisine and model, and get structured recipe instructions!</p></div>")
|
187 |
+
recipe_input = gr.Textbox(label="Enter Recipe Name or Ingredients", placeholder="e.g., Chicken curry or chicken, garlic, onions", elem_classes=["chat-input"])
|
188 |
+
selected_cuisine = gr.Radio(choices=cuisines, label="Cuisine", value="Indian")
|
189 |
+
selected_model = gr.Radio(choices=list(models.keys()), label="Model", value="Flan-T5 Small")
|
190 |
+
recipe_output = gr.Textbox(label="Recipe", lines=15, elem_classes=["chat-output"])
|
191 |
+
generate_button = gr.Button("Generate Recipe", elem_classes=["chat-button"])
|
192 |
+
generate_button.click(generate_recipe, inputs=[recipe_input, selected_model, selected_cuisine], outputs=recipe_output)
|
193 |
+
|
194 |
+
with gr.Tab("Recipe Finder from Image"):
|
195 |
+
gr.HTML("<div class='chat-container'><div class='chat-header'><i class='icon'>📸</i>Recipe Finder from Image</div><p class='tab-title'>Upload an image of a dish to find a matching recipe.</p></div>")
|
196 |
+
image_input = gr.Image(type="filepath", label="Upload an Image")
|
197 |
+
image_output = gr.Textbox(label="Recipe Details", lines=10, elem_classes=["chat-output"])
|
198 |
+
image_input.change(get_recipe_details, inputs=image_input, outputs=image_output)
|
199 |
+
|
200 |
+
with gr.Tab("Recipe Image Search"):
|
201 |
+
gr.HTML("<div class='chat-container'><div class='chat-header'><i class='icon'>📷</i>Recipe Image Search</div><p class='tab-title'>Enter the name of a recipe to view its image.</p></div>")
|
202 |
+
recipe_name_input = gr.Textbox(label="Recipe Name", placeholder="e.g., Mixed Sprouts in Chettinad Masala Recipe", elem_classes=["chat-input"])
|
203 |
+
recipe_image_output = gr.Image(label="Recipe Image")
|
204 |
+
fetch_image_button = gr.Button("Generate Image", elem_classes=["chat-button"])
|
205 |
+
fetch_image_button.click(fetch_recipe_image, inputs=recipe_name_input, outputs=recipe_image_output)
|
206 |
+
|
207 |
+
app.launch()
|
208 |
+
|
209 |
+
if __name__ == "__main__":
|
210 |
+
main()
|
gitattributes
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
Food_Recipe.csv filter=lfs diff=lfs merge=lfs -text
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
tensorflow
|
3 |
+
datasets
|
4 |
+
fuzzywuzzy
|
5 |
+
pandas
|
6 |
+
gradio
|
7 |
+
torch
|
8 |
+
tf-keras
|