LCA-GPT/DataAnalysis/report.py

97 lines
2.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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"
# 填写Qwen的api-key
llm_cfg = {
'model': 'qwen-1.5-72b',
'model_server': 'dashscope',
'api_key': "xxx",
}
system_instruction = '''你是一位专注在生命周期领域做数据分析的助手,在数据分析之后,
如果有可视化要求,请使用 `plt.show()` 显示图像,输出图像的路径,不需要保存。
最后,请对数据分析结果结合生命周期评价领域知识进行解释。'''
tools = ['code_interpreter'] # `code_interpreter` is a built-in tool for executing code.
messages = [] # This stores the chat history.
name = '中国电建集团成都电力金具有限公司产品'
files = ["/home/zhangxj/WorkFile/LCA-GPT/DataAnalysis/lciaData/"+name+".csv","/home/zhangxj/WorkFile/LCA-GPT/DataAnalysis/lciaData/报告模板.md"]
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,
system_message=system_instruction,
function_list=tools,
files=files)
for response in bot.run(messages=messages):
continue
# 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)
for res in response:
res_str += res['content']
try:
with open("/home/zhangxj/WorkFile/LCA-GPT/DataAnalysis/Qwen1.5-72b"+name+".md", "w", encoding="utf-8") as f:
f.write(res_str)
except IOError as e:
print(f"An error occurred: {e}")
# print(res_str)
'''
英文,中英文都有结果;
报告标准化:完整;准确;模板;数据长了之后多轮对话。
请按照报告案例1作为模板用你掌握的信息进行填充并且将可视化得到的图像结果插入到报告中并加以分析
以markdown格式输出填充数据信息之后的报告。
'''