|
''' |
|
参考: https://github.com/shroominic/codeinterpreter-api |
|
|
|
1. 可以存在本地,然后再调出来。 working. |
|
1. 可以直接在内存中读出图片。 |
|
''' |
|
|
|
|
|
import matplotlib as mpl |
|
from codeinterpreterapi import CodeInterpreterSession, File |
|
import streamlit as st |
|
from codeinterpreterapi import CodeInterpreterSession |
|
import openai |
|
import os |
|
import matplotlib.pyplot as plt |
|
import pandas as pd |
|
from io import StringIO |
|
import csv |
|
import tempfile |
|
from tempfile import NamedTemporaryFile |
|
import pathlib |
|
from pathlib import Path |
|
import matplotlib |
|
from matplotlib.font_manager import FontProperties |
|
import seaborn as sns |
|
|
|
os.environ["OPENAI_API_KEY"] = os.environ['user_token'] |
|
openai.api_key = os.environ['user_token'] |
|
os.environ["VERBOSE"] = "True" |
|
|
|
|
|
myfont = FontProperties(fname='YaHei.ttf') |
|
mpl.rcParams['font.family'] = ['myfont'] |
|
sns.set(font='myfont') |
|
plt.rcParams['font.sans-serif'] = ['myfont'] |
|
plt.rcParams['font.family'] = 'sans-serif' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.title("大语言模型商业数据分析中心") |
|
st.subheader("Business Data Analytics Based Upon LLM") |
|
uploaded_file = st.file_uploader( |
|
"Choose a file", type=(["csv", "txt", "xlsx", "xls"])) |
|
|
|
|
|
if uploaded_file is not None: |
|
|
|
csv_file = pd.read_csv(uploaded_file) |
|
st.write(csv_file[:5]) |
|
|
|
uploaded_file_name = "File_provided" |
|
temp_dir = tempfile.TemporaryDirectory() |
|
|
|
uploaded_file_path = pathlib.Path(temp_dir.name) / uploaded_file_name |
|
with open(uploaded_file_path, 'wb') as output_temporary_file: |
|
|
|
|
|
output_temporary_file.write(uploaded_file.getvalue()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def main(): |
|
if "openai_model" not in st.session_state: |
|
|
|
|
|
st.session_state["openai_model"] = "gpt-4" |
|
|
|
if "messages" not in st.session_state: |
|
st.session_state.messages = [] |
|
|
|
for message in st.session_state.messages: |
|
with st.chat_message(message["role"]): |
|
st.markdown(message["content"]) |
|
|
|
if prompt := st.chat_input("What is up?"): |
|
st.session_state.messages.append({"role": "user", "content": prompt}) |
|
with st.chat_message("user"): |
|
st.markdown(prompt) |
|
|
|
with st.chat_message("assistant"): |
|
message_placeholder = st.empty() |
|
full_response = "" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async with CodeInterpreterSession() as session: |
|
|
|
|
|
|
|
environ_settings = """【背景要求】如果我没有告诉你任何定制化的要求,那么请你按照以下的默认要求来回答: |
|
------------------------------------------------------------------------- |
|
1. 你需要用提问的语言来回答(如:中文提问你就用中文来回答,英文提问你就用英文来回答)。 |
|
2. 如果要求你输出图表,那么图的解析度dpi需要设定为600。图尽量使用seaborn库。seaborn库的参数设定:sns.set(rc={'axes.facecolor':'#FFF9ED','figure.facecolor':'#FFF9ED'}, palette='deep', font='myfont')。 |
|
3. matplotlib和seaborn图表上的字体需要设置为中文字体,如下: |
|
plt.rcParams['font.sans-serif'] = ['myfont'] |
|
plt.rcParams['font.family'] = 'sans-serif' |
|
mpl.rcParams['font.family'] = ['myfont'] |
|
sns.set(font='myfont') |
|
------------------------------------------------------------------------- |
|
""" |
|
|
|
|
|
|
|
user_request = environ_settings + "\n\n" + \ |
|
"你需要完成以下任务:\n\n" + prompt + f"注:文件位置在{uploaded_file_path}" |
|
|
|
|
|
|
|
files = [File.from_path(str(uploaded_file_path))] |
|
|
|
with st.status('processing...', expanded=True, state='running') as status: |
|
mpl.rcParams['font.family'] = 'myfont' |
|
|
|
|
|
response = await session.generate_response( |
|
user_request, files=files |
|
) |
|
|
|
|
|
print("AI: ", response.content) |
|
full_response = response.content |
|
|
|
|
|
|
|
for i, file in enumerate(response.files): |
|
|
|
|
|
|
|
|
|
|
|
st.image(file.get_image(), width=None, |
|
output_format='PNG') |
|
|
|
|
|
|
|
st.write(full_response) |
|
status.update(label='complete', state='complete') |
|
|
|
|
|
await session.astop() |
|
|
|
st.session_state.messages.append( |
|
{"role": "assistant", "content": full_response}) |
|
|
|
|
|
if __name__ == "__main__": |
|
import asyncio |
|
|
|
asyncio.run(main()) |
|
|