LahiruProjects commited on
Commit
561c9b4
Β·
verified Β·
1 Parent(s): d09621a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -15
app.py CHANGED
@@ -1,29 +1,48 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- import fastapi
4
- from gradio import fastapi as gr_api
 
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
- # Using gr.Blocks() for a custom interface
15
- with gr.Blocks() as demo:
16
- with gr.Row():
17
- name = gr.Textbox(label="Recipe Name")
18
- ingredients = gr.Textbox(label="Ingredients (comma-separated)")
19
- calories = gr.Number(label="Max Calories", value=400)
20
- time = gr.Number(label="Max Cooking Time (minutes)", value=30)
21
- output = gr.Textbox()
22
- demo.add(name, ingredients, calories, time, output)
 
 
 
 
23
 
24
- # Set up FastAPI app to expose the REST API
25
- app = fastapi.FastAPI()
26
- app = gr_api.FastAPI(app, demo)
27
 
28
- gr_api.add_api_route(app, "/api/predict", demo, methods=["POST"])
 
 
 
 
 
 
 
 
 
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