Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,50 @@
|
|
| 1 |
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
-
|
| 5 |
from transformers import pipeline
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
)
|
| 17 |
|
| 18 |
-
demo.launch()
|
| 19 |
-
|
| 20 |
-
|
| 21 |
# Launch the interface
|
| 22 |
-
|
|
|
|
|
|
| 1 |
|
| 2 |
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from transformers import pipeline
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# Initialize the text generation pipeline
|
| 8 |
+
generator = pipeline('text-generation', model='gpt2')
|
| 9 |
+
|
| 10 |
+
def generate_recipe(ingredient):
|
| 11 |
+
"""
|
| 12 |
+
Generate a recipe based on the input ingredient/word
|
| 13 |
+
"""
|
| 14 |
+
# Create a prompt template
|
| 15 |
+
prompt = f"Recipe for {ingredient}:\n\nIngredients:\n"
|
| 16 |
+
|
| 17 |
+
# Generate the recipe
|
| 18 |
+
try:
|
| 19 |
+
result = generator(
|
| 20 |
+
prompt,
|
| 21 |
+
max_length=300,
|
| 22 |
+
num_return_sequences=1,
|
| 23 |
+
temperature=0.7,
|
| 24 |
+
pad_token_id=generator.tokenizer.eos_token_id
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Extract and format the generated recipe
|
| 28 |
+
recipe = result[0]['generated_text']
|
| 29 |
+
|
| 30 |
+
# Clean up the output
|
| 31 |
+
recipe = recipe.replace(prompt, '')
|
| 32 |
+
|
| 33 |
+
return recipe
|
| 34 |
+
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return f"Error generating recipe: {str(e)}"
|
| 37 |
+
|
| 38 |
+
# Create the Gradio interface
|
| 39 |
+
iface = gr.Interface(
|
| 40 |
+
fn=generate_recipe,
|
| 41 |
+
inputs=gr.Textbox(lines=1, placeholder="Enter an ingredient or dish name..."),
|
| 42 |
+
outputs=gr.Textbox(lines=10, label="Generated Recipe"),
|
| 43 |
+
title="Recipe Generator",
|
| 44 |
+
description="Enter an ingredient or dish name, and I'll generate a recipe for you!",
|
| 45 |
+
examples=[["chocolate cake"], ["chicken curry"], ["pasta"]]
|
| 46 |
)
|
| 47 |
|
|
|
|
|
|
|
|
|
|
| 48 |
# Launch the interface
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
iface.launch()
|