tahirsher commited on
Commit
be877c4
·
verified ·
1 Parent(s): 9a55802

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -3
app.py CHANGED
@@ -4,10 +4,29 @@ from transformers import pipeline
4
  # Initialize the text generation pipeline with the GPT-2 fine-tuned recipe model
5
  pipe = pipeline("text-generation", model="mrm8488/gpt2-finetuned-recipes-cooking_v2")
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  def generate_recipe(dish_name):
8
  # Generate recipe using the text-generation pipeline
9
  recipe = pipe(f"Recipe for {dish_name}:", max_length=300, num_return_sequences=1)
10
- return recipe[0]['generated_text']
 
 
 
11
 
12
  # Streamlit app
13
  st.title("Cooking Recipe Generator")
@@ -19,8 +38,11 @@ dish_name = st.text_input("Enter your favorite dish")
19
  if st.button("Generate Recipe"):
20
  if dish_name:
21
  with st.spinner("Generating recipe..."):
22
- recipe = generate_recipe(dish_name)
23
  st.subheader("Recipe:")
24
- st.write(recipe)
 
 
 
25
  else:
26
  st.error("Please enter a dish name.")
 
4
  # Initialize the text generation pipeline with the GPT-2 fine-tuned recipe model
5
  pipe = pipeline("text-generation", model="mrm8488/gpt2-finetuned-recipes-cooking_v2")
6
 
7
+ def clean_recipe(text):
8
+ # Split the generated text into steps based on punctuation (e.g., ".", "!", "?")
9
+ steps = text.split('.')
10
+
11
+ # Remove any extra spaces and filter out empty or repetitive steps
12
+ cleaned_steps = []
13
+ seen_steps = set() # Track steps to avoid repetition
14
+
15
+ for step in steps:
16
+ step = step.strip() # Remove leading/trailing spaces
17
+ if step and step.lower() not in seen_steps: # Skip empty or repetitive steps
18
+ cleaned_steps.append(step)
19
+ seen_steps.add(step.lower()) # Add step to the seen set
20
+
21
+ return cleaned_steps
22
+
23
  def generate_recipe(dish_name):
24
  # Generate recipe using the text-generation pipeline
25
  recipe = pipe(f"Recipe for {dish_name}:", max_length=300, num_return_sequences=1)
26
+ recipe_text = recipe[0]['generated_text']
27
+
28
+ # Clean the recipe to remove repetitions and return it as a list of steps
29
+ return clean_recipe(recipe_text)
30
 
31
  # Streamlit app
32
  st.title("Cooking Recipe Generator")
 
38
  if st.button("Generate Recipe"):
39
  if dish_name:
40
  with st.spinner("Generating recipe..."):
41
+ recipe_steps = generate_recipe(dish_name)
42
  st.subheader("Recipe:")
43
+
44
+ # Display the recipe in bullet points
45
+ for i, step in enumerate(recipe_steps, 1):
46
+ st.write(f"{i}. {step}")
47
  else:
48
  st.error("Please enter a dish name.")