Spaces:
Sleeping
Sleeping
Update FastAPI_app.py
Browse files- FastAPI_app.py +19 -19
FastAPI_app.py
CHANGED
|
@@ -7,7 +7,6 @@ import time
|
|
| 7 |
import traceback
|
| 8 |
import threading
|
| 9 |
import signal
|
| 10 |
-
from dotenv import load_dotenv
|
| 11 |
|
| 12 |
import uvicorn
|
| 13 |
import numpy as np
|
|
@@ -25,10 +24,6 @@ import google.generativeai as genai
|
|
| 25 |
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 26 |
|
| 27 |
|
| 28 |
-
# CONFIGURATION
|
| 29 |
-
# Load environment variables
|
| 30 |
-
load_dotenv()
|
| 31 |
-
|
| 32 |
# Ingredient model (load once)
|
| 33 |
MODEL_PATH = "models/ingredient_model.h5"
|
| 34 |
if not os.path.exists(MODEL_PATH):
|
|
@@ -50,7 +45,7 @@ else:
|
|
| 50 |
'sweetpotato', 'tomato', 'turnip', 'watermelon'
|
| 51 |
]
|
| 52 |
|
| 53 |
-
#
|
| 54 |
def timeout_handler(signum, frame):
|
| 55 |
raise TimeoutError("Model load timed out after 300s")
|
| 56 |
|
|
@@ -86,21 +81,25 @@ def load_Qwen():
|
|
| 86 |
def generate_recipe_qwen(ingredient_names):
|
| 87 |
tokenizer, model = load_Qwen()
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
| 101 |
outputs = model.generate(
|
| 102 |
inputs.input_ids,
|
| 103 |
-
max_new_tokens=
|
| 104 |
temperature=0.7,
|
| 105 |
top_p=0.9,
|
| 106 |
do_sample=True
|
|
@@ -108,7 +107,8 @@ def generate_recipe_qwen(ingredient_names):
|
|
| 108 |
|
| 109 |
recipe_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 110 |
# Strip the prompt part
|
| 111 |
-
return recipe_text.split("assistant")[-1].strip()
|
|
|
|
| 112 |
|
| 113 |
|
| 114 |
# Infer uploaded image function
|
|
|
|
| 7 |
import traceback
|
| 8 |
import threading
|
| 9 |
import signal
|
|
|
|
| 10 |
|
| 11 |
import uvicorn
|
| 12 |
import numpy as np
|
|
|
|
| 24 |
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 25 |
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# Ingredient model (load once)
|
| 28 |
MODEL_PATH = "models/ingredient_model.h5"
|
| 29 |
if not os.path.exists(MODEL_PATH):
|
|
|
|
| 45 |
'sweetpotato', 'tomato', 'turnip', 'watermelon'
|
| 46 |
]
|
| 47 |
|
| 48 |
+
# Timeout handler
|
| 49 |
def timeout_handler(signum, frame):
|
| 50 |
raise TimeoutError("Model load timed out after 300s")
|
| 51 |
|
|
|
|
| 81 |
def generate_recipe_qwen(ingredient_names):
|
| 82 |
tokenizer, model = load_Qwen()
|
| 83 |
|
| 84 |
+
messages = [
|
| 85 |
+
{"role": "system", "content": "You are a helpful chef. Always respond ONLY with clean markdown, no extra text, no greetings, no explanations."},
|
| 86 |
+
{"role": "user", "content": f"""Create a delicious recipe using only these ingredients: {', '.join(ingredient_names)}
|
| 87 |
+
|
| 88 |
+
Return ONLY clean markdown with:
|
| 89 |
+
- Recipe title (## Title)
|
| 90 |
+
- One-sentence description
|
| 91 |
+
- Ingredients list with quantities
|
| 92 |
+
- Numbered steps
|
| 93 |
+
- Optional tip"""}
|
| 94 |
+
]
|
| 95 |
|
| 96 |
+
# Use Qwen chat template
|
| 97 |
+
input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 98 |
+
inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
|
| 99 |
+
|
| 100 |
outputs = model.generate(
|
| 101 |
inputs.input_ids,
|
| 102 |
+
max_new_tokens=450,
|
| 103 |
temperature=0.7,
|
| 104 |
top_p=0.9,
|
| 105 |
do_sample=True
|
|
|
|
| 107 |
|
| 108 |
recipe_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 109 |
# Strip the prompt part
|
| 110 |
+
return recipe_text.split("<|assistant|>")[-1].strip()
|
| 111 |
+
|
| 112 |
|
| 113 |
|
| 114 |
# Infer uploaded image function
|