File size: 1,721 Bytes
81c21da
9a55802
81c21da
9a55802
 
81c21da
be877c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81c21da
9a55802
 
be877c4
 
 
 
81c21da
 
 
 
 
 
 
 
 
 
 
be877c4
81c21da
be877c4
 
 
 
81c21da
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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.")