Commit c7733665 authored by Nirmal M.D.S's avatar Nirmal M.D.S

final models

parent 1df50156
from flask import Flask, jsonify, request
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
import pickle
app = Flask(__name__)
def save_pickle(obj, filename):
with open(filename, 'wb') as file:
pickle.dump(obj, file)
def train_model(data_path):
# Load and preprocess the data
df = pd.read_csv(data_path)
X_train, _, y_train, _ = train_test_split(df['TaskDescription'], df['Level'], test_size=0.2, random_state=42)
vectorizer = TfidfVectorizer()
X_train_vec = vectorizer.fit_transform(X_train)
# Initialize GridSearchCV
param_grid = {'C': [0.1, 1, 10, 100, 1000],
'gamma': [1, 0.1, 0.01, 0.001, 0.0001],
'kernel': ['linear', 'rbf', 'sigmoid']}
grid = GridSearchCV(SVC(), param_grid, refit=True, verbose=3)
grid.fit(X_train_vec, y_train)
# Save the trained classifier and vectorizer as .pickle files
save_pickle(grid, 'classifier.pickle')
save_pickle(vectorizer, 'vectorizer.pickle')
return 'Model trained and saved successfully'
@app.route('/train', methods=['GET'])
def trigger_training():
try:
result = train_model("TaskDescCopy3.csv")
return jsonify({'message': result})
except Exception as e:
return jsonify({'error': str(e)})
if __name__ == '__main__':
app.run(host='127.0.0.1', port=5001, debug=False)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment