Enhance database connection settings and improve error handling in mood logging functions. Added pool_pre_ping and pool_recycle options to the SQLAlchemy engine. Implemented try-except blocks to handle SQLAlchemy errors during mood log and recommendation log creation.
Browse files- app/db.py +7 -1
- app/main.py +44 -39
app/db.py
CHANGED
|
@@ -9,7 +9,13 @@ class Base(DeclarativeBase):
|
|
| 9 |
|
| 10 |
|
| 11 |
settings = get_settings()
|
| 12 |
-
engine = create_engine(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False, future=True)
|
| 14 |
|
| 15 |
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
settings = get_settings()
|
| 12 |
+
engine = create_engine(
|
| 13 |
+
settings.database_url,
|
| 14 |
+
echo=False,
|
| 15 |
+
future=True,
|
| 16 |
+
pool_pre_ping=True,
|
| 17 |
+
pool_recycle=300,
|
| 18 |
+
)
|
| 19 |
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False, future=True)
|
| 20 |
|
| 21 |
|
app/main.py
CHANGED
|
@@ -6,6 +6,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 6 |
from pydantic import BaseModel
|
| 7 |
from PIL import Image
|
| 8 |
from sqlalchemy.orm import Session
|
|
|
|
| 9 |
|
| 10 |
from .db import Base, engine, get_db
|
| 11 |
from .models import MoodLog, RecommendationLog
|
|
@@ -82,27 +83,29 @@ def mood_from_text(
|
|
| 82 |
label, score = analyze_text_emotion(body.text)
|
| 83 |
track = recommend_track_for_emotion(label, source="text")
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
rec_log = None
|
| 95 |
-
track_id = None
|
| 96 |
-
if track:
|
| 97 |
-
track_id = track["id"]
|
| 98 |
-
rec_log = RecommendationLog(
|
| 99 |
-
user_id=None,
|
| 100 |
-
mood_id=mood_log.id,
|
| 101 |
-
spotify_track_id=track_id,
|
| 102 |
)
|
| 103 |
-
db.add(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
|
|
|
|
|
|
|
| 106 |
|
| 107 |
return RecommendationResponse(
|
| 108 |
mood_label=label,
|
|
@@ -128,27 +131,29 @@ async def mood_from_face(
|
|
| 128 |
label, score = analyze_face_emotion(image)
|
| 129 |
track = recommend_track_for_emotion(label, source="face")
|
| 130 |
|
| 131 |
-
|
| 132 |
-
source="face",
|
| 133 |
-
raw_input=None,
|
| 134 |
-
emotion_label=label,
|
| 135 |
-
emotion_score=score,
|
| 136 |
-
)
|
| 137 |
-
db.add(mood_log)
|
| 138 |
-
db.flush()
|
| 139 |
-
|
| 140 |
-
rec_log = None
|
| 141 |
-
track_id = None
|
| 142 |
-
if track:
|
| 143 |
-
track_id = track["id"]
|
| 144 |
-
rec_log = RecommendationLog(
|
| 145 |
-
user_id=None,
|
| 146 |
-
mood_id=mood_log.id,
|
| 147 |
-
spotify_track_id=track_id,
|
| 148 |
-
)
|
| 149 |
-
db.add(rec_log)
|
| 150 |
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
return RecommendationResponse(
|
| 154 |
mood_label=label,
|
|
|
|
| 6 |
from pydantic import BaseModel
|
| 7 |
from PIL import Image
|
| 8 |
from sqlalchemy.orm import Session
|
| 9 |
+
from sqlalchemy.exc import SQLAlchemyError
|
| 10 |
|
| 11 |
from .db import Base, engine, get_db
|
| 12 |
from .models import MoodLog, RecommendationLog
|
|
|
|
| 83 |
label, score = analyze_text_emotion(body.text)
|
| 84 |
track = recommend_track_for_emotion(label, source="text")
|
| 85 |
|
| 86 |
+
track_id = track.get("id") if track else None
|
| 87 |
+
|
| 88 |
+
try:
|
| 89 |
+
mood_log = MoodLog(
|
| 90 |
+
source="text",
|
| 91 |
+
raw_input=body.text[:512],
|
| 92 |
+
emotion_label=label,
|
| 93 |
+
emotion_score=score,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
)
|
| 95 |
+
db.add(mood_log)
|
| 96 |
+
db.flush()
|
| 97 |
+
|
| 98 |
+
if track_id:
|
| 99 |
+
rec_log = RecommendationLog(
|
| 100 |
+
user_id=None,
|
| 101 |
+
mood_id=mood_log.id,
|
| 102 |
+
spotify_track_id=track_id,
|
| 103 |
+
)
|
| 104 |
+
db.add(rec_log)
|
| 105 |
|
| 106 |
+
db.commit()
|
| 107 |
+
except SQLAlchemyError:
|
| 108 |
+
db.rollback()
|
| 109 |
|
| 110 |
return RecommendationResponse(
|
| 111 |
mood_label=label,
|
|
|
|
| 131 |
label, score = analyze_face_emotion(image)
|
| 132 |
track = recommend_track_for_emotion(label, source="face")
|
| 133 |
|
| 134 |
+
track_id = track.get("id") if track else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
+
try:
|
| 137 |
+
mood_log = MoodLog(
|
| 138 |
+
source="face",
|
| 139 |
+
raw_input=None,
|
| 140 |
+
emotion_label=label,
|
| 141 |
+
emotion_score=score,
|
| 142 |
+
)
|
| 143 |
+
db.add(mood_log)
|
| 144 |
+
db.flush()
|
| 145 |
+
|
| 146 |
+
if track_id:
|
| 147 |
+
rec_log = RecommendationLog(
|
| 148 |
+
user_id=None,
|
| 149 |
+
mood_id=mood_log.id,
|
| 150 |
+
spotify_track_id=track_id,
|
| 151 |
+
)
|
| 152 |
+
db.add(rec_log)
|
| 153 |
+
|
| 154 |
+
db.commit()
|
| 155 |
+
except SQLAlchemyError:
|
| 156 |
+
db.rollback()
|
| 157 |
|
| 158 |
return RecommendationResponse(
|
| 159 |
mood_label=label,
|