Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Load a model from Hugging Face for recipe generation
|
5 |
+
model = pipeline("text2text-generation", model="flax-community/t5-recipe-generation")
|
6 |
+
|
7 |
+
# Recipe generation function
|
8 |
+
def suggest_recipes(ingredients):
|
9 |
+
prompt = f"You are an expert in cooking. Please suggest 3 recipes using the following ingredients: {ingredients}. Provide a title for each recipe, include preparation time, and list step-by-step directions."
|
10 |
+
response = model(prompt, max_length=512, num_return_sequences=3)
|
11 |
+
|
12 |
+
# Parse model output into a structured format
|
13 |
+
recipes = []
|
14 |
+
for i, recipe in enumerate(response):
|
15 |
+
text = recipe["generated_text"]
|
16 |
+
|
17 |
+
# Split into meaningful sections for formatting
|
18 |
+
lines = text.split("\n")
|
19 |
+
title = f"Recipe {i+1}: {lines[0]}" if lines else f"Recipe {i+1}: Title Missing"
|
20 |
+
prep_time = next((line for line in lines if "Preparation time" in line), "Preparation time: Not provided")
|
21 |
+
directions = "\n".join([f"{idx+1}. {line}" for idx, line in enumerate(lines[1:]) if line])
|
22 |
+
|
23 |
+
recipes.append(f"{title}\n{prep_time}\nDirections:\n{directions}\n\nReady to serve!")
|
24 |
+
|
25 |
+
return "\n\n".join(recipes)
|
26 |
+
|
27 |
+
# Gradio interface
|
28 |
+
with gr.Blocks() as app:
|
29 |
+
gr.Markdown("# Recipe Suggestion App")
|
30 |
+
gr.Markdown("Provide the ingredients you have, and this app will suggest recipes along with preparation times!")
|
31 |
+
|
32 |
+
with gr.Row():
|
33 |
+
ingredients_input = gr.Textbox(label="Enter Ingredients (comma-separated):", placeholder="e.g., eggs, milk, flour")
|
34 |
+
|
35 |
+
recipe_output = gr.Textbox(label="Suggested Recipes:", lines=15, interactive=False)
|
36 |
+
|
37 |
+
generate_button = gr.Button("Get Recipes")
|
38 |
+
|
39 |
+
generate_button.click(suggest_recipes, inputs=ingredients_input, outputs=recipe_output)
|
40 |
+
|
41 |
+
# Launch the app
|
42 |
+
app.launch()
|