Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,48 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
-
|
| 4 |
-
from gradio import
|
|
|
|
| 5 |
|
|
|
|
| 6 |
generator = pipeline("text2text-generation", model="LahiruProjects/recipe-generator-flan-t5")
|
| 7 |
|
|
|
|
| 8 |
def generate_recipe(name, ingredients, calories, time):
|
| 9 |
prompt = f"""Create a step-by-step recipe for "{name}" using these ingredients: {', '.join(ingredients.split(','))}.
|
| 10 |
Keep it under {calories} calories and make sure it's ready in less than {time} minutes."""
|
| 11 |
result = generator(prompt)
|
| 12 |
return result[0]["generated_text"]
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
app =
|
| 26 |
-
app = gr_api.FastAPI(app, demo)
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
+
from gradio import routes
|
| 5 |
+
import uvicorn
|
| 6 |
|
| 7 |
+
# Load the model (this runs only once!)
|
| 8 |
generator = pipeline("text2text-generation", model="LahiruProjects/recipe-generator-flan-t5")
|
| 9 |
|
| 10 |
+
# Function to generate recipe steps
|
| 11 |
def generate_recipe(name, ingredients, calories, time):
|
| 12 |
prompt = f"""Create a step-by-step recipe for "{name}" using these ingredients: {', '.join(ingredients.split(','))}.
|
| 13 |
Keep it under {calories} calories and make sure it's ready in less than {time} minutes."""
|
| 14 |
result = generator(prompt)
|
| 15 |
return result[0]["generated_text"]
|
| 16 |
|
| 17 |
+
# Gradio interface
|
| 18 |
+
iface = gr.Interface(
|
| 19 |
+
fn=generate_recipe,
|
| 20 |
+
inputs=[
|
| 21 |
+
gr.Textbox(label="Recipe Name"),
|
| 22 |
+
gr.Textbox(label="Ingredients (comma-separated)"),
|
| 23 |
+
gr.Number(label="Max Calories", value=400),
|
| 24 |
+
gr.Number(label="Max Cooking Time (minutes)", value=30)
|
| 25 |
+
],
|
| 26 |
+
outputs="text",
|
| 27 |
+
title="π³ Recipe Generator (FLAN-T5)",
|
| 28 |
+
description="Generate a step-by-step recipe based on ingredients, calorie limit, and time"
|
| 29 |
+
)
|
| 30 |
|
| 31 |
+
# FastAPI integration
|
| 32 |
+
app = FastAPI()
|
|
|
|
| 33 |
|
| 34 |
+
# Define the API route using Gradio interface
|
| 35 |
+
@app.post("/api/predict")
|
| 36 |
+
async def predict(data: list):
|
| 37 |
+
inputs = data[0]
|
| 38 |
+
name = inputs[0]
|
| 39 |
+
ingredients = inputs[1]
|
| 40 |
+
calories = inputs[2]
|
| 41 |
+
time = inputs[3]
|
| 42 |
+
result = generate_recipe(name, ingredients, calories, time)
|
| 43 |
+
return {"data": [result]}
|
| 44 |
|
| 45 |
+
# Serve the Gradio interface and FastAPI app using Uvicorn (locally for testing)
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
iface.launch(server_name="0.0.0.0", server_port=7860) # This will launch the Gradio app
|
| 48 |
+
uvicorn.run(app, host="0.0.0.0", port=8000) # FastAPI app will run on port 8000
|