Update Backend/app/routes/recommendation.py
Browse files
Backend/app/routes/recommendation.py
CHANGED
|
@@ -1,36 +1,40 @@
|
|
| 1 |
-
from flask import Blueprint, jsonify, request
|
| 2 |
-
from app.services.recommendation import RecommendationService
|
| 3 |
-
import logging
|
| 4 |
-
|
| 5 |
-
bp = Blueprint('recommendation', __name__)
|
| 6 |
-
logger = logging.getLogger(__name__)
|
| 7 |
-
|
| 8 |
-
@bp.route('/health', methods=['GET'])
|
| 9 |
-
def health_check():
|
| 10 |
-
return jsonify({'status': 'healthy'}), 200
|
| 11 |
-
|
| 12 |
-
@bp.route('/recommend', methods=['POST'])
|
| 13 |
-
def recommend():
|
| 14 |
-
try:
|
| 15 |
-
data = request.get_json()
|
| 16 |
-
song_input = data.get('song', '').strip()
|
| 17 |
-
artist_input = data.get('artist', '').strip()
|
| 18 |
-
genre_input = data.get('genre', '').strip()
|
| 19 |
-
|
| 20 |
-
recommendation_service = RecommendationService()
|
| 21 |
-
|
| 22 |
-
if song_input:
|
| 23 |
-
recommendations = recommendation_service.get_recommendations_by_song(song_input)
|
| 24 |
-
elif artist_input and genre_input:
|
| 25 |
-
recommendations = recommendation_service.get_recommendations_by_artist_and_genre(artist_input, genre_input)
|
| 26 |
-
else:
|
| 27 |
-
return jsonify({'error': 'Invalid input. Please provide either a song or both artist and genre.'}), 400
|
| 28 |
-
|
| 29 |
-
if isinstance(recommendations, dict) and 'error' in recommendations:
|
| 30 |
-
return jsonify(recommendations), 400
|
| 31 |
-
|
| 32 |
-
return jsonify(recommendations)
|
| 33 |
-
|
| 34 |
-
except Exception as e:
|
| 35 |
-
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
| 36 |
-
return jsonify({'error': str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Blueprint, jsonify, request
|
| 2 |
+
from app.services.recommendation import RecommendationService
|
| 3 |
+
import logging
|
| 4 |
+
|
| 5 |
+
bp = Blueprint('recommendation', __name__)
|
| 6 |
+
logger = logging.getLogger(__name__)
|
| 7 |
+
|
| 8 |
+
@bp.route('/health', methods=['GET'])
|
| 9 |
+
def health_check():
|
| 10 |
+
return jsonify({'status': 'healthy'}), 200
|
| 11 |
+
|
| 12 |
+
@bp.route('/recommend', methods=['POST'])
|
| 13 |
+
def recommend():
|
| 14 |
+
try:
|
| 15 |
+
data = request.get_json()
|
| 16 |
+
song_input = data.get('song', '').strip()
|
| 17 |
+
artist_input = data.get('artist', '').strip()
|
| 18 |
+
genre_input = data.get('genre', '').strip()
|
| 19 |
+
|
| 20 |
+
recommendation_service = RecommendationService()
|
| 21 |
+
|
| 22 |
+
if song_input:
|
| 23 |
+
recommendations = recommendation_service.get_recommendations_by_song(song_input)
|
| 24 |
+
elif artist_input and genre_input:
|
| 25 |
+
recommendations = recommendation_service.get_recommendations_by_artist_and_genre(artist_input, genre_input)
|
| 26 |
+
else:
|
| 27 |
+
return jsonify({'error': 'Invalid input. Please provide either a song or both artist and genre.'}), 400
|
| 28 |
+
|
| 29 |
+
if isinstance(recommendations, dict) and 'error' in recommendations:
|
| 30 |
+
return jsonify(recommendations), 400
|
| 31 |
+
|
| 32 |
+
return jsonify(recommendations)
|
| 33 |
+
|
| 34 |
+
except Exception as e:
|
| 35 |
+
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
| 36 |
+
return jsonify({'error': str(e)}), 500
|
| 37 |
+
|
| 38 |
+
@bp.route('/health', methods=['GET'])
|
| 39 |
+
def health_check():
|
| 40 |
+
return jsonify({'status': 'healthy'}), 200
|