GPTrequest.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. ##################################
  2. # GPTrequest #
  3. ##################################
  4. #Version date : 2024-07-08
  5. #region : CH
  6. import uuid
  7. import requests
  8. import json
  9. import base64
  10. import os
  11. try:
  12. class LazyImport:
  13. def __init__(self, module_name):
  14. self.module_name = module_name
  15. self.module = None
  16. def __getattr__(self, name):
  17. if self.module is None:
  18. self.module = __import__(self.module_name)
  19. return getattr(self.module, name)
  20. uuid = LazyImport("uuid")
  21. requests = LazyImport("requests")
  22. json = LazyImport("json")
  23. base64 = LazyImport("base64")
  24. except ModuleNotFoundError as e:
  25. pass
  26. class GPTrequest():
  27. def __init__(self):
  28. self.uid = str(uuid.uuid1().hex)
  29. #获取hardware mac地址
  30. cmd="cat /sys/class/sunxi_info/sys_info"
  31. rec=os.popen(cmd).read()
  32. self.hwMac=rec[rec.find("sunxi_serial : ")+20:rec.find("sunxi_serial : ")+52]
  33. print(self.hwMac)
  34. # 文字生成图片
  35. def get_text_to_img(self,text):
  36. uid = str(uuid.uuid1().hex)
  37. param = {
  38. "size": "1024x1024",
  39. "quality": "standard",
  40. "n": 1,
  41. "prompt": text,
  42. "style":"natural",
  43. "uid": uid
  44. }
  45. headers={
  46. "Content-Type":"application/json",
  47. "hwMac":self.hwMac
  48. }
  49. imgUrl = ""
  50. res = requests.post("https://gpt4.cocorobo.cn/getImageV831", headers=headers, json=param)
  51. try:
  52. url = (json.loads(res.text))["FunctionResponse"]["image_url_list"][0]
  53. r = requests.get("https://gpt4.cocorobo.cn"+url)
  54. with open("/root/user/img/" + uid + ".png","wb") as f:
  55. f.write(r.content)
  56. imgUrl = "/root/user/img/" + uid + ".png"
  57. except:
  58. imgUrl = ""
  59. return imgUrl
  60. # 图片转base64
  61. def encode_image(self,image_path):
  62. img_data = ""
  63. try:
  64. with open(image_path, "rb") as f:
  65. img_data = f.read()
  66. except:
  67. pass
  68. return str(base64.b64encode(img_data), "utf-8")
  69. # 通过图片获取文字
  70. def get_image_to_text(self,imgUrl,textDesc):
  71. uid = str(uuid.uuid1().hex)
  72. strBase = self.encode_image(imgUrl)
  73. params = {
  74. "max_tokens": 4096,
  75. "messages":[
  76. {
  77. "role": "user",
  78. "content":[
  79. {"text": textDesc,"type": "text"},
  80. {"image_url": {"url": f"data:image/jpeg;base64,{strBase}"},"type": "image_url"}
  81. ]
  82. }
  83. ],
  84. "uid": uid,
  85. "stream": False
  86. }
  87. headers={
  88. "Content-Type":"application/json",
  89. "hwMac":self.hwMac
  90. }
  91. textContent = ""
  92. res = requests.post("https://gpt4.cocorobo.cn/imageAnalyse", headers=headers, data=json.dumps(params))
  93. try:
  94. if res.status_code == 200:
  95. textContent = json.loads(res.text)["FunctionResponse"]["choices"][0]["message"]["content"]
  96. except:
  97. textContent = ""
  98. return textContent
  99. # 文字生成音頻文件
  100. def get_text_to_voice(self,text):
  101. uid = str(uuid.uuid1().hex)
  102. url = "https://gpt4.cocorobo.cn/getV831Audio"
  103. payload = json.dumps({"input": text,"response_format":"mp3","voice": "shimmer","uid":uid})
  104. headers={
  105. "Content-Type":"application/json",
  106. "hwMac":self.hwMac
  107. }
  108. response=requests.request("POST", url, headers=headers, data=payload)
  109. voiceFilePath = ""
  110. try:
  111. if response.status_code == 200:
  112. # print(json.loads(response.text)["FunctionResponse"]["url"])
  113. a = "https://gpt4.cocorobo.cn" + json.loads(response.text)["FunctionResponse"]["url"]
  114. r = requests.get(a)
  115. with open("/root/preset/audio/" + uid+".wav","wb") as f:
  116. f.write(r.content)
  117. voiceFilePath = "/root/preset/audio/" + uid+".wav"
  118. except:
  119. voiceFilePath = ""
  120. return voiceFilePath
  121. # 音频转文字
  122. def get_voice_to_text(self,voicePath):
  123. voiceData = open(voicePath,'rb')
  124. data = {"file": voiceData}
  125. param = {}
  126. headers={
  127. "hwMac":self.hwMac
  128. }
  129. res = requests.post("https://gpt4.cocorobo.cn/transcribe_file_stream",headers=headers, files=data, data=param)
  130. try:
  131. voiceResult = (json.loads(res.text))["FunctionResponse"]
  132. except:
  133. voiceResult = ""
  134. return voiceResult
  135. # gpt 對話,並生成音頻文件
  136. def get_post_chatgpt(self,datas):
  137. url = "https://gpt4.cocorobo.cn/v831AskForTopicNew"
  138. payload = json.dumps({"topic": datas})
  139. headers={
  140. "Content-Type":"application/json",
  141. "hwMac":self.hwMac
  142. }
  143. response=requests.request("POST", url, headers=headers, data=payload)
  144. result = ""
  145. try:
  146. result = json.loads(response.text)["FunctionResponse"]["result"]
  147. except:
  148. result = ""
  149. # print("resule:"+str(result))
  150. return result
  151. def get_post_AI_Intelligence(self,id,text):
  152. url = "https://gpt4.cocorobo.cn/ai_agent_chat"
  153. params = {
  154. "id":id,
  155. "userId":self.hwMac,
  156. "message":text,
  157. "file_ids": []
  158. }
  159. headers={
  160. "Content-Type":"application/json",
  161. "hwMac":self.hwMac
  162. }
  163. textContent = ""
  164. res = requests.post(url, headers=headers, data=json.dumps(params))
  165. try:
  166. textContent = json.loads(res.text)["FunctionResponse"]["message"]
  167. except:
  168. textContent = ""
  169. return textContent