OHO-15(Added ML)

This commit is contained in:
lokeshramchand-ctrl 2025-08-22 09:25:35 +05:30
parent 2e7a8ecfc5
commit fd80a9be70
3 changed files with 231 additions and 4 deletions

View file

@ -0,0 +1,227 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"df = pd.read_csv(\"transcations.csv\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a074a45e",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"df = df[['Description', 'Category']].head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fe26b20f",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"df[['Description', 'Category']].head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "44c3995d",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"df['Description'] = df['Description'].str.lower().str.replace('[^a-z\\s]', '', regex=True)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "82245456",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"df['Category'] = df['Category'].str.lower().str.replace('[^a-z\\s]', '', regex=True)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "620ef271",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split\n",
"\n",
"X = df['Description']\n",
"y = df['Category']\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3eebf352",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
"\n",
"vectorizer = TfidfVectorizer()\n",
"X_train_vec = vectorizer.fit_transform(X_train)\n",
"X_test_vec = vectorizer.transform(X_test)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "75a141a8",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"from sklearn.naive_bayes import MultinomialNB\n",
"\n",
"model = MultinomialNB()\n",
"model.fit(X_train_vec, y_train)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "184354db",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"def predict_category(text):\n",
" text = [text.lower()]\n",
" text_vec = vectorizer.transform(text)\n",
" return model.predict(text_vec)[0]\n",
"\n",
"# Example\n",
"print(predict_category(\"Netflix\"))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c30b9f27",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"from sklearn.metrics import classification_report\n",
"y_pred = model.predict(X_test_vec)\n",
"print(classification_report(y_test, y_pred))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "109a12d5",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"import pickle\n",
"\n",
"# Save model\n",
"with open('model.pkl', 'wb') as f:\n",
" pickle.dump(model, f)\n",
"\n",
"# Save vectorizer\n",
"with open('vectorizer.pkl', 'wb') as f:\n",
" pickle.dump(vectorizer, f)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bcb76b6a",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"import joblib\n",
"\n",
"joblib.dump(model, 'model.pkl')\n",
"joblib.dump(vectorizer, 'vectorizer.pkl')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f16d6da5",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"from google.colab import files\n",
"files.download('model.pkl')\n",
"files.download('vectorizer.pkl')\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View file

@ -160,7 +160,7 @@ app.post('/api/transaction/add', async (req, res) => {
const { description, amount, userId } = req.body;
if (!userId) return res.status(400).json({ error: 'Missing userId' });
const predictRes = await axios.post('http://192.168.1.101:5000/api/predict', {
const predictRes = await axios.post('http://10.231.41.181:5000/api/predict', {
description,
});
const category = predictRes.data.category || 'Other';
@ -227,7 +227,7 @@ app.post('/api/transactions/voice', async (req, res) => {
.trim();
const description = cleaned || 'misc';
// Step 3: Predict category using your Flask API
const predictRes = await axios.post('http://192.168.1.101:5000/api/predict', {
const predictRes = await axios.post('http://10.231.41.181:5000/api/predict', {
description,
});
const category = predictRes.data?.category || 'Other';
@ -394,7 +394,7 @@ app.post('/api/sync-gmail', async (req, res) => {
let category = 'Other';
try {
const predictRes = await axios.post('http://192.168.1.101:5000/api/predict', {
const predictRes = await axios.post('http://10.231.41.181:5000/api/predict', {
description: parsed.vendor || "Unknown"
}, { timeout: 5000 }); // Optional timeout

View file

@ -1,5 +1,5 @@
class Environment {
static const String baseUrl = 'http://192.168.1.101:3000';
static const String baseUrl = 'http://10.231.41.181:3000';
static const String serverClientId =
"278433619849-cg6f7gk5cc45rgu1ojrlkf794lm4udgn.apps.googleusercontent.com";
}