Untitled
unknown
plain_text
a year ago
5.1 kB
2
Indexable
Never
Code- from flask import jsonify, request, Flask, render_template, json from feature_engineering import feature_engineering from model_training import get_top_persons_who_resolved from sklearn.preprocessing import LabelEncoder import pandas as pd import pdb from urllib.parse import quote, unquote import ast import json from pandas.io.json import json_normalize app = Flask(__name__) app.app_context().push() @app.route('/run_recommendation', methods=['POST']) def run_recommendation(): #pdb.set_trace() if request.method =='POST': param_vals = request.args.get('ticket_data') # Retrieve the JSON data from the request body #print("param_vals: ", param_vals) # URL-decode the ticket_data ticket_data_encoded = unquote(param_vals) # Convert the decoded string back to a dictionary ticket_data = ast.literal_eval(ticket_data_encoded) ticket_data= str(ticket_data) json_data = ast.literal_eval(ticket_data) recommendations=get_top_persons_who_resolved(pred_data) for i, rec in enumerate(recommendations.iterrows()): index, row = rec print(f"Recommendation {i+1}: User {row['person_who_resolved']}, Owner User ID {row['owner_user_id']}, Role Name {row['role_name']}") return jsonify(recommendations) if __name__ == "__main__": app.run(host='100.187.21.56', port=8895, threaded=True) Error on sending request- File "/tmp/ipykernel_25888/1424686922.py", line 59, in run_recommendation recommendations=get_top_persons_who_resolved(pred_data) File "/Analytics/venv/Jup/CAPE_ServicePlus_UC/model_training.py", line 57, in get_top_persons_who_resolved predicted_output_data=output_df.iloc[similar_indices] File "/Analytics/venv/CAPEANALYTICS/lib/python3.8/site-packages/pandas/core/indexing.py", line 925, in __getitem__ return self._getitem_tuple(key) File "/Analytics/venv/CAPEANALYTICS/lib/python3.8/site-packages/pandas/core/indexing.py", line 1506, in _getitem_tuple self._has_valid_tuple(tup) File "/Analytics/venv/CAPEANALYTICS/lib/python3.8/site-packages/pandas/core/indexing.py", line 754, in _has_valid_tuple self._validate_key(k, i) File "/Analytics/venv/CAPEANALYTICS/lib/python3.8/site-packages/pandas/core/indexing.py", line 1424, in _validate_key raise IndexError("positional indexers are out-of-bounds") IndexError: positional indexers are out-of-bounds SO it goes into get_top_persons_who_resolved function and faces this issue- COde for get_top_persons_who_resolved is - def get_top_persons_who_resolved(pred_data): distance_metric='cosine' print("pred_data: ", pred_data) row=feature_engineering(pred_data) print("row: ", row) label_enc = LabelEncoder() #row['role_name_encoded'] = label_enc.fit_transform(row['role_name']) #row['role_name_decoded'] = label_enc.inverse_transform(row['role_name_encoded']) # Link the X vector with index #index = row.index.values ##Fetching the ticket data details from API #pdb.set_trace() ticket_data= ticket_data = pd.concat(map(pd.read_csv, ['/Analytics/venv/Jup/CAPE_ServicePlus_UC/ServicePlusIncidentData_Post_01-01-2019_Till_07-07-2019.csv', '/Analytics/venv/Jup/CAPE_ServicePlus_UC/ServicePlusTicket_Data_Till-2019-01-01.csv']), ignore_index=True) df=feature_engineering(ticket_data) #print("Dataframe",df) # Sample training data with text features train_data = df.drop(columns=['person_who_resolved','owner_user_id','role_name']) #print("Train data",train_data) output_df =df[['person_who_resolved','owner_user_id','role_name']] print("Output Columns",output_df) # New data for similarity calculation new_data = row print("New Data",new_data) # Create TF-IDF vectorizer and fit on training data #vectorizer = TfidfVectorizer() X = vectorizer.fit_transform(train_data) # Transform new data using the same vectorizer X_new = vectorizer.transform(new_data) # Calculate cosine similarity between new data and training data similarity_matrix = cosine_similarity(X_new, X) print("Similarity Matrix:",similarity_matrix) # Find the most similar training data indices for each new data point similar_indices = np.where(similarity_matrix > 0.5) print('Similar Indices',similar_indices) # Get the corresponding output TF-IDF vectors for new data predicted_output_data=output_df.iloc[similar_indices] print("Predicted Output",predicted_output_data) print("Similarity Matrix:") print(similarity_matrix) print("\nSimilar Data Indices with Cosine Similarity > 0.5:", similar_indices) print("\nPredicted Output Data:") print(predicted_output_data) so similar indices we are getting output- Similar Indices (array([0, 1, 2, 3, 4, 5]), array([ 3, 4, 5, 6, 7, 16])) but later on its not getting predicted_output_data. If you want I can share the code for feature_engineering() function as well.