Untitled

mail@pastecode.io avatarunknown
plain_text
5 days ago
4.2 kB
1
Indexable
Never
This is the code-

import json
from pandas.io.json import json_normalize

app = Flask(__name__)
app.app_context().push()

@app.route('/user_prediction', 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)
        print("Ticket Data encoded : ",ticket_data_encoded)
        
        ## error here
        # Convert the decoded string back to a dictionary
        ticket_data = ast.literal_eval(ticket_data_encoded)
        print("Ticket_data Literal Eval : ",ticket_data)
        
        ticket_data= str(ticket_data)
        print("Ticket_data : ",ticket_data)
        
        json_data = ast.literal_eval(ticket_data)
        print("Json Data : ",json_data)

        pred_data = pd.DataFrame(json_data, index=[0])
        print("Pred Data : ",pred_data)
        
        recommendations=ticket_prediction.event_prediction(pred_data)
        
        if recommendations is not None:
            print("Recommended users for the ticket are-")
            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 recommendations.to_json()
        
        else:
            return jsonify({"error":"No recommendation found"})
                    
if __name__ == "__main__":
    app.run(host='100.87.2.56', port=8895, threaded=True)

I am getting an error when I passing Http request-

Traceback (most recent call last):
  File "/Analytics/venv/CAPEANALYTICS/lib/python3.8/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "/Analytics/venv/CAPEANALYTICS/lib/python3.8/site-packages/flask/app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Analytics/venv/CAPEANALYTICS/lib/python3.8/site-packages/flask/app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Analytics/venv/CAPEANALYTICS/lib/python3.8/site-packages/flask/app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/tmp/ipykernel_51763/3451279359.py", line 31, in run_recommendation
    ticket_data = ast.literal_eval(ticket_data_encoded)
  File "/Analytics/python3/lib/python3.8/ast.py", line 99, in literal_eval
    return _convert(node_or_string)
  File "/Analytics/python3/lib/python3.8/ast.py", line 88, in _convert
    return dict(zip(map(_convert, node.keys),
  File "/Analytics/python3/lib/python3.8/ast.py", line 98, in _convert
    return _convert_signed_num(node)
  File "/Analytics/python3/lib/python3.8/ast.py", line 75, in _convert_signed_num
    return _convert_num(node)
  File "/Analytics/python3/lib/python3.8/ast.py", line 66, in _convert_num
    _raise_malformed_node(node)
  File "/Analytics/python3/lib/python3.8/ast.py", line 63, in _raise_malformed_node
    raise ValueError(f'malformed node or string: {node!r}')
ValueError: malformed node or string: <_ast.Name object at 0x7f2ec7321a00>
192.168.10.7 - - [18/Sep/2023 11:57:16] "POST /user_prediction?ticket_data=%7B%27ticket_category%27:%27Application%27,%27ticket_type%27:%27HCM%20-%20OPM%27,%27ticket_item%27:%27Talent%20Profile%27,%27ticket_summary%27:%27Application%20data%20error%27,ticket_desc:%27employee%20hired%20chroma%20recrui%27,%27ticket_severity%27:%273%20-%20Minor%27%7D HTTP/1.1" 500 -
param_vals:  {'ticket_category':'Application','ticket_type':'HCM - OPM','ticket_item':'Talent Profile','ticket_summary':'Application data error',ticket_desc:'employee hired chroma recrui','ticket_severity':'3 - Minor'}
Ticket Data encoded :  {'ticket_category':'Application','ticket_type':'HCM - OPM','ticket_item':'Talent Profile','ticket_summary':'Application data error',ticket_desc:'employee hired chroma recrui','ticket_severity':'3 - Minor'}

Somewhere near ast.literal_eval we are getting error.

Can you help fix this issue.