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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 10 |
+
# Load models using pipeline for recipe generation
|
| 11 |
+
models = {
|
| 12 |
+
"Flan-T5 Small": pipeline("text2text-generation", model="BhavaishKumar112/flan-t5-small"),
|
| 13 |
+
"GPT-Neo 125M": pipeline("text-generation", model="BhavaishKumar112/gpt-neo-125M"),
|
| 14 |
+
"Final GPT-2 Trained": pipeline("text-generation", model="BhavaishKumar112/finalgpt2trained")
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
# Supported cuisines for recipe generation
|
| 18 |
+
cuisines = ["Thai", "Indian", "Chinese", "Italian"]
|
| 19 |
+
|
| 20 |
+
# Load the dataset for image classification
|
| 21 |
+
dataset_path = "/content/Food_Recipe.csv" # Update with your dataset path
|
| 22 |
+
data_df = pd.read_csv(dataset_path)
|
| 23 |
+
|
| 24 |
+
# Load MobileNetV2 pre-trained model for image classification
|
| 25 |
+
mobilenet_model = MobileNetV2(weights="imagenet")
|
| 26 |
+
|
| 27 |
+
# Function to preprocess images
|
| 28 |
+
def preprocess_image(image_path, target_size=(224, 224)):
|
| 29 |
+
image = load_img(image_path, target_size=target_size)
|
| 30 |
+
image_array = img_to_array(image)
|
| 31 |
+
image_array = np.expand_dims(image_array, axis=0)
|
| 32 |
+
return preprocess_input(image_array)
|
| 33 |
+
|
| 34 |
+
# Function to classify an image
|
| 35 |
+
def classify_image(image):
|
| 36 |
+
try:
|
| 37 |
+
image_array = preprocess_image(image)
|
| 38 |
+
predictions = mobilenet_model.predict(image_array)
|
| 39 |
+
decoded_predictions = decode_predictions(predictions, top=3)[0]
|
| 40 |
+
return decoded_predictions
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print(f"Error during classification: {e}")
|
| 43 |
+
return []
|
| 44 |
+
|
| 45 |
+
# Map classification to recipe using fuzzy matching
|
| 46 |
+
def map_to_recipe(classification_results):
|
| 47 |
+
for result in classification_results:
|
| 48 |
+
best_match = None
|
| 49 |
+
best_score = 0
|
| 50 |
+
for index, row in data_df.iterrows():
|
| 51 |
+
score = fuzz.partial_ratio(result[1].lower(), row["name"].lower())
|
| 52 |
+
if score > best_score:
|
| 53 |
+
best_score = score
|
| 54 |
+
best_match = row
|
| 55 |
+
if best_score >= 70:
|
| 56 |
+
return best_match
|
| 57 |
+
return None
|
| 58 |
+
|
| 59 |
+
# Generate recipe summary
|
| 60 |
+
def generate_summary(recipe):
|
| 61 |
+
ingredients = recipe.get("ingredients_name", "No ingredients provided")
|
| 62 |
+
time_to_cook = recipe.get("time_to_cook", "Time to cook not provided")
|
| 63 |
+
instructions = recipe.get("instructions", "No instructions provided")
|
| 64 |
+
return f"Ingredients: {ingredients}\n\nTime to Cook: {time_to_cook}\n\nInstructions: {instructions}"
|
| 65 |
+
|
| 66 |
+
# Function to handle image input and return recipe details
|
| 67 |
+
def get_recipe_details(image):
|
| 68 |
+
classification_results = classify_image(image)
|
| 69 |
+
if not classification_results:
|
| 70 |
+
return "Error: No classification results found for the image."
|
| 71 |
+
recipe = map_to_recipe(classification_results)
|
| 72 |
+
if recipe is not None:
|
| 73 |
+
return generate_summary(recipe)
|
| 74 |
+
else:
|
| 75 |
+
return "No matching recipe found for this image."
|
| 76 |
+
|
| 77 |
+
# Function for recipe generation (as before)
|
| 78 |
+
def generate_recipe(input_text, selected_model, selected_cuisine):
|
| 79 |
+
prompt = (
|
| 80 |
+
f"Generate a detailed and structured {selected_cuisine} recipe for {input_text}. "
|
| 81 |
+
f"Include all the necessary details such as ingredients under an 'Ingredients' heading "
|
| 82 |
+
f"and steps under a 'Recipe' heading. Ensure the response is concise and well-organized."
|
| 83 |
+
)
|
| 84 |
+
model = models[selected_model]
|
| 85 |
+
output = model(prompt, max_length=500, num_return_sequences=1)[0]['generated_text']
|
| 86 |
+
return output
|
| 87 |
+
|
| 88 |
+
# Gradio interface with vibrant colors and icons
|
| 89 |
+
def main():
|
| 90 |
+
with gr.Blocks(css="""
|
| 91 |
+
body {
|
| 92 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 93 |
+
background-color: #f5f5f7;
|
| 94 |
+
margin: 0;
|
| 95 |
+
padding: 0;
|
| 96 |
+
}
|
| 97 |
+
.chat-container {
|
| 98 |
+
max-width: 800px;
|
| 99 |
+
margin: 30px auto;
|
| 100 |
+
padding: 20px;
|
| 101 |
+
background: #ffffff;
|
| 102 |
+
border-radius: 16px;
|
| 103 |
+
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
|
| 104 |
+
}
|
| 105 |
+
.chat-header {
|
| 106 |
+
text-align: center;
|
| 107 |
+
font-size: 32px;
|
| 108 |
+
font-weight: bold;
|
| 109 |
+
color: #00796b;
|
| 110 |
+
margin-bottom: 20px;
|
| 111 |
+
}
|
| 112 |
+
.chat-input {
|
| 113 |
+
width: 100%;
|
| 114 |
+
padding: 14px;
|
| 115 |
+
font-size: 16px;
|
| 116 |
+
border-radius: 12px;
|
| 117 |
+
border: 1px solid #00796b;
|
| 118 |
+
margin-bottom: 15px;
|
| 119 |
+
background-color: #e0f2f1;
|
| 120 |
+
}
|
| 121 |
+
.chat-button {
|
| 122 |
+
background-color: #00796b;
|
| 123 |
+
color: white;
|
| 124 |
+
border: none;
|
| 125 |
+
padding: 12px 24px;
|
| 126 |
+
font-size: 16px;
|
| 127 |
+
border-radius: 12px;
|
| 128 |
+
cursor: pointer;
|
| 129 |
+
}
|
| 130 |
+
.chat-button:hover {
|
| 131 |
+
background-color: #004d40;
|
| 132 |
+
}
|
| 133 |
+
.chat-output {
|
| 134 |
+
padding: 15px;
|
| 135 |
+
background: #e8f5e9;
|
| 136 |
+
border-radius: 10px;
|
| 137 |
+
border: 1px solid #c8e6c9;
|
| 138 |
+
white-space: pre-wrap;
|
| 139 |
+
min-height: 120px;
|
| 140 |
+
}
|
| 141 |
+
.tab-title {
|
| 142 |
+
font-weight: bold;
|
| 143 |
+
font-size: 22px;
|
| 144 |
+
color: #00796b;
|
| 145 |
+
}
|
| 146 |
+
.tab-button {
|
| 147 |
+
background-color: #e0f2f1;
|
| 148 |
+
color: #00796b;
|
| 149 |
+
border: 1px solid #00796b;
|
| 150 |
+
padding: 12px;
|
| 151 |
+
border-radius: 12px;
|
| 152 |
+
}
|
| 153 |
+
.tab-button:hover {
|
| 154 |
+
background-color: #b2dfdb;
|
| 155 |
+
}
|
| 156 |
+
.icon {
|
| 157 |
+
font-size: 20px;
|
| 158 |
+
margin-right: 10px;
|
| 159 |
+
}
|
| 160 |
+
.gradio-container {
|
| 161 |
+
margin-top: 20px;
|
| 162 |
+
}
|
| 163 |
+
""") as app:
|
| 164 |
+
|
| 165 |
+
with gr.Tab("Recipe Generator"):
|
| 166 |
+
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>")
|
| 167 |
+
recipe_input = gr.Textbox(label="Enter Recipe Name or Ingredients", placeholder="e.g., Chicken curry or chicken, garlic, onions", elem_classes=["chat-input"])
|
| 168 |
+
selected_cuisine = gr.Radio(choices=cuisines, label="Cuisine", value="Indian")
|
| 169 |
+
selected_model = gr.Radio(choices=list(models.keys()), label="Model", value="Flan-T5 Small")
|
| 170 |
+
recipe_output = gr.Textbox(label="Recipe", lines=15, elem_classes=["chat-output"])
|
| 171 |
+
generate_button = gr.Button("Generate Recipe", elem_classes=["chat-button"])
|
| 172 |
+
generate_button.click(generate_recipe, inputs=[recipe_input, selected_model, selected_cuisine], outputs=recipe_output)
|
| 173 |
+
|
| 174 |
+
with gr.Tab("Recipe Finder from Image"):
|
| 175 |
+
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>")
|
| 176 |
+
image_input = gr.Image(type="filepath", label="Upload an Image")
|
| 177 |
+
image_output = gr.Textbox(label="Recipe Details", lines=10, elem_classes=["chat-output"])
|
| 178 |
+
image_input.change(get_recipe_details, inputs=image_input, outputs=image_output)
|
| 179 |
+
|
| 180 |
+
app.launch()
|
| 181 |
+
|
| 182 |
+
if __name__ == "__main__":
|
| 183 |
+
main()
|