Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import faiss
|
| 4 |
import torch
|
|
@@ -18,6 +19,10 @@ index = None
|
|
| 18 |
df = None
|
| 19 |
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
@app.on_event("startup")
|
| 22 |
def load_assets():
|
| 23 |
global model, index, df
|
|
@@ -42,24 +47,35 @@ def load_assets():
|
|
| 42 |
print("✅ All assets loaded successfully!")
|
| 43 |
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
-
@app.get("/search")
|
| 51 |
def clean_instructions(instruction_input):
|
| 52 |
if isinstance(instruction_input, str) and instruction_input.startswith('c("'):
|
| 53 |
-
|
| 54 |
content = re.search(r'c\("(.*)"\)', instruction_input)
|
| 55 |
if content:
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
|
| 59 |
if isinstance(instruction_input, (list, np.ndarray)):
|
| 60 |
return list(instruction_input)
|
| 61 |
|
| 62 |
return [str(instruction_input)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
def search(query: str):
|
| 64 |
query_vector = model.encode([query])
|
| 65 |
faiss.normalize_L2(query_vector)
|
|
@@ -82,7 +98,7 @@ def search(query: str):
|
|
| 82 |
),
|
| 83 |
"calories": float(row["calories"]),
|
| 84 |
"protein": float(row["protein"]),
|
| 85 |
-
"instructions": clean_instructions(row[
|
| 86 |
})
|
| 87 |
|
| 88 |
return {
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
import pandas as pd
|
| 4 |
import faiss
|
| 5 |
import torch
|
|
|
|
| 19 |
df = None
|
| 20 |
|
| 21 |
|
| 22 |
+
# ==============================
|
| 23 |
+
# STARTUP EVENT
|
| 24 |
+
# ==============================
|
| 25 |
+
|
| 26 |
@app.on_event("startup")
|
| 27 |
def load_assets():
|
| 28 |
global model, index, df
|
|
|
|
| 47 |
print("✅ All assets loaded successfully!")
|
| 48 |
|
| 49 |
|
| 50 |
+
# ==============================
|
| 51 |
+
# UTILITY FUNCTION
|
| 52 |
+
# ==============================
|
|
|
|
| 53 |
|
|
|
|
| 54 |
def clean_instructions(instruction_input):
|
| 55 |
if isinstance(instruction_input, str) and instruction_input.startswith('c("'):
|
|
|
|
| 56 |
content = re.search(r'c\("(.*)"\)', instruction_input)
|
| 57 |
if content:
|
| 58 |
+
return [
|
| 59 |
+
step.strip().strip('"')
|
| 60 |
+
for step in content.group(1).split('", "')
|
| 61 |
+
]
|
| 62 |
|
| 63 |
if isinstance(instruction_input, (list, np.ndarray)):
|
| 64 |
return list(instruction_input)
|
| 65 |
|
| 66 |
return [str(instruction_input)]
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# ==============================
|
| 70 |
+
# ROUTES
|
| 71 |
+
# ==============================
|
| 72 |
+
|
| 73 |
+
@app.get("/")
|
| 74 |
+
def home():
|
| 75 |
+
return {"status": "KitchenElite API Running 🚀"}
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
@app.get("/search")
|
| 79 |
def search(query: str):
|
| 80 |
query_vector = model.encode([query])
|
| 81 |
faiss.normalize_L2(query_vector)
|
|
|
|
| 98 |
),
|
| 99 |
"calories": float(row["calories"]),
|
| 100 |
"protein": float(row["protein"]),
|
| 101 |
+
"instructions": clean_instructions(row["RecipeInstructions"])
|
| 102 |
})
|
| 103 |
|
| 104 |
return {
|