Advertisement
IssyPutchy

Crude Bard API script for Termux and Tasker

Jun 14th, 2023
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. #!/data/data/com.termux/files/usr/bin/python3
  2.  
  3. from bardapi import Bard
  4. import sys
  5. import json
  6.  
  7. sys.stdout.reconfigure(encoding='utf-8')
  8. input_text = sys.argv[1]
  9.  
  10. token = '__Secure-1PSID'
  11. bard = Bard(token=token, timeout=30)
  12. res = bard.get_answer(input_text)
  13.  
  14.  
  15. content = res['content']
  16. choices = res['choices']
  17. query = res['textQuery']
  18. links = res['links']
  19. imgset = res['images']
  20. conv_id = res['conversation_id']
  21. resp_id = res['response_id']
  22. facts = res['factualityQueries']
  23.  
  24. images = list(imgset)
  25.  
  26. output = {
  27.   "content": (content),
  28.   "choices": (choices),
  29.   "conv_id": (conv_id),
  30.   "resp_id": (resp_id),
  31.   "links": (links),
  32.   "images": (images),
  33.   "facts": (facts),
  34.   "enquiry": (query)
  35. }
  36.  
  37. bardReply = json.dumps(output)
  38.  
  39. with open("/data/data/com.termux/files/home/bard/output.txt", "w") as f:
  40.   print("Links: ", (links), file=f)
  41.   print(("Images: ", images), file=f)
  42.  
  43. bpath = "/sdcard/Tasker Logs/Bard/json/"
  44. apath = "/sdcard/Tasker\ Logs/Bard/json/"
  45.  
  46. with open(f'{bpath}{conv_id}.json', "w", encoding='utf-8') as jf:
  47.   print((bardReply), file=jf)
  48.  
  49. #sys.stdout.write(conv_id)
  50. print(f'{apath}{conv_id}.json')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement