Sumayyea commited on
Commit
00b8a84
Β·
verified Β·
1 Parent(s): e9d51a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -18
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, int]:
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 (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
 
@@ -74,7 +87,7 @@ class DocumentClassifier:
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,9 +103,19 @@ if uploaded_files:
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'])
 
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'])