feat: 微信# 对话形式

This commit is contained in:
eric
2023-08-05 18:32:27 +08:00
parent 167daebd7a
commit 38032706b9

View File

@@ -182,31 +182,36 @@ async def img(
max_length=None max_length=None
top_p=None top_p=None
temperature=None temperature=None
response=None
try: try:
query = unquote(query["query"], "utf-8") query = unquote(query["query"], "utf-8")
# 判断有没有'/img'标识符 # 判断有没有'/img'标识符
if ('/img' in query): if ('/img' in query) is True:
query=query.replace('/img', '') query=query.replace('/img', '')
prompt=query prompt=query
txt2img_url = 'http://127.0.0.1:7861/sdapi/v1/txt2img' txt2img_url = 'http://127.0.0.1:7861/sdapi/v1/txt2img'
data = {'prompt': prompt} data = {'prompt': prompt}
res = submit_post(txt2img_url, data) res = submit_post(txt2img_url, data)
# 以时间戳命名 # 以时间戳命名
now = datetime.datetime.now() now =str(time.time())
save_encoded_image(res.json()['images'][0], f'img/{now}.png') save_encoded_image(res.json()['images'][0], f'img/{now}.png')
response=f'http://aiimg.yidooplanet.com/{now}.png' response=f'http://aiimg.yidooplanet.com/{now}.png'
print('response',response)
# return responseimg
else: else:
prompt = query #问题 prompt = query #问题
# 请求ai模型 # 请求ai模型
response, history = model.chat(tokenizer, response, history = model.chat(tokenizer,
prompt, prompt,
history=history, history=history,
max_length=max_length if max_length else 2048, max_length=max_length if max_length else 2048,
top_p=top_p if top_p else 0.7, top_p=top_p if top_p else 0.7,
temperature=temperature if temperature else 0.95) temperature=temperature if temperature else 0.95)
except: except(ValueError, ArithmeticError):
print('ValueError',ValueError)
print('ArithmeticError',ArithmeticError)
prompt = "" prompt = ""
@@ -228,10 +233,12 @@ async def img(
if __name__ == '__main__': if __name__ == '__main__':
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm2-6b-int4", trust_remote_code=True) tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm2-6b-int4", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm2-6b-int4", trust_remote_code=True).cuda() model = AutoModel.from_pretrained("THUDM/chatglm2-6b-int4", trust_remote_code=True).half().quantize(8).cuda()
# 多显卡支持使用下面三行代替上面两行将num_gpus改为你实际的显卡数量 # 多显卡支持使用下面三行代替上面两行将num_gpus改为你实际的显卡数量
# model_path = "THUDM/chatglm2-6b" # model_path = "THUDM/chatglm2-6b"
# tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True) # tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
# model = load_model_on_gpus(model_path, num_gpus=2) # model = load_model_on_gpus(model_path, num_gpus=2)
model.eval()
# model.eval()
uvicorn.run(app, host='0.0.0.0', port=8000, workers=1) uvicorn.run(app, host='0.0.0.0', port=8000, workers=1)