| |
| |
| |
| |
| |
|
|
| import warnings |
|
|
| warnings.filterwarnings("ignore") |
|
|
| import torch |
| from transformers import pipeline |
|
|
|
|
| class ExpenseCategorizerAI: |
| def __init__(self): |
| print( |
| "Loading AI Model (This might take a minute on the first run as it downloads the model)..." |
| ) |
|
|
| |
| if torch.cuda.is_available(): |
| device = 0 |
| print("Hardware Acceleration: Enabled (CUDA GPU)") |
| elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available(): |
| device = "mps" |
| print("Hardware Acceleration: Enabled (Apple Metal)") |
| else: |
| device = -1 |
| print("Hardware Acceleration: None (Using CPU)") |
|
|
| |
| |
| |
| self.classifier = pipeline( |
| "zero-shot-classification", |
| model="valhalla/distilbart-mnli-12-3", |
| device=device, |
| ) |
|
|
| |
| self.categories = [ |
| "Food & Drinks", |
| "Groceries", |
| "Shopping", |
| "Bills & Utilities", |
| "Entertainment", |
| "Health", |
| "Education", |
| "Subscriptions", |
| "Travel", |
| "Rent", |
| "Family & Friends", |
| "Miscellaneous", |
| "Gifts", |
| "Party", |
| "Personal Care", |
| "Home & Hygiene", |
| "Others", |
| "Recharge", |
| ] |
| print("AI Model loaded successfully!\n") |
|
|
| def predict_category(self, expense_text): |
| """ |
| Takes an expense description and returns the most likely category. |
| """ |
| |
| |
| result = self.classifier( |
| expense_text, |
| candidate_labels=self.categories, |
| hypothesis_template="This expense is for {}.", |
| ) |
|
|
| |
| |
| top_category = result["labels"][0] |
| confidence_score = result["scores"][0] |
|
|
| return top_category, confidence_score |
|
|
|
|
| |
| |
| |
| if __name__ == "__main__": |
| |
| ai_categorizer = ExpenseCategorizerAI() |
|
|
| |
| test_expenses = [ |
| "I spend 100 rupees on burger", |
| "Paid 1500 for the electricity and water", |
| "Bought paracetamol and cough syrup", |
| "Auto fare from home to the railway station", |
| "Netflix and Amazon Prime monthly deduction", |
| "Got a new shirt and jeans from the mall", |
| ] |
|
|
| print("-" * 40) |
| print("TESTING EXPENSE CATEGORIZATION") |
| print("-" * 40) |
|
|
| for text in test_expenses: |
| category, confidence = ai_categorizer.predict_category(text) |
|
|
| print(f"Expense : '{text}'") |
| print(f"AI Choice : {category}") |
| print(f"Confidence: {confidence * 100:.1f}%\n") |
|
|
| |
| print("-" * 40) |
| print("Type your own expenses below (type 'quit' to exit):") |
| while True: |
| user_input = input("Enter expense text: ") |
| if user_input.lower() in ["quit", "exit"]: |
| break |
|
|
| cat, conf = ai_categorizer.predict_category(user_input) |
| print(f"--> Categorized as: [{cat}] (Confidence: {conf * 100:.1f}%)\n") |
|
|