Spaces:
Running
Running
Syed Arfan
Claude
commited on
Commit
·
52cdaa9
1
Parent(s):
b789b98
Add /history endpoint for analysis history
Browse files- Added GET /history endpoint with limit parameter
- Returns total count and list of recent analyses
- Ordered by creation date (newest first)
- Matches frontend HistoryResponse type
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
- src/main.py +51 -2
src/main.py
CHANGED
|
@@ -30,7 +30,7 @@ sentiment_analyzer = pipeline(
|
|
| 30 |
print("Model loaded!")
|
| 31 |
|
| 32 |
class TextRequest(BaseModel):
|
| 33 |
-
text: str = Field(..., min_length=1, max_length=512,
|
| 34 |
example="I love this product!")
|
| 35 |
|
| 36 |
class SentimentResponse(BaseModel):
|
|
@@ -38,7 +38,19 @@ class SentimentResponse(BaseModel):
|
|
| 38 |
sentiment: str
|
| 39 |
confidence: float
|
| 40 |
processing_time_ms: int
|
| 41 |
-
cached: bool = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
@app.get("/")
|
| 44 |
def root():
|
|
@@ -116,6 +128,43 @@ def health():
|
|
| 116 |
"""Kubernetes-style health check"""
|
| 117 |
return {"status": "ok"}
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
@app.get("/cache/stats")
|
| 120 |
def get_cache_statistics():
|
| 121 |
"""
|
|
|
|
| 30 |
print("Model loaded!")
|
| 31 |
|
| 32 |
class TextRequest(BaseModel):
|
| 33 |
+
text: str = Field(..., min_length=1, max_length=512,
|
| 34 |
example="I love this product!")
|
| 35 |
|
| 36 |
class SentimentResponse(BaseModel):
|
|
|
|
| 38 |
sentiment: str
|
| 39 |
confidence: float
|
| 40 |
processing_time_ms: int
|
| 41 |
+
cached: bool = False
|
| 42 |
+
|
| 43 |
+
class HistoryItem(BaseModel):
|
| 44 |
+
id: int
|
| 45 |
+
text: str
|
| 46 |
+
sentiment: str
|
| 47 |
+
confidence: float
|
| 48 |
+
processing_time_ms: int
|
| 49 |
+
created_at: str
|
| 50 |
+
|
| 51 |
+
class HistoryResponse(BaseModel):
|
| 52 |
+
total: int
|
| 53 |
+
analyses: list[HistoryItem]
|
| 54 |
|
| 55 |
@app.get("/")
|
| 56 |
def root():
|
|
|
|
| 128 |
"""Kubernetes-style health check"""
|
| 129 |
return {"status": "ok"}
|
| 130 |
|
| 131 |
+
@app.get("/history", response_model=HistoryResponse)
|
| 132 |
+
def get_history(
|
| 133 |
+
limit: int = 10,
|
| 134 |
+
db: Session = Depends(get_db)
|
| 135 |
+
):
|
| 136 |
+
"""
|
| 137 |
+
Get sentiment analysis history from database
|
| 138 |
+
|
| 139 |
+
Returns recent analyses ordered by creation date (newest first)
|
| 140 |
+
"""
|
| 141 |
+
try:
|
| 142 |
+
# Get total count
|
| 143 |
+
total = db.query(SentimentAnalysis).count()
|
| 144 |
+
|
| 145 |
+
# Get recent analyses
|
| 146 |
+
analyses = db.query(SentimentAnalysis)\
|
| 147 |
+
.order_by(SentimentAnalysis.created_at.desc())\
|
| 148 |
+
.limit(limit)\
|
| 149 |
+
.all()
|
| 150 |
+
|
| 151 |
+
# Convert to response format
|
| 152 |
+
history_items = [
|
| 153 |
+
HistoryItem(
|
| 154 |
+
id=a.id,
|
| 155 |
+
text=a.text,
|
| 156 |
+
sentiment=a.sentiment,
|
| 157 |
+
confidence=a.confidence,
|
| 158 |
+
processing_time_ms=a.processing_time_ms,
|
| 159 |
+
created_at=a.created_at.isoformat()
|
| 160 |
+
)
|
| 161 |
+
for a in analyses
|
| 162 |
+
]
|
| 163 |
+
|
| 164 |
+
return HistoryResponse(total=total, analyses=history_items)
|
| 165 |
+
except Exception as e:
|
| 166 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 167 |
+
|
| 168 |
@app.get("/cache/stats")
|
| 169 |
def get_cache_statistics():
|
| 170 |
"""
|