Spaces:
Sleeping
Sleeping
Moved severity classifier to utils and exposed endpoint
Browse files- main.py +3 -11
- utils/severity_classifier.py +48 -0
main.py
CHANGED
|
@@ -1,20 +1,15 @@
|
|
| 1 |
-
# main.py
|
| 2 |
-
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from pydantic import BaseModel
|
| 5 |
-
from typing import List
|
| 6 |
from core.preprocess import preprocess_text_list
|
| 7 |
from core.analyzer import analyze_feedback
|
| 8 |
from utils.counter import aggregate_results
|
| 9 |
|
| 10 |
-
# ✅
|
| 11 |
-
from
|
| 12 |
|
| 13 |
app = FastAPI(title="Batch Sentiment + Severity Classifier")
|
| 14 |
|
| 15 |
-
# -----------------------
|
| 16 |
-
# Route 1: Analyze Batch
|
| 17 |
-
# -----------------------
|
| 18 |
class FeedbackBatch(BaseModel):
|
| 19 |
texts: List[str]
|
| 20 |
|
|
@@ -25,9 +20,6 @@ def analyze_batch_endpoint(batch: FeedbackBatch):
|
|
| 25 |
summary = aggregate_results(results)
|
| 26 |
return summary
|
| 27 |
|
| 28 |
-
# -----------------------
|
| 29 |
-
# Route 2: Classify Severity
|
| 30 |
-
# -----------------------
|
| 31 |
@app.post("/classify-severity")
|
| 32 |
def classify_severity_endpoint(payload: InputPayload):
|
| 33 |
return classify_issues(payload)
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
+
from typing import List
|
| 4 |
from core.preprocess import preprocess_text_list
|
| 5 |
from core.analyzer import analyze_feedback
|
| 6 |
from utils.counter import aggregate_results
|
| 7 |
|
| 8 |
+
# ✅ NEW import (moved from severity_service)
|
| 9 |
+
from utils.severity_classifier import classify_issues, InputPayload
|
| 10 |
|
| 11 |
app = FastAPI(title="Batch Sentiment + Severity Classifier")
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
class FeedbackBatch(BaseModel):
|
| 14 |
texts: List[str]
|
| 15 |
|
|
|
|
| 20 |
summary = aggregate_results(results)
|
| 21 |
return summary
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
@app.post("/classify-severity")
|
| 24 |
def classify_severity_endpoint(payload: InputPayload):
|
| 25 |
return classify_issues(payload)
|
utils/severity_classifier.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
+
from typing import Dict
|
| 3 |
+
|
| 4 |
+
# Input schema
|
| 5 |
+
class IssueData(BaseModel):
|
| 6 |
+
count: int
|
| 7 |
+
severity: int
|
| 8 |
+
impact_score: int
|
| 9 |
+
|
| 10 |
+
class EmotionSummary(BaseModel):
|
| 11 |
+
positive_total: int
|
| 12 |
+
negative_total: int
|
| 13 |
+
overall_mood: str
|
| 14 |
+
|
| 15 |
+
class InputPayload(BaseModel):
|
| 16 |
+
issues: Dict[str, IssueData]
|
| 17 |
+
positive_emotions: Dict[str, int]
|
| 18 |
+
negative_emotions: Dict[str, int]
|
| 19 |
+
emotion_summary: EmotionSummary
|
| 20 |
+
|
| 21 |
+
# Scoring logic
|
| 22 |
+
def compute_combined_score(issue: IssueData, alpha: float = 1.0, beta: float = 4.0) -> float:
|
| 23 |
+
return alpha * issue.impact_score + beta * issue.severity
|
| 24 |
+
|
| 25 |
+
def classify_severity(score: float) -> str:
|
| 26 |
+
if score < 20:
|
| 27 |
+
return "low"
|
| 28 |
+
elif score < 40:
|
| 29 |
+
return "medium"
|
| 30 |
+
else:
|
| 31 |
+
return "high"
|
| 32 |
+
|
| 33 |
+
def classify_issues(payload: InputPayload):
|
| 34 |
+
results = {}
|
| 35 |
+
for issue_name, issue_data in payload.issues.items():
|
| 36 |
+
combined_score = compute_combined_score(issue_data)
|
| 37 |
+
final_sev = classify_severity(combined_score)
|
| 38 |
+
results[issue_name] = {
|
| 39 |
+
"combined_score": combined_score,
|
| 40 |
+
"final_severity": final_sev,
|
| 41 |
+
"original_count": issue_data.count,
|
| 42 |
+
"original_impact_score": issue_data.impact_score,
|
| 43 |
+
"original_severity": issue_data.severity
|
| 44 |
+
}
|
| 45 |
+
return {
|
| 46 |
+
"classified_issues": results,
|
| 47 |
+
"overall_mood": payload.emotion_summary.overall_mood
|
| 48 |
+
}
|