Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -33,8 +33,8 @@ app.add_middleware(
|
|
| 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)
|
| 40 |
def root():
|
|
@@ -115,6 +115,7 @@ async def analyze(data: ReviewInput, x_api_key: str = Header(None)):
|
|
| 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(),
|
|
@@ -170,6 +171,13 @@ async def followup(request: FollowUpRequest, x_api_key: str = Header(None)):
|
|
| 170 |
logging.error(f"β Follow-up failed: {traceback.format_exc()}")
|
| 171 |
raise HTTPException(status_code=500, detail="Internal Server Error during follow-up.")
|
| 172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
@app.post("/bulk/")
|
| 174 |
async def bulk_analyze(data: BulkReviewInput, token: str = Query(None)):
|
| 175 |
if token != VALID_API_KEY:
|
|
@@ -190,9 +198,21 @@ async def bulk_analyze(data: BulkReviewInput, token: str = Query(None)):
|
|
| 190 |
summary = smart_summarize(review_text, n_clusters=2 if data.intelligence else 1)
|
| 191 |
sentiment = sentiment_pipeline(review_text)[0]
|
| 192 |
emotion = detect_emotion(review_text)
|
|
|
|
| 193 |
churn = assess_churn_risk(sentiment["label"], emotion)
|
| 194 |
pain = extract_pain_points(review_text) if data.aspects else []
|
| 195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
ind = auto_fill(data.industry[i] if data.industry else None, detect_industry(review_text))
|
| 198 |
prod = auto_fill(data.product_category[i] if data.product_category else None, detect_product_category(review_text))
|
|
@@ -223,8 +243,3 @@ async def bulk_analyze(data: BulkReviewInput, token: str = Query(None)):
|
|
| 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}
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 36 |
VALID_API_KEY = "my-secret-key"
|
| 37 |
+
log_store = []
|
| 38 |
|
| 39 |
@app.get("/", response_class=HTMLResponse)
|
| 40 |
def root():
|
|
|
|
| 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 |
+
|
| 119 |
# Log churn risk analysis
|
| 120 |
log_entry = {
|
| 121 |
"timestamp": datetime.now(),
|
|
|
|
| 171 |
logging.error(f"β Follow-up failed: {traceback.format_exc()}")
|
| 172 |
raise HTTPException(status_code=500, detail="Internal Server Error during follow-up.")
|
| 173 |
|
| 174 |
+
@app.get("/log/")
|
| 175 |
+
async def get_churn_log(x_api_key: str = Header(None)):
|
| 176 |
+
if x_api_key and x_api_key != VALID_API_KEY:
|
| 177 |
+
raise HTTPException(status_code=401, detail="Unauthorized")
|
| 178 |
+
return {"log": log_store}
|
| 179 |
+
|
| 180 |
+
|
| 181 |
@app.post("/bulk/")
|
| 182 |
async def bulk_analyze(data: BulkReviewInput, token: str = Query(None)):
|
| 183 |
if token != VALID_API_KEY:
|
|
|
|
| 198 |
summary = smart_summarize(review_text, n_clusters=2 if data.intelligence else 1)
|
| 199 |
sentiment = sentiment_pipeline(review_text)[0]
|
| 200 |
emotion = detect_emotion(review_text)
|
| 201 |
+
|
| 202 |
churn = assess_churn_risk(sentiment["label"], emotion)
|
| 203 |
pain = extract_pain_points(review_text) if data.aspects else []
|
| 204 |
|
| 205 |
+
# π Log churn data
|
| 206 |
+
log_entry = {
|
| 207 |
+
"timestamp": datetime.now(),
|
| 208 |
+
"product": prod,
|
| 209 |
+
"churn_risk": churn,
|
| 210 |
+
"user_id": str(uuid.uuid4())
|
| 211 |
+
}
|
| 212 |
+
log_store.append(log_entry)
|
| 213 |
+
if len(log_store) > 1000:
|
| 214 |
+
log_store = log_store[-1000:]
|
| 215 |
+
|
| 216 |
|
| 217 |
ind = auto_fill(data.industry[i] if data.industry else None, detect_industry(review_text))
|
| 218 |
prod = auto_fill(data.product_category[i] if data.product_category else None, detect_product_category(review_text))
|
|
|
|
| 243 |
logging.error(f"π₯ Bulk processing failed: {traceback.format_exc()}")
|
| 244 |
raise HTTPException(status_code=500, detail="Failed to analyze bulk reviews")
|
| 245 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|