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 |
# -------------------------
|
|
@@ -17,9 +17,8 @@ class DocumentClassifier:
|
|
| 17 |
'HR': ['recruitment', 'promotion', 'transfer', 'leave', 'employee'],
|
| 18 |
'TECHNICAL': ['specification', 'tender', 'project', 'development', 'infrastructure']
|
| 19 |
}
|
| 20 |
-
self.confidence_threshold = 0.7
|
| 21 |
|
| 22 |
-
def extract_features(self, document_text: str) -> Dict[str,
|
| 23 |
text_lower = document_text.lower()
|
| 24 |
features = {}
|
| 25 |
|
|
@@ -30,21 +29,35 @@ class DocumentClassifier:
|
|
| 30 |
|
| 31 |
features['word_count'] = len(text_lower.split())
|
| 32 |
|
| 33 |
-
# Date patterns (
|
| 34 |
date_patterns = [
|
| 35 |
-
r'\b\d{1,2}[-/]\d{1,2}[-/]\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\d{1,2}(?:st|nd|rd|th)?\s+[A-Z][a-z]+\s+\d{4}\b',
|
| 38 |
-
r'\b[A-Z][a-z]+\s+\d{1,2},\s+\d{4}\b'
|
| 39 |
]
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
ref_patterns = [
|
| 44 |
-
r'\bref
|
| 45 |
-
r'\bno
|
| 46 |
]
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
return features
|
| 50 |
|
|
@@ -74,7 +87,7 @@ class DocumentClassifier:
|
|
| 74 |
# Streamlit UI
|
| 75 |
# -------------------------
|
| 76 |
|
| 77 |
-
st.set_page_config(page_title="Document Classifier", layout="
|
| 78 |
st.title("π AI-Based Document Classifier")
|
| 79 |
st.subheader("Upload government text files for automatic classification")
|
| 80 |
|
|
@@ -90,9 +103,19 @@ if uploaded_files:
|
|
| 90 |
result = classifier.classify_document(content, filename)
|
| 91 |
|
| 92 |
st.markdown("---")
|
| 93 |
-
st.
|
| 94 |
-
st.
|
| 95 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
with st.expander("π Feature Details", expanded=False):
|
| 98 |
st.json(result['features'])
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import re
|
| 3 |
from datetime import datetime
|
| 4 |
+
from typing import Dict, Any, Optional
|
| 5 |
|
| 6 |
|
| 7 |
# -------------------------
|
|
|
|
| 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 |
|
|
|
|
| 29 |
|
| 30 |
features['word_count'] = len(text_lower.split())
|
| 31 |
|
| 32 |
+
# Date patterns (extract actual date if found)
|
| 33 |
date_patterns = [
|
| 34 |
+
r'\b\d{1,2}[-/]\d{1,2}[-/]\d{4}\b', # 12/02/2023
|
| 35 |
+
r'\b\d{1,2}(?:st|nd|rd|th)?\s+[A-Z][a-z]+,\s+\d{4}\b', # 7th January, 2025
|
| 36 |
+
r'\b\d{1,2}(?:st|nd|rd|th)?\s+[A-Z][a-z]+\s+\d{4}\b', # 29 January 2025
|
| 37 |
+
r'\b[A-Z][a-z]+\s+\d{1,2},\s+\d{4}\b' # January 29, 2025
|
| 38 |
]
|
| 39 |
+
found_date = None
|
| 40 |
+
for pattern in date_patterns:
|
| 41 |
+
match = re.search(pattern, document_text)
|
| 42 |
+
if match:
|
| 43 |
+
found_date = match.group()
|
| 44 |
+
break
|
| 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 patterns (extract actual reference if found)
|
| 49 |
ref_patterns = [
|
| 50 |
+
r'\bref(?:\.|\s|:)*[A-Za-z0-9/\-()]+', # Ref No: XYZ-123
|
| 51 |
+
r'\bno(?:\.|\s|:)*[A-Za-z0-9/\-()]+', # No. DD/XYZ-123
|
| 52 |
]
|
| 53 |
+
found_ref = None
|
| 54 |
+
for pattern in ref_patterns:
|
| 55 |
+
match = re.search(pattern, document_text, flags=re.IGNORECASE)
|
| 56 |
+
if match:
|
| 57 |
+
found_ref = match.group().strip()
|
| 58 |
+
break
|
| 59 |
+
features['has_reference'] = 1 if found_ref else 0
|
| 60 |
+
features['extracted_reference'] = found_ref if found_ref else "N/A"
|
| 61 |
|
| 62 |
return features
|
| 63 |
|
|
|
|
| 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 |
|
|
|
|
| 103 |
result = classifier.classify_document(content, filename)
|
| 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 |
+
# Show extracted date and reference if found
|
| 111 |
+
extracted_date = result["features"].get("extracted_date", "N/A")
|
| 112 |
+
extracted_ref = result["features"].get("extracted_reference", "N/A")
|
| 113 |
+
|
| 114 |
+
col1, col2 = st.columns(2)
|
| 115 |
+
with col1:
|
| 116 |
+
st.markdown(f"ποΈ **Extracted Date:** `{extracted_date}`")
|
| 117 |
+
with col2:
|
| 118 |
+
st.markdown(f"π **Reference Number:** `{extracted_ref}`")
|
| 119 |
|
| 120 |
with st.expander("π Feature Details", expanded=False):
|
| 121 |
st.json(result['features'])
|