Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
# Initialize the text generation pipeline with the GPT-2 fine-tuned recipe model | |
pipe = pipeline("text-generation", model="mrm8488/gpt2-finetuned-recipes-cooking_v2") | |
def clean_recipe(text): | |
# Split the generated text into steps based on punctuation (e.g., ".", "!", "?") | |
steps = text.split('.') | |
# Remove any extra spaces and filter out empty or repetitive steps | |
cleaned_steps = [] | |
seen_steps = set() # Track steps to avoid repetition | |
for step in steps: | |
step = step.strip() # Remove leading/trailing spaces | |
if step and step.lower() not in seen_steps: # Skip empty or repetitive steps | |
cleaned_steps.append(step) | |
seen_steps.add(step.lower()) # Add step to the seen set | |
return cleaned_steps | |
def generate_recipe(dish_name): | |
# Generate recipe using the text-generation pipeline | |
recipe = pipe(f"Recipe for {dish_name}:", max_length=300, num_return_sequences=1) | |
recipe_text = recipe[0]['generated_text'] | |
# Clean the recipe to remove repetitions and return it as a list of steps | |
return clean_recipe(recipe_text) | |
# Streamlit app | |
st.title("Cooking Recipe Generator") | |
# Input for favorite dish | |
dish_name = st.text_input("Enter your favorite dish") | |
# Button to generate the recipe | |
if st.button("Generate Recipe"): | |
if dish_name: | |
with st.spinner("Generating recipe..."): | |
recipe_steps = generate_recipe(dish_name) | |
st.subheader("Recipe:") | |
# Display the recipe in bullet points | |
for i, step in enumerate(recipe_steps, 1): | |
st.write(f"{i}. {step}") | |
else: | |
st.error("Please enter a dish name.") | |