1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| import requests from flask import Flask, request from urllib.parse import quote import http.client import hashlib import urllib import random import json import time
app = Flask(__name__)
over_send_msg = [] key_msg = [] --------------------------------------------------------------
def start_piant(key_new): cookies = { }
value = (random.uniform(100236542, 875987458)) value = int(value) key = "masterpiece, best quality, " + key_new print(key, "绘图中")
headers = { 'Accept': '*/*', 'Accept-Language': 'en,zh-CN;q=0.9,zh;q=0.8', 'Authorization': 'Bearer', 'Connection': 'keep-alive', 'Origin': 'http://localhost:6969', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-origin', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36', 'sec-ch-ua': '"Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"Windows"', } json_data = { 'prompt': key, 'width': 512, 'height': 768, 'scale': 12, 'sampler': 'k_euler_ancestral', 'steps': 28, 'seed': value, 'n_samples': 1, 'ucPreset': 0, 'uc': 'lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry', }
response = requests.post('http://localhost:6969/generate-stream', cookies=cookies, headers=headers, json=json_data)
global key_qq_msg key_qq_msg = key_new + " s-" + str(value) + ".png" return key_new + " s-" + str(value) + ".png"
--------------------------------------------------------------
def tanslate(q): appid = '' secretKey = '' httpClient = None myurl = '/api/trans/vip/translate' fromLang = 'zh' toLang = 'en' salt = random.randint(32768, 65536) sign = appid + q + str(salt) + secretKey sign = hashlib.md5(sign.encode()).hexdigest() myurl = myurl + '?appid=' + appid + '&q=' + urllib.parse.quote( q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str( salt) + '&sign=' + sign
try: httpClient = http.client.HTTPConnection('api.fanyi.baidu.com') httpClient.request('GET', myurl)
response = httpClient.getresponse() result_all = response.read().decode("utf-8") result = json.loads(result_all) return result["trans_result"][0]["dst"]
except Exception as e: print(e) finally: if httpClient: httpClient.close()
--------------------------------------------------------------
def send(message, gid, uid):
推送到的地址,需要改变ip put = 'http://192.168.2.199:5700/send_group_msg?group_id={0}&message={1}&message_type=group&auto_escape=false' def send_qq_msg(path, gid): path = quote(path, 'utf-8') pic_path = f'[CQ:image,file=http://frp1.freefrp.net:13389/{path}]' data = { "message_type": "group", "group_id": gid, "message": pic_path } print(pic_path, gid) response = requests.post('http://192.168.2.199:5700/send_group_msg', json=data) if not message.find("画了个画"): key = message[5:].split(',') m = [] for ii in range(len(key)): m.append(tanslate(key[ii])) time.sleep(1) yu = ",".join(m)
if yu not in over_send_msg: message = "开始绘画\n关键词:" + message[5:] + "[" + yu + "]\n预计时间:1m..." over_send_msg.append(",".join(m)) requests.get(url=put.format(gid, message)) print("画图\n关键词:" + message[5:] + "\n==>" + yu) print(over_send_msg) paint_state = True requests.get(url=put.format(gid, "来自:%d的绘图完成\n%s" % (uid, start_piant(yu)))) send_qq = input("是否允许推送QQ图片?(y表示不推送):") if send_qq != "y": send_qq_msg(key_qq_msg, gid) else: requests.get(url=put.format(gid, "你的绘图,太刺激了吧.....不给你看"))
elif not message.find("高级画画"): yu = message[5:].replace(",", ",") if yu not in over_send_msg: message = "开始绘画\n关键词:[" + yu + "]\n预计时间:1m..." over_send_msg.append(yu) requests.get(url=put.format(gid, message)) print("画图\n关键词:\n==>" + yu) print(over_send_msg) paint_state = True requests.get(url=put.format(gid, "来自:%d的绘图完成\n%s" % (uid, start_piant(yu)))) send_qq = input("是否允许推送QQ图片?(y表示不推送):") if send_qq != "y": send_qq_msg(key_qq_msg, gid) else: requests.get(url=put.format(gid, "你的绘图,太刺激了吧.....不给你看"))
""" else: requests.get(url=put.format(gid, "喂,已经画过了喂,GPU很烫的!"))"""
@app.route('/', methods=["POST"]) def post_data(): if request.get_json().get('message_type') == 'private': uid = request.get_json().get('sender').get('user_id') message = request.get_json().get('raw_message') print(message, uid)
if request.get_json().get('message_type') == 'group': gid = request.get_json().get('group_id') uid = request.get_json().get('sender').get('user_id') message = request.get_json().get('raw_message') print(message, gid, uid) send(message, gid, uid)
return 'OK'
if __name__ == '__main__': app.run(host='192.168.2.191', port=1234)
|