Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -3,6 +3,8 @@ from fastapi.responses import HTMLResponse, JSONResponse
|
|
3 |
from fastapi.openapi.docs import get_swagger_ui_html
|
4 |
from fastapi.middleware.cors import CORSMiddleware
|
5 |
from pydantic import BaseModel
|
|
|
|
|
6 |
from transformers import pipeline
|
7 |
import logging, traceback
|
8 |
from typing import Optional, List, Union
|
@@ -31,6 +33,7 @@ app.add_middleware(
|
|
31 |
)
|
32 |
|
33 |
logging.basicConfig(level=logging.INFO)
|
|
|
34 |
VALID_API_KEY = "my-secret-key"
|
35 |
|
36 |
@app.get("/", response_class=HTMLResponse)
|
@@ -112,6 +115,17 @@ async def analyze(data: ReviewInput, x_api_key: str = Header(None)):
|
|
112 |
emotion_raw = detect_emotion(data.text)
|
113 |
emotion = emotion_raw["label"] if isinstance(emotion_raw, dict) and "label" in emotion_raw else str(emotion_raw)
|
114 |
churn_risk = assess_churn_risk(sentiment["label"], emotion)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
pain_points = []
|
116 |
if data.aspects:
|
117 |
from model import extract_pain_points # π Import inline if not already
|
@@ -209,3 +223,8 @@ async def bulk_analyze(data: BulkReviewInput, token: str = Query(None)):
|
|
209 |
logging.error(f"π₯ Bulk processing failed: {traceback.format_exc()}")
|
210 |
raise HTTPException(status_code=500, detail="Failed to analyze bulk reviews")
|
211 |
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from fastapi.openapi.docs import get_swagger_ui_html
|
4 |
from fastapi.middleware.cors import CORSMiddleware
|
5 |
from pydantic import BaseModel
|
6 |
+
from datetime import datetime
|
7 |
+
import uuid
|
8 |
from transformers import pipeline
|
9 |
import logging, traceback
|
10 |
from typing import Optional, List, Union
|
|
|
33 |
)
|
34 |
|
35 |
logging.basicConfig(level=logging.INFO)
|
36 |
+
log_store = []
|
37 |
VALID_API_KEY = "my-secret-key"
|
38 |
|
39 |
@app.get("/", response_class=HTMLResponse)
|
|
|
115 |
emotion_raw = detect_emotion(data.text)
|
116 |
emotion = emotion_raw["label"] if isinstance(emotion_raw, dict) and "label" in emotion_raw else str(emotion_raw)
|
117 |
churn_risk = assess_churn_risk(sentiment["label"], emotion)
|
118 |
+
# Log churn risk analysis
|
119 |
+
log_entry = {
|
120 |
+
"timestamp": datetime.now(),
|
121 |
+
"product": data.product_category or "Generic",
|
122 |
+
"churn_risk": churn_risk,
|
123 |
+
"user_id": str(uuid.uuid4()) # Optional
|
124 |
+
}
|
125 |
+
log_store.append(log_entry)
|
126 |
+
if len(log_store) > 1000:
|
127 |
+
log_store = log_store[-1000:] # keep latest 1000 entries
|
128 |
+
|
129 |
pain_points = []
|
130 |
if data.aspects:
|
131 |
from model import extract_pain_points # π Import inline if not already
|
|
|
223 |
logging.error(f"π₯ Bulk processing failed: {traceback.format_exc()}")
|
224 |
raise HTTPException(status_code=500, detail="Failed to analyze bulk reviews")
|
225 |
|
226 |
+
@app.get("/log/")
|
227 |
+
async def get_churn_log(x_api_key: str = Header(None)):
|
228 |
+
if x_api_key and x_api_key != VALID_API_KEY:
|
229 |
+
raise HTTPException(status_code=401, detail="Unauthorized")
|
230 |
+
return {"log": log_store}
|