Spaces:
Running
Running
Update app/api/routes.py
Browse files- app/api/routes.py +30 -31
app/api/routes.py
CHANGED
|
@@ -2,37 +2,16 @@ from flask import Blueprint, Response, request, jsonify, current_app
|
|
| 2 |
from app.models.recipe import Recipe
|
| 3 |
import json
|
| 4 |
import asyncio
|
| 5 |
-
import os
|
| 6 |
from app.services import extraction
|
| 7 |
from app.services import image_query
|
| 8 |
|
| 9 |
-
api_bp = Blueprint('api', __name__
|
| 10 |
-
|
| 11 |
-
@api_bp.route('/', methods=['GET'])
|
| 12 |
-
def health_check():
|
| 13 |
-
return jsonify({
|
| 14 |
-
"status": "healthy",
|
| 15 |
-
"message": "Recipe Rover API is running",
|
| 16 |
-
"endpoints": {
|
| 17 |
-
"form_data": "/api/form-data [GET]",
|
| 18 |
-
"recommend": "/api/recommend [POST]",
|
| 19 |
-
"extract_recipe": "/api/extract-recipe-attributes [POST]",
|
| 20 |
-
"analyze_image": "/api/analyze-food-image [POST]"
|
| 21 |
-
}
|
| 22 |
-
})
|
| 23 |
-
|
| 24 |
@api_bp.route('/form-data', methods=['GET'])
|
| 25 |
def get_form_data():
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
with open(file_path, 'r') as file:
|
| 30 |
-
data = file.read()
|
| 31 |
-
return data
|
| 32 |
-
except FileNotFoundError:
|
| 33 |
-
return jsonify({"error": f"File not found at {file_path}"}), 404
|
| 34 |
-
except Exception as e:
|
| 35 |
-
return jsonify({"error": str(e)}), 500
|
| 36 |
|
| 37 |
@api_bp.route('/recommend', methods=['POST'])
|
| 38 |
async def recommend_recipes(): # Make this function async
|
|
@@ -53,6 +32,11 @@ async def recommend_recipes(): # Make this function async
|
|
| 53 |
except ValueError:
|
| 54 |
return jsonify({"error": "Calories and time must be integers if provided"}), 400
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
# Use await to call the async function
|
| 57 |
recommendations = await current_app.recommendation_system.get_recommendations(
|
| 58 |
category=category,
|
|
@@ -61,7 +45,8 @@ async def recommend_recipes(): # Make this function async
|
|
| 61 |
calories=calories,
|
| 62 |
time=time,
|
| 63 |
keywords=keywords,
|
| 64 |
-
keywords_name=keywords_name
|
|
|
|
| 65 |
)
|
| 66 |
|
| 67 |
return jsonify([vars(recipe) for recipe in recommendations])
|
|
@@ -83,6 +68,11 @@ async def recommend_recipes2():
|
|
| 83 |
# Check if extraction was successful
|
| 84 |
if 'error' in extracted_info:
|
| 85 |
return jsonify(extracted_info), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
# Access the extracted attributes
|
| 88 |
category = extracted_info.get('category', '')
|
|
@@ -90,6 +80,7 @@ async def recommend_recipes2():
|
|
| 90 |
time = extracted_info.get('time', None)
|
| 91 |
keywords = extracted_info.get('keywords', [])
|
| 92 |
keywords_name = extracted_info.get('keywords_name', [])
|
|
|
|
| 93 |
|
| 94 |
# Convert calories and time to integers if they exist
|
| 95 |
try:
|
|
@@ -101,11 +92,12 @@ async def recommend_recipes2():
|
|
| 101 |
# Get recommendations using the recommendation system
|
| 102 |
recommendations = await current_app.recommendation_system.get_recommendations(
|
| 103 |
category=category,
|
| 104 |
-
ingredients=
|
| 105 |
calories=calories,
|
| 106 |
time=time,
|
| 107 |
keywords=keywords,
|
| 108 |
-
keywords_name=keywords_name
|
|
|
|
| 109 |
)
|
| 110 |
|
| 111 |
# Convert recommendations to JSON-serializable format
|
|
@@ -138,12 +130,18 @@ async def handle_analyze_food_image():
|
|
| 138 |
if 'error' in extracted_info:
|
| 139 |
return jsonify(extracted_info), 500
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
# Access the extracted attributes
|
| 142 |
category = extracted_info.get('category', '')
|
| 143 |
calories = extracted_info.get('calories', None)
|
| 144 |
time = extracted_info.get('time', None)
|
| 145 |
keywords = extracted_info.get('keywords', [])
|
| 146 |
keywords_name = extracted_info.get('keywords_name', [])
|
|
|
|
| 147 |
|
| 148 |
# Convert calories and time to integers if they exist
|
| 149 |
try:
|
|
@@ -155,11 +153,12 @@ async def handle_analyze_food_image():
|
|
| 155 |
# Get recommendations using the recommendation system
|
| 156 |
recommendations = await current_app.recommendation_system.get_recommendations(
|
| 157 |
category=category,
|
| 158 |
-
ingredients=
|
| 159 |
calories=calories,
|
| 160 |
time=time,
|
| 161 |
keywords=keywords,
|
| 162 |
-
keywords_name=keywords_name
|
|
|
|
| 163 |
)
|
| 164 |
|
| 165 |
# Convert recommendations to JSON-serializable format
|
|
|
|
| 2 |
from app.models.recipe import Recipe
|
| 3 |
import json
|
| 4 |
import asyncio
|
|
|
|
| 5 |
from app.services import extraction
|
| 6 |
from app.services import image_query
|
| 7 |
|
| 8 |
+
api_bp = Blueprint('api', __name__)
|
| 9 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
@api_bp.route('/form-data', methods=['GET'])
|
| 11 |
def get_form_data():
|
| 12 |
+
with open('form_data.json', 'r') as file:
|
| 13 |
+
data = json.load(file)
|
| 14 |
+
return jsonify(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
@api_bp.route('/recommend', methods=['POST'])
|
| 17 |
async def recommend_recipes(): # Make this function async
|
|
|
|
| 32 |
except ValueError:
|
| 33 |
return jsonify({"error": "Calories and time must be integers if provided"}), 400
|
| 34 |
|
| 35 |
+
feature_weights_recommend = {
|
| 36 |
+
'ingredients': 0.15, 'category': 0.25, 'dietary': 0.20,
|
| 37 |
+
'calories': 0.10, 'time': 0.10, 'keywords': 0.10, 'keywords_name': 0.10
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
# Use await to call the async function
|
| 41 |
recommendations = await current_app.recommendation_system.get_recommendations(
|
| 42 |
category=category,
|
|
|
|
| 45 |
calories=calories,
|
| 46 |
time=time,
|
| 47 |
keywords=keywords,
|
| 48 |
+
keywords_name=keywords_name,
|
| 49 |
+
feature_weights=feature_weights_recommend
|
| 50 |
)
|
| 51 |
|
| 52 |
return jsonify([vars(recipe) for recipe in recommendations])
|
|
|
|
| 68 |
# Check if extraction was successful
|
| 69 |
if 'error' in extracted_info:
|
| 70 |
return jsonify(extracted_info), 500
|
| 71 |
+
|
| 72 |
+
feature_weights_extract = {
|
| 73 |
+
'ingredients': 0.50, 'category': 0.0, 'dietary': 0.0,
|
| 74 |
+
'calories': 0.0, 'time': 0.0, 'keywords': 0.40, 'keywords_name': 0.10
|
| 75 |
+
}
|
| 76 |
|
| 77 |
# Access the extracted attributes
|
| 78 |
category = extracted_info.get('category', '')
|
|
|
|
| 80 |
time = extracted_info.get('time', None)
|
| 81 |
keywords = extracted_info.get('keywords', [])
|
| 82 |
keywords_name = extracted_info.get('keywords_name', [])
|
| 83 |
+
ingredients = extracted_info.get('ingredients', [])
|
| 84 |
|
| 85 |
# Convert calories and time to integers if they exist
|
| 86 |
try:
|
|
|
|
| 92 |
# Get recommendations using the recommendation system
|
| 93 |
recommendations = await current_app.recommendation_system.get_recommendations(
|
| 94 |
category=category,
|
| 95 |
+
ingredients=ingredients, # Adjust if you plan to add ingredients in the extraction function
|
| 96 |
calories=calories,
|
| 97 |
time=time,
|
| 98 |
keywords=keywords,
|
| 99 |
+
keywords_name=keywords_name,
|
| 100 |
+
feature_weights=feature_weights_extract
|
| 101 |
)
|
| 102 |
|
| 103 |
# Convert recommendations to JSON-serializable format
|
|
|
|
| 130 |
if 'error' in extracted_info:
|
| 131 |
return jsonify(extracted_info), 500
|
| 132 |
|
| 133 |
+
feature_weights_extract = {
|
| 134 |
+
'ingredients': 0.50, 'category': 0.0, 'dietary': 0.0,
|
| 135 |
+
'calories': 0.0, 'time': 0.0, 'keywords': 0.40, 'keywords_name': 0.10
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
# Access the extracted attributes
|
| 139 |
category = extracted_info.get('category', '')
|
| 140 |
calories = extracted_info.get('calories', None)
|
| 141 |
time = extracted_info.get('time', None)
|
| 142 |
keywords = extracted_info.get('keywords', [])
|
| 143 |
keywords_name = extracted_info.get('keywords_name', [])
|
| 144 |
+
ingredients = extracted_info.get('ingredients', [])
|
| 145 |
|
| 146 |
# Convert calories and time to integers if they exist
|
| 147 |
try:
|
|
|
|
| 153 |
# Get recommendations using the recommendation system
|
| 154 |
recommendations = await current_app.recommendation_system.get_recommendations(
|
| 155 |
category=category,
|
| 156 |
+
ingredients=ingredients, # Adjust if you plan to add ingredients in the extraction function
|
| 157 |
calories=calories,
|
| 158 |
time=time,
|
| 159 |
keywords=keywords,
|
| 160 |
+
keywords_name=keywords_name,
|
| 161 |
+
feature_weights=feature_weights_extract
|
| 162 |
)
|
| 163 |
|
| 164 |
# Convert recommendations to JSON-serializable format
|