TimInf commited on
Commit
098c005
·
verified ·
1 Parent(s): 74d625f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -463
app.py CHANGED
@@ -1,471 +1,73 @@
1
  import gradio as gr
2
- from transformers import FlaxAutoModelForSeq2SeqLM, AutoTokenizer, AutoModel
3
- import torch
4
- import numpy as np
5
- import random
6
  import json
7
- import os
8
-
9
- # Disable Gradio analytics for better performance
10
- os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
11
-
12
- # Load RecipeBERT model (for semantic ingredient combination)
13
- bert_model_name = "alexdseo/RecipeBERT"
14
- bert_tokenizer = AutoTokenizer.from_pretrained(bert_model_name)
15
- bert_model = AutoModel.from_pretrained(bert_model_name)
16
- bert_model.eval()
17
-
18
- # Load T5 recipe generation model
19
- MODEL_NAME_OR_PATH = "flax-community/t5-recipe-generation"
20
- t5_tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME_OR_PATH, use_fast=True)
21
- t5_model = FlaxAutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME_OR_PATH)
22
-
23
- # Token mapping for T5 model output processing
24
- special_tokens = t5_tokenizer.all_special_tokens
25
- tokens_map = {
26
- "<sep>": "--",
27
- "<section>": "\n"
28
- }
29
-
30
-
31
- def get_embedding(text):
32
- """Computes embedding for a text with Mean Pooling over all tokens"""
33
- inputs = bert_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
34
- with torch.no_grad():
35
- outputs = bert_model(**inputs)
36
-
37
- # Mean Pooling - take average of all token embeddings
38
- attention_mask = inputs['attention_mask']
39
- token_embeddings = outputs.last_hidden_state
40
- input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
41
- sum_embeddings = torch.sum(token_embeddings * input_mask_expanded, 1)
42
- sum_mask = torch.clamp(input_mask_expanded.sum(1), min=1e-9)
43
-
44
- return (sum_embeddings / sum_mask).squeeze(0)
45
-
46
-
47
- def average_embedding(embedding_list):
48
- """Computes the average of a list of embeddings"""
49
- tensors = torch.stack([emb for _, emb in embedding_list])
50
- return tensors.mean(dim=0)
51
-
52
-
53
- def get_cosine_similarity(vec1, vec2):
54
- """Computes the cosine similarity between two vectors"""
55
- if torch.is_tensor(vec1):
56
- vec1 = vec1.detach().numpy()
57
- if torch.is_tensor(vec2):
58
- vec2 = vec2.detach().numpy()
59
-
60
- # Make sure vectors have the right shape (flatten if necessary)
61
- vec1 = vec1.flatten()
62
- vec2 = vec2.flatten()
63
-
64
- dot_product = np.dot(vec1, vec2)
65
- norm_a = np.linalg.norm(vec1)
66
- norm_b = np.linalg.norm(vec2)
67
-
68
- # Avoid division by zero
69
- if norm_a == 0 or norm_b == 0:
70
- return 0
71
-
72
- return dot_product / (norm_a * norm_b)
73
-
74
-
75
- def get_combined_scores(query_vector, embedding_list, all_good_embeddings, avg_weight=0.6):
76
- """Computes combined score considering both similarity to average and individual ingredients"""
77
- results = []
78
-
79
- for name, emb in embedding_list:
80
- # Similarity to average vector
81
- avg_similarity = get_cosine_similarity(query_vector, emb)
82
-
83
- # Average similarity to individual ingredients
84
- individual_similarities = [get_cosine_similarity(good_emb, emb)
85
- for _, good_emb in all_good_embeddings]
86
- avg_individual_similarity = sum(individual_similarities) / len(individual_similarities)
87
-
88
- # Combined score (weighted average)
89
- combined_score = avg_weight * avg_similarity + (1 - avg_weight) * avg_individual_similarity
90
-
91
- results.append((name, emb, combined_score))
92
-
93
- # Sort by combined score (descending)
94
- results.sort(key=lambda x: x[2], reverse=True)
95
- return results
96
-
97
-
98
- def find_best_ingredients(required_ingredients, available_ingredients, max_ingredients=6, avg_weight=0.6):
99
- """
100
- Finds the best ingredients based on RecipeBERT embeddings.
101
- """
102
- # Clean and prepare ingredient lists
103
- required_ingredients = [ing.strip() for ing in required_ingredients if ing.strip()]
104
- available_ingredients = [ing.strip() for ing in available_ingredients if ing.strip()]
105
-
106
- # Remove duplicates
107
- required_ingredients = list(set(required_ingredients))
108
- available_ingredients = list(set([i for i in available_ingredients if i not in required_ingredients]))
109
-
110
- # Special case: If no required ingredients, randomly select one from available ingredients
111
- if not required_ingredients and available_ingredients:
112
- random_ingredient = random.choice(available_ingredients)
113
- required_ingredients = [random_ingredient]
114
- available_ingredients = [i for i in available_ingredients if i != random_ingredient]
115
-
116
- # If still no ingredients or already at max capacity
117
- if not required_ingredients or len(required_ingredients) >= max_ingredients:
118
- return required_ingredients[:max_ingredients]
119
-
120
- # If no additional ingredients available
121
- if not available_ingredients:
122
- return required_ingredients
123
-
124
- # Calculate embeddings for all ingredients
125
- embed_required = [(e, get_embedding(e)) for e in required_ingredients]
126
- embed_available = [(e, get_embedding(e)) for e in available_ingredients]
127
-
128
- # Number of ingredients to add
129
- num_to_add = min(max_ingredients - len(required_ingredients), len(available_ingredients))
130
-
131
- # Copy required ingredients to final list
132
- final_ingredients = embed_required.copy()
133
-
134
- # Add best ingredients
135
- for _ in range(num_to_add):
136
- # Calculate average vector of current combination
137
- avg = average_embedding(final_ingredients)
138
-
139
- # Calculate combined scores for all candidates
140
- candidates = get_combined_scores(avg, embed_available, final_ingredients, avg_weight)
141
-
142
- # If no candidates left, break
143
- if not candidates:
144
- break
145
-
146
- # Choose best ingredient
147
- best_name, best_embedding, _ = candidates[0]
148
-
149
- # Add best ingredient to final list
150
- final_ingredients.append((best_name, best_embedding))
151
-
152
- # Remove ingredient from available ingredients
153
- embed_available = [item for item in embed_available if item[0] != best_name]
154
-
155
- # Extract only ingredient names
156
- return [name for name, _ in final_ingredients]
157
-
158
-
159
- def skip_special_tokens(text, special_tokens):
160
- """Removes special tokens from text"""
161
- for token in special_tokens:
162
- text = text.replace(token, "")
163
- return text
164
-
165
-
166
- def target_postprocessing(texts, special_tokens):
167
- """Post-processes generated text"""
168
- if not isinstance(texts, list):
169
- texts = [texts]
170
-
171
- new_texts = []
172
- for text in texts:
173
- text = skip_special_tokens(text, special_tokens)
174
-
175
- for k, v in tokens_map.items():
176
- text = text.replace(k, v)
177
-
178
- new_texts.append(text)
179
-
180
- return new_texts
181
-
182
-
183
- def validate_recipe_ingredients(recipe_ingredients, expected_ingredients, tolerance=0):
184
- """Validates if the recipe contains approximately the expected ingredients."""
185
- recipe_count = len([ing for ing in recipe_ingredients if ing and ing.strip()])
186
- expected_count = len(expected_ingredients)
187
- return abs(recipe_count - expected_count) <= tolerance
188
-
189
-
190
- def generate_recipe_with_t5(ingredients_list, max_retries=5):
191
- """Generates a recipe using the T5 recipe generation model with validation."""
192
- original_ingredients = ingredients_list.copy()
193
-
194
- for attempt in range(max_retries):
195
- try:
196
- # For retries after the first attempt, shuffle the ingredients
197
- if attempt > 0:
198
- current_ingredients = original_ingredients.copy()
199
- random.shuffle(current_ingredients)
200
- else:
201
- current_ingredients = ingredients_list
202
-
203
- # Format ingredients as a comma-separated string
204
- ingredients_string = ", ".join(current_ingredients)
205
- prefix = "items: "
206
-
207
- # Generation settings
208
- generation_kwargs = {
209
- "max_length": 512,
210
- "min_length": 64,
211
- "do_sample": True,
212
- "top_k": 60,
213
- "top_p": 0.95
214
- }
215
-
216
- # Tokenize input
217
- inputs = t5_tokenizer(
218
- prefix + ingredients_string,
219
- max_length=256,
220
- padding="max_length",
221
- truncation=True,
222
- return_tensors="jax"
223
- )
224
-
225
- # Generate text
226
- output_ids = t5_model.generate(
227
- input_ids=inputs.input_ids,
228
- attention_mask=inputs.attention_mask,
229
- **generation_kwargs
230
- )
231
-
232
- # Decode and post-process
233
- generated = output_ids.sequences
234
- generated_text = target_postprocessing(
235
- t5_tokenizer.batch_decode(generated, skip_special_tokens=False),
236
- special_tokens
237
- )[0]
238
-
239
- # Parse sections
240
- recipe = {}
241
- sections = generated_text.split("\n")
242
- for section in sections:
243
- section = section.strip()
244
- if section.startswith("title:"):
245
- recipe["title"] = section.replace("title:", "").strip().capitalize()
246
- elif section.startswith("ingredients:"):
247
- ingredients_text = section.replace("ingredients:", "").strip()
248
- recipe["ingredients"] = [item.strip().capitalize() for item in ingredients_text.split("--") if
249
- item.strip()]
250
- elif section.startswith("directions:"):
251
- directions_text = section.replace("directions:", "").strip()
252
- recipe["directions"] = [step.strip().capitalize() for step in directions_text.split("--") if
253
- step.strip()]
254
-
255
- # If title is missing, create one
256
- if "title" not in recipe:
257
- recipe["title"] = f"Recipe with {', '.join(current_ingredients[:3])}"
258
-
259
- # Ensure all sections exist
260
- if "ingredients" not in recipe:
261
- recipe["ingredients"] = current_ingredients
262
- if "directions" not in recipe:
263
- recipe["directions"] = ["No directions generated"]
264
-
265
- # Validate the recipe
266
- if validate_recipe_ingredients(recipe["ingredients"], original_ingredients, tolerance=1):
267
- return recipe
268
- else:
269
- if attempt == max_retries - 1:
270
- return recipe
271
-
272
- except Exception as e:
273
- if attempt == max_retries - 1:
274
- return {
275
- "title": f"Recipe with {original_ingredients[0] if original_ingredients else 'ingredients'}",
276
- "ingredients": original_ingredients,
277
- "directions": ["Error generating recipe instructions"]
278
- }
279
-
280
- # Fallback
281
- return {
282
- "title": f"Recipe with {original_ingredients[0] if original_ingredients else 'ingredients'}",
283
- "ingredients": original_ingredients,
284
- "directions": ["Error generating recipe instructions"]
285
- }
286
-
287
-
288
- def generate_recipe_interface(required_ingredients_text, available_ingredients_text, max_ingredients):
289
- """Main interface function for Gradio"""
290
- try:
291
- # Parse ingredient inputs
292
- required_ingredients = []
293
- available_ingredients = []
294
-
295
- if required_ingredients_text:
296
- required_ingredients = [ing.strip() for ing in required_ingredients_text.split(',') if ing.strip()]
297
-
298
- if available_ingredients_text:
299
- available_ingredients = [ing.strip() for ing in available_ingredients_text.split(',') if ing.strip()]
300
-
301
- # Validate inputs
302
- if not required_ingredients and not available_ingredients:
303
- return "❌ **Error:** Please provide at least some ingredients!", "", "", ""
304
-
305
- # Find best ingredient combination
306
- optimized_ingredients = find_best_ingredients(
307
- required_ingredients,
308
- available_ingredients,
309
- max_ingredients
310
- )
311
-
312
- # Generate recipe
313
- recipe = generate_recipe_with_t5(optimized_ingredients)
314
-
315
- # Format output
316
- title = f"🍽️ **{recipe['title']}**"
317
-
318
- ingredients_formatted = "## 📋 Ingredients:\n" + "\n".join([f"• {ing}" for ing in recipe['ingredients']])
319
-
320
- directions_formatted = "## 👨‍🍳 Instructions:\n" + "\n".join(
321
- [f"{i + 1}. {step}" for i, step in enumerate(recipe['directions'])])
322
-
323
- used_ingredients = "## ✅ Used Ingredients:\n" + ", ".join(optimized_ingredients)
324
-
325
- return title, ingredients_formatted, directions_formatted, used_ingredients
326
-
327
- except Exception as e:
328
- return f"❌ **Error:** {str(e)}", "", "", ""
329
-
330
-
331
- def generate_recipe_api(required_ingredients_text, available_ingredients_text, max_ingredients):
332
- """API-compatible function that returns JSON format"""
333
- try:
334
- # Parse ingredient inputs
335
- required_ingredients = []
336
- available_ingredients = []
337
-
338
- if required_ingredients_text:
339
- required_ingredients = [ing.strip() for ing in required_ingredients_text.split(',') if ing.strip()]
340
-
341
- if available_ingredients_text:
342
- available_ingredients = [ing.strip() for ing in available_ingredients_text.split(',') if ing.strip()]
343
-
344
- # Validate inputs
345
- if not required_ingredients and not available_ingredients:
346
- return json.dumps({"error": "No ingredients provided"}, indent=2)
347
-
348
- # Find best ingredient combination
349
- optimized_ingredients = find_best_ingredients(
350
- required_ingredients,
351
- available_ingredients,
352
- max_ingredients
353
- )
354
-
355
- # Generate recipe
356
- recipe = generate_recipe_with_t5(optimized_ingredients)
357
-
358
- # Format for API response
359
- api_response = {
360
- 'title': recipe['title'],
361
- 'ingredients': recipe['ingredients'],
362
- 'directions': recipe['directions'],
363
- 'used_ingredients': optimized_ingredients
364
- }
365
-
366
- return json.dumps(api_response, indent=2, ensure_ascii=False)
367
-
368
- except Exception as e:
369
- return json.dumps({"error": f"Error in recipe generation: {str(e)}"}, indent=2)
370
-
371
-
372
- # Create Gradio interface
373
- with gr.Blocks(title="🍳 AI Recipe Generator", theme=gr.themes.Soft()) as demo:
374
- gr.Markdown("""
375
- # 🍳 AI Recipe Generator
376
-
377
- Generate delicious recipes using AI! This tool uses **RecipeBERT** to find the best ingredient combinations and **T5** to generate complete recipes.
378
-
379
- ## How to use:
380
- 1. **Required Ingredients:** Enter ingredients you must use (comma-separated)
381
- 2. **Available Ingredients:** Enter additional ingredients you have available (comma-separated)
382
- 3. **Max Ingredients:** Set the maximum number of ingredients for your recipe
383
- 4. Click **Generate Recipe** to create your personalized recipe!
384
- """)
385
-
386
- with gr.Tab("🍽️ Recipe Generator"):
387
- with gr.Row():
388
- with gr.Column():
389
- required_ingredients = gr.Textbox(
390
- label="🎯 Required Ingredients",
391
- placeholder="chicken, rice, onions",
392
- info="Ingredients that must be included in the recipe (comma-separated)"
393
- )
394
- available_ingredients = gr.Textbox(
395
- label="🥕 Available Ingredients",
396
- placeholder="garlic, tomatoes, basil, cheese",
397
- info="Additional ingredients you have available (comma-separated)"
398
- )
399
- max_ingredients = gr.Slider(
400
- minimum=3, maximum=12, value=7, step=1,
401
- label="📊 Maximum Ingredients",
402
- info="Maximum number of ingredients to use in the recipe"
403
- )
404
- generate_btn = gr.Button("🚀 Generate Recipe", variant="primary", size="lg")
405
-
406
- with gr.Column():
407
- recipe_title = gr.Markdown()
408
- used_ingredients = gr.Markdown()
409
-
410
- with gr.Row():
411
- with gr.Column():
412
- recipe_ingredients = gr.Markdown()
413
- with gr.Column():
414
- recipe_directions = gr.Markdown()
415
-
416
- with gr.Tab("🔌 API Format"):
417
- gr.Markdown("""
418
- ## API Response Format
419
- This tab shows the response in JSON format, compatible with your Flutter app.
420
- """)
421
-
422
- with gr.Row():
423
- with gr.Column():
424
- api_required = gr.Textbox(
425
- label="Required Ingredients",
426
- placeholder="chicken, rice, onions"
427
- )
428
- api_available = gr.Textbox(
429
- label="Available Ingredients",
430
- placeholder="garlic, tomatoes, basil"
431
- )
432
- api_max = gr.Slider(
433
- minimum=3, maximum=12, value=7, step=1,
434
- label="Max Ingredients"
435
- )
436
- api_generate_btn = gr.Button("Generate JSON", variant="secondary")
437
-
438
- with gr.Column():
439
- api_output = gr.Code(language="json", label="API Response")
440
-
441
- # Event handlers - WICHTIG: Diese erstellen die API-Endpunkte!
442
  generate_btn.click(
443
- fn=generate_recipe_interface,
444
- inputs=[required_ingredients, available_ingredients, max_ingredients],
445
- outputs=[recipe_title, recipe_ingredients, recipe_directions, used_ingredients]
446
  )
447
 
448
- api_generate_btn.click(
449
- fn=generate_recipe_api,
450
- inputs=[api_required, api_available, api_max],
451
- outputs=[api_output]
452
- )
453
 
454
- # Example inputs
455
- gr.Examples(
456
- examples=[
457
- ["chicken, rice", "onions, garlic, tomatoes, basil", 6],
458
- ["eggs, flour", "milk, sugar, vanilla, butter", 7],
459
- ["salmon", "lemon, dill, potatoes, asparagus", 5],
460
- ["", "beef, potatoes, carrots, onions, garlic", 6]
461
- ],
462
- inputs=[required_ingredients, available_ingredients, max_ingredients]
463
- )
 
 
 
 
 
 
464
 
465
- # Launch the application
466
  if __name__ == "__main__":
467
- demo.launch(
468
- server_name="0.0.0.0",
469
- server_port=7860,
470
- show_error=True
471
- )
 
1
  import gradio as gr
 
 
 
 
2
  import json
3
+ from fastapi import FastAPI, Request
4
+ from fastapi.responses import JSONResponse
5
+ import uvicorn
6
+ import threading
7
+
8
+ # --------------------------------------
9
+ # Rezept-Logik
10
+ # --------------------------------------
11
+ def generate_recipe(required, available, max_ingredients):
12
+ # Dummy-Logik – hier kannst du dein echtes Rezeptmodell verwenden
13
+ recipe = f"""Rezept mit maximal {max_ingredients} Zutaten:
14
+
15
+ Verwendete Zutaten:
16
+ - Benötigt: {required}
17
+ - Verfügbar: {available}
18
+
19
+ Zubereitung:
20
+ 1. Schneide alle Zutaten klein.
21
+ 2. Koche sie zusammen in einem Topf.
22
+ 3. Würze nach Geschmack und serviere heiß.
23
+ """
24
+ return recipe
25
+
26
+ def generate_recipe_api(required, available, max_ingredients):
27
+ recipe = generate_recipe(required, available, max_ingredients)
28
+ return json.dumps({"recipe": recipe})
29
+
30
+ # --------------------------------------
31
+ # Gradio UI
32
+ # --------------------------------------
33
+ with gr.Blocks() as demo:
34
+ gr.Markdown("## KI-Rezept-Generator")
35
+
36
+ with gr.Row():
37
+ required_input = gr.Textbox(label="Benötigte Zutaten", placeholder="z.B. chicken, rice")
38
+ available_input = gr.Textbox(label="Verfügbare Zutaten", placeholder="z.B. onions, garlic, tomatoes")
39
+
40
+ max_ingredients_input = gr.Slider(label="Maximale Anzahl Zutaten", minimum=3, maximum=12, value=6, step=1)
41
+ output = gr.Textbox(label="Generiertes Rezept")
42
+
43
+ generate_btn = gr.Button("Rezept generieren")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  generate_btn.click(
45
+ fn=generate_recipe,
46
+ inputs=[required_input, available_input, max_ingredients_input],
47
+ outputs=output
48
  )
49
 
50
+ # --------------------------------------
51
+ # FastAPI-Server für eigene API
52
+ # --------------------------------------
53
+ app = FastAPI()
 
54
 
55
+ @app.post("/generate")
56
+ async def generate(request: Request):
57
+ data = await request.json()
58
+ required = data.get("required", "")
59
+ available = data.get("available", "")
60
+ max_ingredients = int(data.get("max_ingredients", 6))
61
+ result = generate_recipe_api(required, available, max_ingredients)
62
+ return JSONResponse(content=json.loads(result))
63
+
64
+ # --------------------------------------
65
+ # Beide Server starten
66
+ # --------------------------------------
67
+ def launch_gradio():
68
+ demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
69
+
70
+ threading.Thread(target=launch_gradio).start()
71
 
 
72
  if __name__ == "__main__":
73
+ uvicorn.run(app, host="0.0.0.0", port=7861)