Spaces:
Running
Running
Sync from GitHub via hub-sync
Browse files- app/main.py +4 -2
app/main.py
CHANGED
|
@@ -38,16 +38,17 @@ def predict_sentiment(request: SentimentRequest, db: Session = Depends(get_db)):
|
|
| 38 |
try:
|
| 39 |
request_data = request.model_dump()
|
| 40 |
prediction_result = ml_model.predict(request_data["text"])
|
|
|
|
| 41 |
log_entry = InferenceLog(
|
| 42 |
input_text=request_data['text'],
|
| 43 |
sentiment_prediction=prediction_result['sentiment'],
|
| 44 |
confidence_score=prediction_result['confidence'],
|
| 45 |
-
timestamp=
|
| 46 |
)
|
| 47 |
|
| 48 |
db.add(log_entry)
|
| 49 |
db.commit()
|
| 50 |
-
db.
|
| 51 |
|
| 52 |
return SentimentResponse(
|
| 53 |
input_text=request_data["text"],
|
|
@@ -56,6 +57,7 @@ def predict_sentiment(request: SentimentRequest, db: Session = Depends(get_db)):
|
|
| 56 |
timestamp=log_entry.timestamp
|
| 57 |
)
|
| 58 |
except Exception as e:
|
|
|
|
| 59 |
raise HTTPException(status_code=500, detail=str(e))
|
| 60 |
|
| 61 |
|
|
|
|
| 38 |
try:
|
| 39 |
request_data = request.model_dump()
|
| 40 |
prediction_result = ml_model.predict(request_data["text"])
|
| 41 |
+
current_time = datetime.now(timezone.utc)
|
| 42 |
log_entry = InferenceLog(
|
| 43 |
input_text=request_data['text'],
|
| 44 |
sentiment_prediction=prediction_result['sentiment'],
|
| 45 |
confidence_score=prediction_result['confidence'],
|
| 46 |
+
timestamp=current_time
|
| 47 |
)
|
| 48 |
|
| 49 |
db.add(log_entry)
|
| 50 |
db.commit()
|
| 51 |
+
db.refresh(log_entry) # Refresh to get the generated ID and timestamp from the database
|
| 52 |
|
| 53 |
return SentimentResponse(
|
| 54 |
input_text=request_data["text"],
|
|
|
|
| 57 |
timestamp=log_entry.timestamp
|
| 58 |
)
|
| 59 |
except Exception as e:
|
| 60 |
+
db.rollback() # Rollback in case of any error during database operations
|
| 61 |
raise HTTPException(status_code=500, detail=str(e))
|
| 62 |
|
| 63 |
|