File size: 1,829 Bytes
81c21da
9a55802
2c7d942
81c21da
9a55802
 
81c21da
be877c4
2c7d942
 
 
 
be877c4
 
 
 
 
2c7d942
 
be877c4
2c7d942
be877c4
 
 
81c21da
9a55802
 
be877c4
 
 
 
81c21da
 
 
 
 
 
 
 
 
 
 
be877c4
81c21da
be877c4
d8997e3
be877c4
d8997e3
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
50
51
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.")