Put the py file under filter_plugins

and call the function using below ansible code

- name: calling custom function
  set_fact: my_dict=""
  run_once: true
  delegate_to: ""