Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
import re | |
# 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 text into sentences based on periods, question marks, or exclamation points | |
steps = re.split(r'(?<=[.!?])\s+', text) | |
# Remove any irrelevant or overly technical information by filtering based on length and content | |
cleaned_steps = [] | |
seen_steps = set() # Track steps to avoid repetition | |
for step in steps: | |
step = step.strip() # Remove leading/trailing spaces | |
# Skip irrelevant or short steps and avoid repetitions | |
if len(step) > 20 and step.lower() not in seen_steps: | |
cleaned_steps.append(step) | |
seen_steps.add(step.lower()) # Track unique steps | |
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 (numbered list) | |
for i, step in enumerate(recipe_steps, 1): | |
st.markdown(f"{i}. {step}") | |
else: | |
st.error("Please enter a dish name.") | |