diff --git a/AI/WxMini/Milvus/Config/__pycache__/MulvusConfig.cpython-310.pyc b/AI/WxMini/Milvus/Config/__pycache__/MulvusConfig.cpython-310.pyc index c6b52f75..0eb85b14 100644 Binary files a/AI/WxMini/Milvus/Config/__pycache__/MulvusConfig.cpython-310.pyc and b/AI/WxMini/Milvus/Config/__pycache__/MulvusConfig.cpython-310.pyc differ diff --git a/AI/WxMini/Start.py b/AI/WxMini/Start.py index eb51c254..58f9165f 100644 --- a/AI/WxMini/Start.py +++ b/AI/WxMini/Start.py @@ -48,7 +48,7 @@ async def lifespan(app: FastAPI): # 会话结束后,调用检查方法,判断是不是有需要介入的问题出现 async def on_session_end(session_id): # 获取聊天记录 - result = await get_chat_log_by_session(app.state.mysql_pool, session_id, page=1, page_size=100) + result = await get_chat_log_by_session(app.state.mysql_pool, session_id, page=1, page_size=1) # 拼接历史聊天记录 history = "" @@ -79,6 +79,8 @@ async def on_session_end(session_id): # 异步执行 update_risk asyncio.create_task(update_risk(app.state.mysql_pool, session_id, analysis_result)) logger.info(f"已异步更新 session_id={session_id} 的风险状态。") + else: + logger.info(f"AI大模型没有发现任何心理健康问题,用户会话 {session_id} 没有风险。") # 初始化 FastAPI 应用 diff --git a/AI/WxMini/Test/T11_TuYa.py b/AI/WxMini/Test/T11_TuYa.py new file mode 100644 index 00000000..70fd9cb3 --- /dev/null +++ b/AI/WxMini/Test/T11_TuYa.py @@ -0,0 +1,63 @@ +from http import HTTPStatus +from urllib.parse import urlparse, unquote +from pathlib import PurePosixPath +from dashscope import ImageSynthesis +from WxMini.Milvus.Config.MulvusConfig import * + +def generate_image(prompt, sketch_image_url, model="wanx-sketch-to-image-lite", task="image2image", n=1, size='768*768', style=''): + """ + 根据提示词和草图生成图片 + :param prompt: 提示词 + :param sketch_image_url: 草图图片 URL + :param model: 使用的模型,默认为 "wanx-sketch-to-image-lite" + :param task: 任务类型,默认为 "image2image" + :param n: 生成图片的数量,默认为 1 + :param size: 图片尺寸,默认为 '768*768' + :param style: 图片风格,默认为 '' + :return: (success, message) success 为 True 表示成功,False 表示失败;message 为提示信息 + """ + try: + print('----同步调用,请稍等----') + rsp = ImageSynthesis.call(api_key=MODEL_API_KEY, + model=model, + prompt=prompt, + n=n, + style=style, + size=size, + sketch_image_url=sketch_image_url, + task=task) + print('response: %s' % rsp) + + if rsp.status_code == HTTPStatus.OK: + if rsp.output.task_status == "SUCCEEDED": + # 在当前目录下保存图片 + for result in rsp.output.results: + file_name = PurePosixPath(unquote(urlparse(result.url).path)).parts[-1] + #with open('./%s' % file_name, 'wb+') as f: + # f.write(requests.get(result.url).content) + #print(f"图片已生成,文件名: {file_name}") + return True, "图片生成成功" + else: + # 生成失败,返回错误信息 + error_message = f"生成失败,错误码: {rsp.output.code}, 错误信息: {rsp.output.message}" + print(error_message) + return False, error_message + else: + # 调用失败,返回错误信息 + error_message = f"同步调用失败,状态码: {rsp.status_code}, 错误码: {rsp.code}, 错误信息: {rsp.message}" + print(error_message) + return False, error_message + except Exception as e: + # 捕获异常,返回错误信息 + error_message = f"生成图片时发生异常: {str(e)}" + print(error_message) + return False, error_message + + +# 示例调用 +if __name__ == '__main__': + prompt = "一棵参天大树" + sketch_image_url = "https://help-static-aliyun-doc.aliyuncs.com/assets/img/zh-CN/6609471071/p743851.jpg" + success, message = generate_image(prompt, sketch_image_url) + if not success: + print(f"生成失败: {message}") \ No newline at end of file diff --git a/AI/WxMini/Test/T1_WenBenShengTu.py b/AI/WxMini/Test/T1_WenBenShengTu.py new file mode 100644 index 00000000..fba6c4da --- /dev/null +++ b/AI/WxMini/Test/T1_WenBenShengTu.py @@ -0,0 +1,44 @@ +from http import HTTPStatus +from urllib.parse import urlparse, unquote +from pathlib import PurePosixPath +from dashscope import ImageSynthesis +from WxMini.Milvus.Config.MulvusConfig import * + +def generate_image(prompt, model="wanx2.1-t2i-turbo", n=1, size='1024*1024'): + """ + 根据提示词生成图片 + :param prompt: 提示词 + :param model: 使用的模型,默认为 "wanx2.1-t2i-turbo" + :param n: 生成图片的数量,默认为 1 + :param size: 图片尺寸,默认为 '1024*1024' + :return: (success, message) success 为 True 表示成功,False 表示失败;message 为提示信息 + """ + try: + print('----同步调用,请稍等----') + rsp = ImageSynthesis.call(api_key=MODEL_API_KEY, model=model, prompt=prompt, n=n, size=size) + print('response: %s' % rsp) + + if rsp.status_code == HTTPStatus.OK: + # 在当前目录下保存图片 + for result in rsp.output.results: + file_name = PurePosixPath(unquote(urlparse(result.url).path)).parts[-1] + print(f"图片已生成,文件名: {file_name}") + return True, "图片生成成功" + else: + # 生成失败,返回错误信息 + error_message = f"同步调用失败,状态码: {rsp.status_code}, 错误码: {rsp.code}, 错误信息: {rsp.message}" + print(error_message) + return False, error_message + except Exception as e: + # 捕获异常,返回错误信息 + error_message = f"生成图片时发生异常: {str(e)}" + print(error_message) + return False, error_message + + +# 示例调用 +if __name__ == '__main__': + prompt = "哪吒闹海" + success, message = generate_image(prompt) + if not success: + print(f"生成失败: {message}") \ No newline at end of file diff --git a/AI/WxMini/Test/文档.txt b/AI/WxMini/Test/文档.txt new file mode 100644 index 00000000..97384848 --- /dev/null +++ b/AI/WxMini/Test/文档.txt @@ -0,0 +1,2 @@ +大模型服务平台百炼 +https://help.aliyun.com/zh/model-studio/user-guide/style-repaint?spm=a2c4g.11186623.help-menu-2400256.d_1_0_6_1.6c05408du5HUT3 \ No newline at end of file