Spaces:
Sleeping
Sleeping
Update app/main.py
Browse files- 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
|
5 |
-
from app.
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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))
|