File size: 1,741 Bytes
6c9ef97
be1f0f3
561c9b4
 
 
6c9ef97
561c9b4
be1f0f3
6c9ef97
561c9b4
be1f0f3
 
 
 
 
 
561c9b4
 
 
 
 
 
 
 
 
 
 
 
 
97f00ad
561c9b4
 
97f00ad
561c9b4
 
 
 
 
 
 
 
 
 
be1f0f3
561c9b4
 
 
 
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
import gradio as gr
from transformers import pipeline
from fastapi import FastAPI
from gradio import routes
import uvicorn

# Load the model (this runs only once!)
generator = pipeline("text2text-generation", model="LahiruProjects/recipe-generator-flan-t5")

# Function to generate recipe steps
def generate_recipe(name, ingredients, calories, time):
    prompt = f"""Create a step-by-step recipe for "{name}" using these ingredients: {', '.join(ingredients.split(','))}.
    Keep it under {calories} calories and make sure it's ready in less than {time} minutes."""
    result = generator(prompt)
    return result[0]["generated_text"]

# Gradio interface
iface = gr.Interface(
    fn=generate_recipe,
    inputs=[
        gr.Textbox(label="Recipe Name"),
        gr.Textbox(label="Ingredients (comma-separated)"),
        gr.Number(label="Max Calories", value=400),
        gr.Number(label="Max Cooking Time (minutes)", value=30)
    ],
    outputs="text",
    title="🍳 Recipe Generator (FLAN-T5)",
    description="Generate a step-by-step recipe based on ingredients, calorie limit, and time"
)

# FastAPI integration
app = FastAPI()

# Define the API route using Gradio interface
@app.post("/api/predict")
async def predict(data: list):
    inputs = data[0]
    name = inputs[0]
    ingredients = inputs[1]
    calories = inputs[2]
    time = inputs[3]
    result = generate_recipe(name, ingredients, calories, time)
    return {"data": [result]}

# Serve the Gradio interface and FastAPI app using Uvicorn (locally for testing)
if __name__ == "__main__":
    iface.launch(server_name="0.0.0.0", server_port=7860)  # This will launch the Gradio app
    uvicorn.run(app, host="0.0.0.0", port=8000)  # FastAPI app will run on port 8000