EmTpro01 commited on
Commit
3460142
1 Parent(s): 0aa79e7

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +44 -32
app/main.py CHANGED
@@ -1,32 +1,44 @@
1
- # app/main.py
2
- from fastapi import FastAPI
3
- from fastapi.middleware.cors import CORSMiddleware
4
- from app.models.recipe import RecipeRequest, RecipeResponse
5
- from app.services.recipe_generator import RecipeGenerator
6
-
7
- app = FastAPI(title="Recipe Generation API")
8
-
9
- # Configure CORS for Vercel frontend
10
- app.add_middleware(
11
- CORSMiddleware,
12
- allow_origins=["*"], # Update this with your Vercel app URL in production
13
- allow_credentials=True,
14
- allow_methods=["*"],
15
- allow_headers=["*"],
16
- )
17
-
18
- # Initialize recipe generator service
19
- recipe_generator = RecipeGenerator()
20
-
21
- @app.get("/")
22
- async def root():
23
- return {"message": "Recipe Generation API is running"}
24
-
25
- @app.post("/generate-recipe", response_model=RecipeResponse)
26
- async def generate_recipe(request: RecipeRequest):
27
- recipe = await recipe_generator.generate(request.ingredients)
28
- return RecipeResponse(
29
- title=recipe["title"],
30
- ingredients=recipe["ingredients"],
31
- instructions=recipe["instructions"]
32
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app/main.py
2
+ from fastapi import FastAPI, HTTPException
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ from typing import List, Dict
5
+ from app.models.recipe import RecipeRequest, RecipeResponse
6
+ from app.services.recipe_generator import RecipeGenerator
7
+
8
+ app = FastAPI(title="Recipe Generation API")
9
+
10
+ # Configure CORS
11
+ app.add_middleware(
12
+ CORSMiddleware,
13
+ allow_origins=["*"],
14
+ allow_credentials=True,
15
+ allow_methods=["*"],
16
+ allow_headers=["*"],
17
+ )
18
+
19
+ # Initialize recipe generator service
20
+ try:
21
+ recipe_generator = RecipeGenerator()
22
+ except Exception as e:
23
+ print(f"Error initializing recipe generator: {str(e)}")
24
+ # You might want to handle this differently in production
25
+ recipe_generator = None
26
+
27
+ @app.get("/")
28
+ async def root():
29
+ return {"message": "Recipe Generation API is running"}
30
+
31
+ @app.post("/generate-recipe", response_model=RecipeResponse)
32
+ async def generate_recipe(request: RecipeRequest):
33
+ if not recipe_generator:
34
+ raise HTTPException(status_code=503, detail="Recipe generator service unavailable")
35
+
36
+ try:
37
+ recipe = await recipe_generator.generate(request.ingredients)
38
+ return RecipeResponse(
39
+ title=recipe["title"],
40
+ ingredients=recipe["ingredients"],
41
+ instructions=recipe["instructions"]
42
+ )
43
+ except Exception as e:
44
+ raise HTTPException(status_code=500, detail=str(e))