Update app.py
Browse files
app.py
CHANGED
|
@@ -28,18 +28,21 @@ def predict():
|
|
| 28 |
input_text = f"{item['category']} - {item['subcategory']} in {item['area']}. {item.get('comments', '')}"
|
| 29 |
text_lower = input_text.lower()
|
| 30 |
|
| 31 |
-
#
|
| 32 |
critical_keywords = [
|
| 33 |
"umuusok", "sunog", "amoy sunog", "spark", "kuryente",
|
| 34 |
"leak", "baha", "gas", "short circuit", "smoke"
|
| 35 |
]
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
low_keywords = [
|
| 38 |
"lightbulb", "bumbilya", "ilaw", "palitan", "replace bulb",
|
| 39 |
-
"painting", "door knob", "hinge", "minor", "cosmetic"
|
| 40 |
]
|
| 41 |
|
| 42 |
-
#
|
| 43 |
if any(word in text_lower for word in critical_keywords):
|
| 44 |
results.append({"priority_score": 5})
|
| 45 |
continue
|
|
@@ -47,24 +50,25 @@ def predict():
|
|
| 47 |
results.append({"priority_score": 2})
|
| 48 |
continue
|
| 49 |
|
| 50 |
-
#
|
| 51 |
inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True)
|
| 52 |
-
|
| 53 |
with torch.no_grad():
|
| 54 |
outputs = model(**inputs)
|
| 55 |
predicted_class = torch.argmax(outputs.logits, dim=1).item()
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
priority_score = predicted_class + 1
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
if
|
| 62 |
-
priority_score =
|
|
|
|
|
|
|
| 63 |
elif priority_score <= 1:
|
| 64 |
-
priority_score = 2
|
| 65 |
|
| 66 |
results.append({"priority_score": priority_score})
|
| 67 |
|
|
|
|
| 68 |
return jsonify(results)
|
| 69 |
|
| 70 |
except Exception as e:
|
|
|
|
| 28 |
input_text = f"{item['category']} - {item['subcategory']} in {item['area']}. {item.get('comments', '')}"
|
| 29 |
text_lower = input_text.lower()
|
| 30 |
|
| 31 |
+
# --- Keyword heuristics ---
|
| 32 |
critical_keywords = [
|
| 33 |
"umuusok", "sunog", "amoy sunog", "spark", "kuryente",
|
| 34 |
"leak", "baha", "gas", "short circuit", "smoke"
|
| 35 |
]
|
| 36 |
+
medium_keywords = [
|
| 37 |
+
"crack", "tagas", "sira", "damage", "malfunction", "no power",
|
| 38 |
+
"no water", "not working", "problem", "repair"
|
| 39 |
+
]
|
| 40 |
low_keywords = [
|
| 41 |
"lightbulb", "bumbilya", "ilaw", "palitan", "replace bulb",
|
| 42 |
+
"painting", "door knob", "hinge", "minor", "cosmetic", "linis", "clean"
|
| 43 |
]
|
| 44 |
|
| 45 |
+
# --- Immediate overrides ---
|
| 46 |
if any(word in text_lower for word in critical_keywords):
|
| 47 |
results.append({"priority_score": 5})
|
| 48 |
continue
|
|
|
|
| 50 |
results.append({"priority_score": 2})
|
| 51 |
continue
|
| 52 |
|
| 53 |
+
# --- Model prediction ---
|
| 54 |
inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True)
|
|
|
|
| 55 |
with torch.no_grad():
|
| 56 |
outputs = model(**inputs)
|
| 57 |
predicted_class = torch.argmax(outputs.logits, dim=1).item()
|
| 58 |
|
| 59 |
+
priority_score = predicted_class + 1 # convert 0β4 β 1β5
|
|
|
|
| 60 |
|
| 61 |
+
# --- Intelligent moderation ---
|
| 62 |
+
if any(word in text_lower for word in medium_keywords):
|
| 63 |
+
priority_score = min(max(priority_score, 3), 4)
|
| 64 |
+
elif priority_score >= 5:
|
| 65 |
+
priority_score = 4
|
| 66 |
elif priority_score <= 1:
|
| 67 |
+
priority_score = 2
|
| 68 |
|
| 69 |
results.append({"priority_score": priority_score})
|
| 70 |
|
| 71 |
+
|
| 72 |
return jsonify(results)
|
| 73 |
|
| 74 |
except Exception as e:
|