Spaces:
Sleeping
Sleeping
Create app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273import streamlit as stimport refrom datetime import datetimefrom typing import Dict, Any, List
# -------------------------# Document Classifier Class# -------------------------
class DocumentClassifier: def __init__(self): self.categories = { 'ADMINISTRATIVE': ['memo', 'circular', 'notification', 'order', 'policy'], 'FINANCIAL': ['budget', 'expenditure', 'audit', 'payment', 'revenue'], 'LEGAL': ['contract', 'agreement', 'legal', 'court', 'judgment'], 'HR': ['recruitment', 'promotion', 'transfer', 'leave', 'employee'], 'TECHNICAL': ['specification', 'tender', 'project', 'development', 'infrastructure'] } self.confidence_threshold = 0.7
def extract_features(self, document_text: str) -> Dict[str, int]: text_lower = document_text.lower() features = {} for category, keywords in self.categories.items(): feature_count = sum(text_lower.count(keyword) for keyword in keywords) features[f'{category}_score'] = feature_count features['word_count'] = len(text_lower.split()) features['has_date'] = 1 if re.search(r'\d{1,2}[-/]\d{1,2}[-/]\d{4}', document_text) else 0 features['has_reference'] = 1 if re.search(r'ref[:\s]*\w+', text_lower) else 0 return features
def classify_document(self, document_text: str, filename: str = "") -> Dict[str, Any]: features = self.extract_features(document_text) scores = {} for category in self.categories.keys(): base_score = features.get(f'{category}_score', 0) normalized_score = base_score / max(features['word_count'] / 100, 1) scores[category] = normalized_score best_category = max(scores, key=scores.get) if scores else 'UNCLASSIFIED' confidence = min(scores[best_category] / 5.0, 1.0) return { 'filename': filename, 'predicted_category': best_category, 'confidence': confidence, 'all_scores': scores, 'classification_date': datetime.now().isoformat(), 'features': features }
# -------------------------# Streamlit UI# -------------------------
st.title("๐ AI-Based Document Classifier")st.subheader("Upload government text files for automatic classification")
uploaded_files = st.file_uploader( "Choose .txt files", type=["txt"], accept_multiple_files=True)
if uploaded_files: classifier = DocumentClassifier() for uploaded_file in uploaded_files: filename = uploaded_file.name content = uploaded_file.read().decode("utf-8") result = classifier.classify_document(content, filename)
st.markdown("---") st.write(f"**Document:** {result['filename']}") st.write(f"**Predicted Category:** `{result['predicted_category']}`") st.write(f"**Confidence:** `{result['confidence']:.2f}`") with st.expander("๐ Feature Details"): st.json(result['features'])app.pyAdded+73