Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import re
|
| 3 |
from datetime import datetime
|
| 4 |
-
from typing import Dict, Any
|
|
|
|
| 5 |
|
| 6 |
# -------------------------
|
| 7 |
# Document Classifier Class
|
|
@@ -21,23 +22,44 @@ class DocumentClassifier:
|
|
| 21 |
def extract_features(self, document_text: str) -> Dict[str, int]:
|
| 22 |
text_lower = document_text.lower()
|
| 23 |
features = {}
|
|
|
|
|
|
|
| 24 |
for category, keywords in self.categories.items():
|
| 25 |
feature_count = sum(text_lower.count(keyword) for keyword in keywords)
|
| 26 |
features[f'{category}_score'] = feature_count
|
|
|
|
| 27 |
features['word_count'] = len(text_lower.split())
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
return features
|
| 31 |
|
| 32 |
def classify_document(self, document_text: str, filename: str = "") -> Dict[str, Any]:
|
| 33 |
features = self.extract_features(document_text)
|
| 34 |
scores = {}
|
| 35 |
-
|
|
|
|
| 36 |
base_score = features.get(f'{category}_score', 0)
|
| 37 |
normalized_score = base_score / max(features['word_count'] / 100, 1)
|
| 38 |
scores[category] = normalized_score
|
| 39 |
-
|
|
|
|
| 40 |
confidence = min(scores[best_category] / 5.0, 1.0)
|
|
|
|
| 41 |
return {
|
| 42 |
'filename': filename,
|
| 43 |
'predicted_category': best_category,
|
|
@@ -47,10 +69,12 @@ class DocumentClassifier:
|
|
| 47 |
'features': features
|
| 48 |
}
|
| 49 |
|
|
|
|
| 50 |
# -------------------------
|
| 51 |
# Streamlit UI
|
| 52 |
# -------------------------
|
| 53 |
|
|
|
|
| 54 |
st.title("π AI-Based Document Classifier")
|
| 55 |
st.subheader("Upload government text files for automatic classification")
|
| 56 |
|
|
@@ -66,8 +90,12 @@ if uploaded_files:
|
|
| 66 |
result = classifier.classify_document(content, filename)
|
| 67 |
|
| 68 |
st.markdown("---")
|
| 69 |
-
st.write(f"**Document:** {result['filename']}")
|
| 70 |
-
st.write(f"**Predicted Category:** `{result['predicted_category']}`")
|
| 71 |
-
st.write(f"**Confidence:** `{result['confidence']:.2f}`")
|
| 72 |
-
|
|
|
|
| 73 |
st.json(result['features'])
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import re
|
| 3 |
from datetime import datetime
|
| 4 |
+
from typing import Dict, Any
|
| 5 |
+
|
| 6 |
|
| 7 |
# -------------------------
|
| 8 |
# Document Classifier Class
|
|
|
|
| 22 |
def extract_features(self, document_text: str) -> Dict[str, int]:
|
| 23 |
text_lower = document_text.lower()
|
| 24 |
features = {}
|
| 25 |
+
|
| 26 |
+
# Score each category based on keywords
|
| 27 |
for category, keywords in self.categories.items():
|
| 28 |
feature_count = sum(text_lower.count(keyword) for keyword in keywords)
|
| 29 |
features[f'{category}_score'] = feature_count
|
| 30 |
+
|
| 31 |
features['word_count'] = len(text_lower.split())
|
| 32 |
+
|
| 33 |
+
# Date patterns (DD Month YYYY, 7th January 2025, 12/02/2023 etc.)
|
| 34 |
+
date_patterns = [
|
| 35 |
+
r'\b\d{1,2}[-/]\d{1,2}[-/]\d{4}\b', # e.g., 12/02/2023
|
| 36 |
+
r'\b\d{1,2}(?:st|nd|rd|th)?\s+[A-Z][a-z]+,\s+\d{4}\b', # e.g., 7th January, 2025
|
| 37 |
+
r'\b\d{1,2}(?:st|nd|rd|th)?\s+[A-Z][a-z]+\s+\d{4}\b', # e.g., 29 January 2025
|
| 38 |
+
r'\b[A-Z][a-z]+\s+\d{1,2},\s+\d{4}\b', # e.g., January 29, 2025
|
| 39 |
+
]
|
| 40 |
+
features['has_date'] = 1 if any(re.search(p, document_text) for p in date_patterns) else 0
|
| 41 |
+
|
| 42 |
+
# Reference patterns (e.g., "No. DD (STI)/5(1)2024-25" or "Ref No: XYZ")
|
| 43 |
+
ref_patterns = [
|
| 44 |
+
r'\bref[.\s:]*[A-Za-z0-9/\-()]+', # Ref No: XYZ-123
|
| 45 |
+
r'\bno[.\s:]*[A-Za-z0-9/\-()]+', # No. DD/XYZ-123
|
| 46 |
+
]
|
| 47 |
+
features['has_reference'] = 1 if any(re.search(p, text_lower) for p in ref_patterns) else 0
|
| 48 |
+
|
| 49 |
return features
|
| 50 |
|
| 51 |
def classify_document(self, document_text: str, filename: str = "") -> Dict[str, Any]:
|
| 52 |
features = self.extract_features(document_text)
|
| 53 |
scores = {}
|
| 54 |
+
|
| 55 |
+
for category in self.categories:
|
| 56 |
base_score = features.get(f'{category}_score', 0)
|
| 57 |
normalized_score = base_score / max(features['word_count'] / 100, 1)
|
| 58 |
scores[category] = normalized_score
|
| 59 |
+
|
| 60 |
+
best_category = max(scores, key=scores.get)
|
| 61 |
confidence = min(scores[best_category] / 5.0, 1.0)
|
| 62 |
+
|
| 63 |
return {
|
| 64 |
'filename': filename,
|
| 65 |
'predicted_category': best_category,
|
|
|
|
| 69 |
'features': features
|
| 70 |
}
|
| 71 |
|
| 72 |
+
|
| 73 |
# -------------------------
|
| 74 |
# Streamlit UI
|
| 75 |
# -------------------------
|
| 76 |
|
| 77 |
+
st.set_page_config(page_title="Document Classifier", layout="wide")
|
| 78 |
st.title("π AI-Based Document Classifier")
|
| 79 |
st.subheader("Upload government text files for automatic classification")
|
| 80 |
|
|
|
|
| 90 |
result = classifier.classify_document(content, filename)
|
| 91 |
|
| 92 |
st.markdown("---")
|
| 93 |
+
st.write(f"**π Document:** `{result['filename']}`")
|
| 94 |
+
st.write(f"**π Predicted Category:** `{result['predicted_category']}`")
|
| 95 |
+
st.write(f"**π Confidence:** `{result['confidence']:.2f}`")
|
| 96 |
+
|
| 97 |
+
with st.expander("π Feature Details", expanded=False):
|
| 98 |
st.json(result['features'])
|
| 99 |
+
|
| 100 |
+
with st.expander("π Full Text", expanded=False):
|
| 101 |
+
st.text(content)
|