Spaces:
Sleeping
Sleeping
| # 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 | |
| async def root(): | |
| return {"message": "Recipe Generation API is running"} | |
| 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)) |