File size: 1,390 Bytes
3460142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# app/main.py
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from typing import List, Dict
from app.models.recipe import RecipeRequest, RecipeResponse
from app.services.recipe_generator import RecipeGenerator

app = FastAPI(title="Recipe Generation API")

# Configure CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Initialize recipe generator service
try:
    recipe_generator = RecipeGenerator()
except Exception as e:
    print(f"Error initializing recipe generator: {str(e)}")
    # You might want to handle this differently in production
    recipe_generator = None

@app.get("/")
async def root():
    return {"message": "Recipe Generation API is running"}

@app.post("/generate-recipe", response_model=RecipeResponse)
async def generate_recipe(request: RecipeRequest):
    if not recipe_generator:
        raise HTTPException(status_code=503, detail="Recipe generator service unavailable")
    
    try:
        recipe = await recipe_generator.generate(request.ingredients)
        return RecipeResponse(
            title=recipe["title"],
            ingredients=recipe["ingredients"],
            instructions=recipe["instructions"]
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))