pratikshahp's picture
Update app.py
b52f779 verified
raw
history blame
1.86 kB
import gradio as gr
from transformers import pipeline
# Load a model from Hugging Face for recipe generation
model = pipeline("text2text-generation", model="google/flan-t5-large")
# Recipe generation function
def suggest_recipes(ingredients):
prompt = f"You are an expert in cooking. Please suggest 3 recipes using the following ingredients: {ingredients}. Provide a title for each recipe, include preparation time, and list step-by-step directions."
response = model(prompt, max_length=512, num_return_sequences=3)
# Parse model output into a structured format
recipes = []
for i, recipe in enumerate(response):
text = recipe["generated_text"]
# Split into meaningful sections for formatting
lines = text.split("\n")
title = f"Recipe {i+1}: {lines[0]}" if lines else f"Recipe {i+1}: Title Missing"
prep_time = next((line for line in lines if "Preparation time" in line), "Preparation time: Not provided")
directions = "\n".join([f"{idx+1}. {line}" for idx, line in enumerate(lines[1:]) if line])
recipes.append(f"{title}\n{prep_time}\nDirections:\n{directions}\n\nReady to serve!")
return "\n\n".join(recipes)
# Gradio interface
with gr.Blocks() as app:
gr.Markdown("# Recipe Suggestion App")
gr.Markdown("Provide the ingredients you have, and this app will suggest recipes along with preparation times!")
with gr.Row():
ingredients_input = gr.Textbox(label="Enter Ingredients (comma-separated):", placeholder="e.g., eggs, milk, flour")
recipe_output = gr.Textbox(label="Suggested Recipes:", lines=15, interactive=False)
generate_button = gr.Button("Get Recipes")
generate_button.click(suggest_recipes, inputs=ingredients_input, outputs=recipe_output)
# Launch the app
app.launch()