Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import re
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
from typing import Dict, Any, List
|
| 5 |
+
|
| 6 |
+
# -------------------------
|
| 7 |
+
# Document Classifier Class
|
| 8 |
+
# -------------------------
|
| 9 |
+
|
| 10 |
+
class DocumentClassifier:
|
| 11 |
+
def __init__(self):
|
| 12 |
+
self.categories = {
|
| 13 |
+
'ADMINISTRATIVE': ['memo', 'circular', 'notification', 'order', 'policy'],
|
| 14 |
+
'FINANCIAL': ['budget', 'expenditure', 'audit', 'payment', 'revenue'],
|
| 15 |
+
'LEGAL': ['contract', 'agreement', 'legal', 'court', 'judgment'],
|
| 16 |
+
'HR': ['recruitment', 'promotion', 'transfer', 'leave', 'employee'],
|
| 17 |
+
'TECHNICAL': ['specification', 'tender', 'project', 'development', 'infrastructure']
|
| 18 |
+
}
|
| 19 |
+
self.confidence_threshold = 0.7
|
| 20 |
+
|
| 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 |
+
features['has_date'] = 1 if re.search(r'\d{1,2}[-/]\d{1,2}[-/]\d{4}', document_text) else 0
|
| 29 |
+
features['has_reference'] = 1 if re.search(r'ref[:\s]*\w+', text_lower) else 0
|
| 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 |
+
for category in self.categories.keys():
|
| 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 |
+
best_category = max(scores, key=scores.get) if scores else 'UNCLASSIFIED'
|
| 40 |
+
confidence = min(scores[best_category] / 5.0, 1.0)
|
| 41 |
+
return {
|
| 42 |
+
'filename': filename,
|
| 43 |
+
'predicted_category': best_category,
|
| 44 |
+
'confidence': confidence,
|
| 45 |
+
'all_scores': scores,
|
| 46 |
+
'classification_date': datetime.now().isoformat(),
|
| 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 |
+
|
| 57 |
+
uploaded_files = st.file_uploader(
|
| 58 |
+
"Choose .txt files", type=["txt"], accept_multiple_files=True
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
if uploaded_files:
|
| 62 |
+
classifier = DocumentClassifier()
|
| 63 |
+
for uploaded_file in uploaded_files:
|
| 64 |
+
filename = uploaded_file.name
|
| 65 |
+
content = uploaded_file.read().decode("utf-8")
|
| 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 |
+
with st.expander("🔍 Feature Details"):
|
| 73 |
+
st.json(result['features'])
|