Poonawala commited on
Commit
2e19dcf
·
verified ·
1 Parent(s): 27972d0

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -231
app.py DELETED
@@ -1,231 +0,0 @@
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 professional design
106
- def main():
107
- with gr.Blocks(css="""
108
- /* General Body Styling */
109
- body {
110
- font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
111
- background-color: #f5f5f5; /* Light background */
112
- margin: 0;
113
- padding: 0;
114
- color: #333; /* Dark text */
115
- }
116
-
117
- /* Header Styling */
118
- .header {
119
- background-color: #006064; /* Professional Blue */
120
- color: white;
121
- padding: 20px;
122
- font-size: 24px;
123
- font-weight: bold;
124
- text-align: center;
125
- border-radius: 10px;
126
- }
127
-
128
- /* Card Style for Tabs */
129
- .tab-container {
130
- background-color: white;
131
- padding: 20px;
132
- border-radius: 8px;
133
- box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
134
- margin-top: 20px;
135
- }
136
-
137
- /* Card for each input section */
138
- .card {
139
- background-color: #ffffff;
140
- padding: 15px;
141
- border-radius: 8px;
142
- box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
143
- margin-bottom: 20px;
144
- }
145
-
146
- .card-title {
147
- font-size: 18px;
148
- font-weight: bold;
149
- color: #006064;
150
- margin-bottom: 10px;
151
- }
152
-
153
- /* Inputs and Buttons */
154
- .input-text, .radio, .button {
155
- width: 100%;
156
- padding: 12px;
157
- border-radius: 8px;
158
- border: 1px solid #ddd;
159
- background-color: #f5f5f5;
160
- color: #333;
161
- font-size: 16px;
162
- }
163
-
164
- .input-text:focus, .radio:focus, .button:focus {
165
- border-color: #006064;
166
- }
167
-
168
- .button {
169
- background-color: #006064;
170
- color: white;
171
- font-weight: bold;
172
- cursor: pointer;
173
- }
174
-
175
- .button:hover {
176
- background-color: #004d40; /* Darker shade of blue on hover */
177
- }
178
-
179
- /* Output Box */
180
- .output-box {
181
- background-color: #ffffff;
182
- padding: 20px;
183
- border-radius: 8px;
184
- box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
185
- font-size: 16px;
186
- color: #333;
187
- max-height: 350px;
188
- overflow-y: auto;
189
- white-space: pre-wrap;
190
- }
191
-
192
- .output-box img {
193
- max-width: 100%;
194
- border-radius: 8px;
195
- }
196
-
197
- /* Tab Title */
198
- .tab-title {
199
- font-size: 22px;
200
- font-weight: bold;
201
- color: #006064;
202
- margin-bottom: 10px;
203
- }
204
- """) as app:
205
-
206
- with gr.Tab("Recipe Generator"):
207
- gr.HTML("<div class='header'>Recipe Generator</div>")
208
- with gr.Column(visible=True):
209
- with gr.Box(elem_classes=["tab-container"]):
210
- recipe_input = gr.Textbox(label="Enter Recipe Name or Ingredients", placeholder="e.g., Chicken curry or chicken, garlic, onions", elem_classes=["input-text"])
211
- selected_cuisine = gr.Radio(choices=cuisines, label="Cuisine", value="Indian", elem_classes=["radio"])
212
- selected_model = gr.Radio(choices=list(models.keys()), label="Model", value="Flan-T5 Small", elem_classes=["radio"])
213
- generate_button = gr.Button("Generate Recipe", elem_classes=["button"])
214
- recipe_output = gr.Textbox(label="Recipe", lines=15, elem_classes=["output-box"])
215
-
216
- generate_button.click(generate_recipe, inputs=[recipe_input, selected_model, selected_cuisine], outputs=recipe_output)
217
-
218
- with gr.Tab("Recipe Finder from Image"):
219
- gr.HTML("<div class='header'>Recipe Finder from Image</div>")
220
- with gr.Column(visible=True):
221
- with gr.Box(elem_classes=["tab-container"]):
222
- image_input = gr.Image(type="filepath", label="Upload an Image")
223
- image_output = gr.Textbox(label="Recipe Details", lines=10, elem_classes=["output-box"])
224
- image_input.change(get_recipe_details, inputs=image_input, outputs=image_output)
225
-
226
- with gr.Tab("Recipe Image Search"):
227
- gr.HTML("<div class='header'>Recipe Image Search</div>")
228
- with gr.Column(visible=True):
229
- with gr.Box(elem_classes=["tab-container"]):
230
- recipe_name_input = gr.Textbox(label="Recipe Name", placeholder="e.g., Mixed Sprouts in Chettinad Masala Recipe", elem_classes=["input-text"])
231
- fetch_image_button