GPTrequest.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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,model="cogview-3"):
  36. uid = str(uuid.uuid1().hex)
  37. param = {
  38. "size": "1024*1024" if model.find("cogview-3")>-1 else "1024x1024",
  39. "quality": "standard",
  40. "n": 1,
  41. "prompt": text,
  42. "style":"natural",
  43. "uid": uid,
  44. "model":model
  45. }
  46. headers={
  47. "Content-Type":"application/json",
  48. "hwMac":self.hwMac
  49. }
  50. imgUrl = ""
  51. res = requests.post("https://gpt4.cocorobo.cn/getImageV831", headers=headers, json=param)
  52. try:
  53. url = (json.loads(res.text))["FunctionResponse"]["image_url_list"][0]
  54. r = requests.get("https://gpt4.cocorobo.cn"+url)
  55. with open("/root/user/img/" + uid + ".png","wb") as f:
  56. f.write(r.content)
  57. imgUrl = "/root/user/img/" + uid + ".png"
  58. except:
  59. imgUrl = ""
  60. return imgUrl
  61. # 图片转base64
  62. def encode_image(self,image_path):
  63. img_data = ""
  64. try:
  65. with open(image_path, "rb") as f:
  66. img_data = f.read()
  67. except:
  68. pass
  69. return str(base64.b64encode(img_data), "utf-8")
  70. # 通过图片获取文字
  71. def get_image_to_text(self,imgUrl,textDesc,model="gpt-4o"):
  72. uid = str(uuid.uuid1().hex)
  73. strBase = self.encode_image(imgUrl)
  74. params = {
  75. "max_tokens": 4096,
  76. "messages":[
  77. {
  78. "role": "user",
  79. "content":[
  80. {"text": textDesc,"type": "text"},
  81. {"image_url": {"url": f"data:image/jpeg;base64,{strBase}"},"type": "image_url"}
  82. ]
  83. }
  84. ],
  85. "uid": uid,
  86. "stream": False,
  87. "model":model
  88. }
  89. headers={
  90. "Content-Type":"application/json",
  91. "hwMac":self.hwMac
  92. }
  93. textContent = ""
  94. res = requests.post("https://gpt4.cocorobo.cn/imageAnalyse", headers=headers, data=json.dumps(params))
  95. try:
  96. if res.status_code == 200:
  97. textContent = json.loads(res.text)["FunctionResponse"]["choices"][0]["message"]["content"]
  98. except:
  99. textContent = ""
  100. return textContent
  101. # 文字生成音頻文件
  102. def get_text_to_voice(self,text,model="tts-1"):
  103. uid = str(uuid.uuid1().hex)
  104. url = "https://gpt4.cocorobo.cn/getV831Audio"
  105. payload = json.dumps({"input": text,"response_format":"mp3","voice": "shimmer","uid":uid,"model":model})
  106. headers={
  107. "Content-Type":"application/json",
  108. "hwMac":self.hwMac
  109. }
  110. response=requests.request("POST", url, headers=headers, data=payload)
  111. voiceFilePath = ""
  112. try:
  113. if response.status_code == 200:
  114. # print(json.loads(response.text)["FunctionResponse"]["url"])
  115. a = "https://gpt4.cocorobo.cn" + json.loads(response.text)["FunctionResponse"]["url"]
  116. r = requests.get(a)
  117. with open("/root/preset/audio/" + uid+".wav","wb") as f:
  118. f.write(r.content)
  119. voiceFilePath = "/root/preset/audio/" + uid+".wav"
  120. except:
  121. voiceFilePath = ""
  122. return voiceFilePath
  123. # 音频转文字
  124. def get_voice_to_text(self,voicePath,model=""):
  125. voiceData = open(voicePath,'rb')
  126. data = {"file": voiceData,"model":model}
  127. param = {}
  128. headers={
  129. "hwMac":self.hwMac
  130. }
  131. res = requests.post("https://gpt4.cocorobo.cn/transcribe_file_stream",headers=headers, files=data, data=param)
  132. try:
  133. voiceResult = (json.loads(res.text))["FunctionResponse"]
  134. except:
  135. voiceResult = ""
  136. return voiceResult
  137. # gpt 對話,並生成音頻文件
  138. def get_post_chatgpt(self,datas,model="deepseek-chat"):
  139. url = "https://gpt4.cocorobo.cn/v831AskForTopicNew"
  140. payload = json.dumps({"topic": datas,"model":model})
  141. headers={
  142. "Content-Type":"application/json",
  143. "hwMac":self.hwMac
  144. }
  145. response=requests.request("POST", url, headers=headers, data=payload)
  146. result = ""
  147. try:
  148. result = json.loads(response.text)["FunctionResponse"]["result"]
  149. except:
  150. result = ""
  151. # print("resule:"+str(result))
  152. return result
  153. def get_post_AI_Intelligence(self,id,text,model="deepseek-chat"):
  154. url = "https://gpt4.cocorobo.cn/ai_agent_chat"
  155. params = {
  156. "id":id,
  157. "userId":self.hwMac,
  158. "message":text,
  159. "file_ids": [],
  160. "model":model
  161. }
  162. headers={
  163. "Content-Type":"application/json",
  164. "hwMac":self.hwMac
  165. }
  166. textContent = ""
  167. res = requests.post(url, headers=headers, data=json.dumps(params))
  168. try:
  169. textContent = json.loads(res.text)["FunctionResponse"]["message"]
  170. except:
  171. textContent = ""
  172. return textContent