Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import re
|
| 3 |
from datetime import datetime
|
| 4 |
-
from typing import Dict, Any,
|
| 5 |
|
| 6 |
|
| 7 |
# -------------------------
|
|
@@ -9,6 +9,13 @@ from typing import Dict, Any, Optional
|
|
| 9 |
# -------------------------
|
| 10 |
|
| 11 |
class DocumentClassifier:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def __init__(self):
|
| 13 |
self.categories = {
|
| 14 |
'ADMINISTRATIVE': ['memo', 'circular', 'notification', 'order', 'policy'],
|
|
@@ -17,24 +24,27 @@ class DocumentClassifier:
|
|
| 17 |
'HR': ['recruitment', 'promotion', 'transfer', 'leave', 'employee'],
|
| 18 |
'TECHNICAL': ['specification', 'tender', 'project', 'development', 'infrastructure']
|
| 19 |
}
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def extract_features(self, document_text: str) -> Dict[str, Any]:
|
|
|
|
| 22 |
text_lower = document_text.lower()
|
| 23 |
features = {}
|
| 24 |
|
| 25 |
-
#
|
| 26 |
for category, keywords in self.categories.items():
|
| 27 |
feature_count = sum(text_lower.count(keyword) for keyword in keywords)
|
| 28 |
features[f'{category}_score'] = feature_count
|
| 29 |
|
| 30 |
features['word_count'] = len(text_lower.split())
|
| 31 |
|
| 32 |
-
# Date
|
| 33 |
date_patterns = [
|
| 34 |
-
r'\b\d{1,2}[-/]\d{1,2}[-/]\d{4}\b',
|
| 35 |
-
r'\b\d{1,2}(?:st|nd|rd|th)?\s+[A-Z][a-z]+,\s+\d{4}\b',
|
| 36 |
-
r'\b\d{1,2}(?:st|nd|rd|th)?\s+[A-Z][a-z]+\s+\d{4}\b',
|
| 37 |
-
r'\b[A-Z][a-z]+\s+\d{1,2},\s+\d{4}\b'
|
| 38 |
]
|
| 39 |
found_date = None
|
| 40 |
for pattern in date_patterns:
|
|
@@ -45,10 +55,10 @@ class DocumentClassifier:
|
|
| 45 |
features['has_date'] = 1 if found_date else 0
|
| 46 |
features['extracted_date'] = found_date if found_date else "N/A"
|
| 47 |
|
| 48 |
-
# Reference number
|
| 49 |
ref_patterns = [
|
| 50 |
-
r'\bref(?:\.|\s|:)*[A-Za-z0-9/\-()]+',
|
| 51 |
-
r'\bno(?:\.|\s|:)*[A-Za-z0-9/\-()]+'
|
| 52 |
]
|
| 53 |
found_ref = None
|
| 54 |
for pattern in ref_patterns:
|
|
@@ -62,11 +72,13 @@ class DocumentClassifier:
|
|
| 62 |
return features
|
| 63 |
|
| 64 |
def classify_document(self, document_text: str, filename: str = "") -> Dict[str, Any]:
|
|
|
|
| 65 |
features = self.extract_features(document_text)
|
| 66 |
scores = {}
|
| 67 |
|
| 68 |
for category in self.categories:
|
| 69 |
-
|
|
|
|
| 70 |
normalized_score = base_score / max(features['word_count'] / 100, 1)
|
| 71 |
scores[category] = normalized_score
|
| 72 |
|
|
@@ -82,12 +94,21 @@ class DocumentClassifier:
|
|
| 82 |
'features': features
|
| 83 |
}
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
# -------------------------
|
| 87 |
-
# Streamlit UI
|
| 88 |
# -------------------------
|
| 89 |
|
| 90 |
-
st.set_page_config(page_title="Document Classifier", layout="centered")
|
| 91 |
st.title("π AI-Based Document Classifier")
|
| 92 |
st.subheader("Upload government text files for automatic classification")
|
| 93 |
|
|
@@ -97,17 +118,24 @@ uploaded_files = st.file_uploader(
|
|
| 97 |
|
| 98 |
if uploaded_files:
|
| 99 |
classifier = DocumentClassifier()
|
|
|
|
|
|
|
|
|
|
| 100 |
for uploaded_file in uploaded_files:
|
| 101 |
filename = uploaded_file.name
|
| 102 |
content = uploaded_file.read().decode("utf-8")
|
| 103 |
-
|
|
|
|
|
|
|
| 104 |
|
|
|
|
|
|
|
| 105 |
st.markdown("---")
|
| 106 |
st.markdown(f"π **Document:** `{result['filename']}`")
|
| 107 |
st.markdown(f"π **Predicted Category:** `{result['predicted_category']}`")
|
| 108 |
st.markdown(f"π **Confidence:** `{result['confidence']:.2f}`")
|
| 109 |
|
| 110 |
-
#
|
| 111 |
extracted_date = result["features"].get("extracted_date", "N/A")
|
| 112 |
extracted_ref = result["features"].get("extracted_reference", "N/A")
|
| 113 |
|
|
@@ -121,4 +149,4 @@ if uploaded_files:
|
|
| 121 |
st.json(result['features'])
|
| 122 |
|
| 123 |
with st.expander("π Full Text", expanded=False):
|
| 124 |
-
st.text(content)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import re
|
| 3 |
from datetime import datetime
|
| 4 |
+
from typing import Dict, Any, List
|
| 5 |
|
| 6 |
|
| 7 |
# -------------------------
|
|
|
|
| 9 |
# -------------------------
|
| 10 |
|
| 11 |
class DocumentClassifier:
|
| 12 |
+
"""
|
| 13 |
+
AI Document Classification System for Government E-Filing
|
| 14 |
+
|
| 15 |
+
This class demonstrates how AI can automatically classify government documents
|
| 16 |
+
into predefined categories based on content analysis.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
def __init__(self):
|
| 20 |
self.categories = {
|
| 21 |
'ADMINISTRATIVE': ['memo', 'circular', 'notification', 'order', 'policy'],
|
|
|
|
| 24 |
'HR': ['recruitment', 'promotion', 'transfer', 'leave', 'employee'],
|
| 25 |
'TECHNICAL': ['specification', 'tender', 'project', 'development', 'infrastructure']
|
| 26 |
}
|
| 27 |
+
self.confidence_threshold = 0.7
|
| 28 |
+
print("Document Classifier initialized with government document categories")
|
| 29 |
|
| 30 |
def extract_features(self, document_text: str) -> Dict[str, Any]:
|
| 31 |
+
"""Extract features from document text for classification"""
|
| 32 |
text_lower = document_text.lower()
|
| 33 |
features = {}
|
| 34 |
|
| 35 |
+
# Count occurrences of category-specific keywords
|
| 36 |
for category, keywords in self.categories.items():
|
| 37 |
feature_count = sum(text_lower.count(keyword) for keyword in keywords)
|
| 38 |
features[f'{category}_score'] = feature_count
|
| 39 |
|
| 40 |
features['word_count'] = len(text_lower.split())
|
| 41 |
|
| 42 |
+
# Date extraction
|
| 43 |
date_patterns = [
|
| 44 |
+
r'\b\d{1,2}[-/]\d{1,2}[-/]\d{4}\b',
|
| 45 |
+
r'\b\d{1,2}(?:st|nd|rd|th)?\s+[A-Z][a-z]+,\s+\d{4}\b',
|
| 46 |
+
r'\b\d{1,2}(?:st|nd|rd|th)?\s+[A-Z][a-z]+\s+\d{4}\b',
|
| 47 |
+
r'\b[A-Z][a-z]+\s+\d{1,2},\s+\d{4}\b'
|
| 48 |
]
|
| 49 |
found_date = None
|
| 50 |
for pattern in date_patterns:
|
|
|
|
| 55 |
features['has_date'] = 1 if found_date else 0
|
| 56 |
features['extracted_date'] = found_date if found_date else "N/A"
|
| 57 |
|
| 58 |
+
# Reference number extraction
|
| 59 |
ref_patterns = [
|
| 60 |
+
r'\bref(?:\.|\s|:)*[A-Za-z0-9/\-()]+',
|
| 61 |
+
r'\bno(?:\.|\s|:)*[A-Za-z0-9/\-()]+'
|
| 62 |
]
|
| 63 |
found_ref = None
|
| 64 |
for pattern in ref_patterns:
|
|
|
|
| 72 |
return features
|
| 73 |
|
| 74 |
def classify_document(self, document_text: str, filename: str = "") -> Dict[str, Any]:
|
| 75 |
+
"""Classify a document using AI-based content analysis"""
|
| 76 |
features = self.extract_features(document_text)
|
| 77 |
scores = {}
|
| 78 |
|
| 79 |
for category in self.categories:
|
| 80 |
+
score_key = f'{category}_score'
|
| 81 |
+
base_score = features.get(score_key, 0)
|
| 82 |
normalized_score = base_score / max(features['word_count'] / 100, 1)
|
| 83 |
scores[category] = normalized_score
|
| 84 |
|
|
|
|
| 94 |
'features': features
|
| 95 |
}
|
| 96 |
|
| 97 |
+
def batch_classify(self, documents: List[Dict[str, str]]) -> List[Dict[str, Any]]:
|
| 98 |
+
"""Classify multiple documents in batch"""
|
| 99 |
+
results = []
|
| 100 |
+
for doc in documents:
|
| 101 |
+
result = self.classify_document(doc['content'], doc.get('filename', ''))
|
| 102 |
+
results.append(result)
|
| 103 |
+
print(f"Batch classification completed for {len(documents)} documents")
|
| 104 |
+
return results
|
| 105 |
+
|
| 106 |
|
| 107 |
# -------------------------
|
| 108 |
+
# Streamlit App UI
|
| 109 |
# -------------------------
|
| 110 |
|
| 111 |
+
st.set_page_config(page_title="AI Document Classifier", layout="centered")
|
| 112 |
st.title("π AI-Based Document Classifier")
|
| 113 |
st.subheader("Upload government text files for automatic classification")
|
| 114 |
|
|
|
|
| 118 |
|
| 119 |
if uploaded_files:
|
| 120 |
classifier = DocumentClassifier()
|
| 121 |
+
|
| 122 |
+
# Read all files for batch processing
|
| 123 |
+
docs = []
|
| 124 |
for uploaded_file in uploaded_files:
|
| 125 |
filename = uploaded_file.name
|
| 126 |
content = uploaded_file.read().decode("utf-8")
|
| 127 |
+
docs.append({"filename": filename, "content": content})
|
| 128 |
+
|
| 129 |
+
results = classifier.batch_classify(docs)
|
| 130 |
|
| 131 |
+
# Display results per document
|
| 132 |
+
for result in results:
|
| 133 |
st.markdown("---")
|
| 134 |
st.markdown(f"π **Document:** `{result['filename']}`")
|
| 135 |
st.markdown(f"π **Predicted Category:** `{result['predicted_category']}`")
|
| 136 |
st.markdown(f"π **Confidence:** `{result['confidence']:.2f}`")
|
| 137 |
|
| 138 |
+
# Extracted metadata
|
| 139 |
extracted_date = result["features"].get("extracted_date", "N/A")
|
| 140 |
extracted_ref = result["features"].get("extracted_reference", "N/A")
|
| 141 |
|
|
|
|
| 149 |
st.json(result['features'])
|
| 150 |
|
| 151 |
with st.expander("π Full Text", expanded=False):
|
| 152 |
+
st.text(next(doc['content'] for doc in docs if doc['filename'] == result['filename']))
|