|
|
import gradio as gr |
|
|
from transformers import FlaxAutoModelForSeq2SeqLM, AutoTokenizer |
|
|
from transformers import AutoModel |
|
|
import torch |
|
|
import numpy as np |
|
|
import random |
|
|
|
|
|
|
|
|
bert_model_name = "alexdseo/RecipeBERT" |
|
|
bert_tokenizer = AutoTokenizer.from_pretrained(bert_model_name) |
|
|
bert_model = AutoModel.from_pretrained(bert_model_name) |
|
|
bert_model.eval() |
|
|
|
|
|
MODEL_NAME_OR_PATH = "flax-community/t5-recipe-generation" |
|
|
t5_tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME_OR_PATH, use_fast=True) |
|
|
t5_model = FlaxAutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME_OR_PATH) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_recipe_interface(required_ingredients_text, available_ingredients_text, max_ingredients, max_retries): |
|
|
"""Gradio interface function""" |
|
|
try: |
|
|
|
|
|
required_ingredients = [ing.strip() for ing in required_ingredients_text.split(',') if ing.strip()] |
|
|
available_ingredients = [ing.strip() for ing in available_ingredients_text.split(',') if ing.strip()] |
|
|
|
|
|
|
|
|
optimized_ingredients = find_best_ingredients( |
|
|
required_ingredients, |
|
|
available_ingredients, |
|
|
max_ingredients |
|
|
) |
|
|
|
|
|
|
|
|
recipe = generate_recipe_with_t5(optimized_ingredients, max_retries) |
|
|
|
|
|
|
|
|
ingredients_list = '\n'.join([f"• {ing}" for ing in recipe['ingredients']]) |
|
|
directions_list = '\n'.join([f"{i+1}. {dir}" for i, dir in enumerate(recipe['directions'])]) |
|
|
used_ingredients = ', '.join(optimized_ingredients) |
|
|
|
|
|
return ( |
|
|
recipe['title'], |
|
|
ingredients_list, |
|
|
directions_list, |
|
|
used_ingredients |
|
|
) |
|
|
|
|
|
except Exception as e: |
|
|
return f"Error: {str(e)}", "", "", "" |
|
|
|
|
|
|
|
|
with gr.Blocks(title="AI Recipe Generator") as demo: |
|
|
gr.Markdown("# 🍳 AI Recipe Generator") |
|
|
gr.Markdown("Generate recipes using AI with intelligent ingredient combination!") |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
required_ing = gr.Textbox( |
|
|
label="Required Ingredients (comma-separated)", |
|
|
placeholder="chicken, rice, onion", |
|
|
lines=2 |
|
|
) |
|
|
available_ing = gr.Textbox( |
|
|
label="Available Ingredients (comma-separated)", |
|
|
placeholder="garlic, tomato, pepper, herbs", |
|
|
lines=2 |
|
|
) |
|
|
max_ing = gr.Slider( |
|
|
minimum=3, |
|
|
maximum=10, |
|
|
value=7, |
|
|
step=1, |
|
|
label="Maximum Ingredients" |
|
|
) |
|
|
max_retries = gr.Slider( |
|
|
minimum=1, |
|
|
maximum=10, |
|
|
value=5, |
|
|
step=1, |
|
|
label="Max Retries" |
|
|
) |
|
|
generate_btn = gr.Button("Generate Recipe", variant="primary") |
|
|
|
|
|
with gr.Column(): |
|
|
title_output = gr.Textbox(label="Recipe Title", interactive=False) |
|
|
ingredients_output = gr.Textbox(label="Ingredients", lines=8, interactive=False) |
|
|
directions_output = gr.Textbox(label="Directions", lines=10, interactive=False) |
|
|
used_ingredients_output = gr.Textbox(label="Used Ingredients", interactive=False) |
|
|
|
|
|
generate_btn.click( |
|
|
fn=generate_recipe_interface, |
|
|
inputs=[required_ing, available_ing, max_ing, max_retries], |
|
|
outputs=[title_output, ingredients_output, directions_output, used_ingredients_output] |
|
|
) |
|
|
|
|
|
|
|
|
gr.Examples( |
|
|
examples=[ |
|
|
["chicken, rice", "onion, garlic, tomato, herbs, pepper", 6, 3], |
|
|
["pasta", "cheese, mushrooms, cream, spinach, garlic", 5, 3], |
|
|
], |
|
|
inputs=[required_ing, available_ing, max_ing, max_retries] |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |