LCA-GPT/DataAnalysis/report.py

97 lines
2.9 KiB
Python
Raw Normal View History

2024-07-30 10:11:41 +08:00
import os
import re
import gradio as gr
from PIL import Image
from pprint import pprint
from qwen_agent.agents import Assistant
import sys
os.chdir(sys.path[0])
os.environ['TMPDIR'] = "/home/zhangxj/WorkFile/LCA-GPT/LCARAG/DataAnalysis/tmp"
2024-12-29 16:18:16 +08:00
# 填写Qwen的api-key
2024-07-30 10:11:41 +08:00
llm_cfg = {
2024-12-29 16:18:16 +08:00
'model': 'qwen-1.5-72b',
2024-07-30 10:11:41 +08:00
'model_server': 'dashscope',
2024-12-29 16:18:16 +08:00
'api_key': "xxx",
2024-07-30 10:11:41 +08:00
}
system_instruction = '''你是一位专注在生命周期领域做数据分析的助手,在数据分析之后,
2024-12-29 16:18:16 +08:00
如果有可视化要求请使用 `plt.show()` 显示图像,输出图像的路径,不需要保存
2024-07-30 10:11:41 +08:00
最后请对数据分析结果结合生命周期评价领域知识进行解释'''
tools = ['code_interpreter'] # `code_interpreter` is a built-in tool for executing code.
messages = [] # This stores the chat history.
2024-12-29 16:18:16 +08:00
name = '中国电建集团成都电力金具有限公司产品'
files = ["/home/zhangxj/WorkFile/LCA-GPT/DataAnalysis/lciaData/"+name+".csv","/home/zhangxj/WorkFile/LCA-GPT/DataAnalysis/lciaData/报告模板.md"]
2024-07-30 10:11:41 +08:00
2024-12-29 16:18:16 +08:00
def getImage(response):
'''
获取图像路径
'''
pattern = r'workspace.*?\.png'
path = None
for res in response:
content = str(res['content'])
matches = re.findall(pattern, content)
# print("*********",res['content'])
if len(matches):
path = matches[0]
# print("###### path #####,", path)
return path
# Convert bot response to string
res_str = ""
image_paths = []
while True:
print("input>")
inputs = input()
if inputs == 'q':
break
image_path_str = ''
if len(image_paths):
image_path_str = ' '.join(image_paths)
user_input = inputs+"多个图像路径如下,"+image_path_str
else:
user_input = inputs
global responses
response = []
messages.append({'role': 'user', 'content': user_input})
bot = Assistant(llm=llm_cfg,
2024-07-30 10:11:41 +08:00
system_message=system_instruction,
function_list=tools,
files=files)
2024-12-29 16:18:16 +08:00
for response in bot.run(messages=messages):
continue
2024-07-30 10:11:41 +08:00
2024-12-29 16:18:16 +08:00
# pprint(response)
messages.extend(response)
image_path = getImage(response)
if image_path:
image_paths.append(image_path)
tmp = ''
for res in response:
tmp += res['content']
print(tmp)
2024-07-30 10:11:41 +08:00
for res in response:
res_str += res['content']
try:
2024-12-29 16:18:16 +08:00
with open("/home/zhangxj/WorkFile/LCA-GPT/DataAnalysis/Qwen1.5-72b"+name+".md", "w", encoding="utf-8") as f:
2024-07-30 10:11:41 +08:00
f.write(res_str)
except IOError as e:
print(f"An error occurred: {e}")
2024-12-29 16:18:16 +08:00
# print(res_str)
'''
英文中英文都有结果
报告标准化完整准确模板数据长了之后多轮对话
2024-07-30 10:11:41 +08:00
2024-12-29 16:18:16 +08:00
请按照报告案例1作为模板用你掌握的信息进行填充并且将可视化得到的图像结果插入到报告中并加以分析
以markdown格式输出填充数据信息之后的报告
'''