EmTpro01 commited on
Commit
4312b6d
·
verified ·
1 Parent(s): c92e2e6

Upload 4 files

Browse files
app/config.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app/config.py
2
+ from pydantic_settings import BaseSettings
3
+
4
+ class Settings(BaseSettings):
5
+ MODEL_NAME: str = "your-model-name"
6
+ MAX_LENGTH: int = 512
7
+ TEMPERATURE: float = 0.7
8
+ TOP_P: float = 0.9
9
+
10
+ class Config:
11
+ env_file = ".env"
app/main.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ )
app/models/recipe.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app/models/recipe.py
2
+ from pydantic import BaseModel
3
+ from typing import List
4
+
5
+ class RecipeRequest(BaseModel):
6
+ ingredients: List[str]
7
+
8
+ class RecipeResponse(BaseModel):
9
+ title: str
10
+ ingredients: List[str]
11
+ instructions: List[str]
app/services/recipe_generator.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app/services/recipe_generator.py
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+
5
+ class RecipeGenerator:
6
+ def __init__(self):
7
+ # Initialize your fine-tuned model and tokenizer
8
+ self.tokenizer = AutoTokenizer.from_pretrained("flax-community/t5-recipe-generation")
9
+ self.model = AutoModelForCausalLM.from_pretrained("flax-community/t5-recipe-generation")
10
+
11
+ async def generate(self, ingredients: List[str]):
12
+ # Format ingredients for input
13
+ input_text = f"Generate a recipe using these ingredients: {', '.join(ingredients)}"
14
+
15
+ # Tokenize and generate
16
+ inputs = self.tokenizer(input_text, return_tensors="pt", padding=True)
17
+ outputs = self.model.generate(
18
+ inputs.input_ids,
19
+ max_length=512,
20
+ num_return_sequences=1,
21
+ temperature=0.7,
22
+ top_p=0.9,
23
+ do_sample=True
24
+ )
25
+
26
+ # Decode and parse the generated recipe
27
+ generated_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
28
+
29
+ # Parse the generated text into structured format
30
+ # This is a simple example - adjust based on your model's output format
31
+ lines = generated_text.split('\n')
32
+ title = lines[0]
33
+ ingredients_list = []
34
+ instructions_list = []
35
+
36
+ # Simple parsing logic - adjust based on your model's output format
37
+ current_section = None
38
+ for line in lines[1:]:
39
+ if "Ingredients:" in line:
40
+ current_section = "ingredients"
41
+ elif "Instructions:" in line:
42
+ current_section = "instructions"
43
+ elif line.strip():
44
+ if current_section == "ingredients":
45
+ ingredients_list.append(line.strip())
46
+ elif current_section == "instructions":
47
+ instructions_list.append(line.strip())
48
+
49
+ return {
50
+ "title": title,
51
+ "ingredients": ingredients_list,
52
+ "instructions": instructions_list
53
+ }