Upload app.py
Browse files
app.py
CHANGED
@@ -27,8 +27,8 @@ os.environ["OPENAI_API_KEY"] = os.environ['user_token']
|
|
27 |
openai.api_key = os.environ['user_token']
|
28 |
os.environ["VERBOSE"] = "True" # 可以看到具体的错误?
|
29 |
|
30 |
-
|
31 |
-
myfont = FontProperties(fname='YaHei.ttf')
|
32 |
sns.set(font='myfont')
|
33 |
# plt.title("这个是数据分布图", fontsize=12, fontproperties=myfont)
|
34 |
# plt.xlabel('数据A', fontproperties=myfont)
|
@@ -44,23 +44,26 @@ sns.set(font='myfont')
|
|
44 |
# st.title("ChatGPT-like clone")
|
45 |
st.title("大语言模型商业数据分析中心")
|
46 |
st.subheader("Business Data Analytics Based Upon LLM")
|
47 |
-
uploaded_file = st.file_uploader(
|
|
|
48 |
# uploaded_file = st.file_uploader("选择一个文件", type=(["csv","txt","xlsx","xls"]))
|
49 |
# st.write(uploaded_file)
|
50 |
if uploaded_file is not None:
|
51 |
# csv_file = csv.reader(uploaded_file)
|
52 |
csv_file = pd.read_csv(uploaded_file)
|
53 |
-
st.write(csv_file[:5])
|
54 |
|
55 |
uploaded_file_name = "File_provided"
|
56 |
temp_dir = tempfile.TemporaryDirectory()
|
57 |
-
|
|
|
58 |
with open(uploaded_file_path, 'wb') as output_temporary_file:
|
59 |
# output_temporary_file.write(uploaded_file.read())
|
60 |
-
|
|
|
61 |
# st.write(uploaded_file_path) #* 可以查看文件是否真实存在,然后是否可以
|
62 |
|
63 |
-
|
64 |
# # files = pd.read_csv(uploaded_file)
|
65 |
# bytes_data = uploaded_file.getvalue()
|
66 |
# # st.write(bytes_data)
|
@@ -79,10 +82,12 @@ if uploaded_file is not None:
|
|
79 |
|
80 |
# openai.api_key = st.secrets["OPENAI_API_KEY"]
|
81 |
|
|
|
82 |
async def main():
|
83 |
if "openai_model" not in st.session_state:
|
84 |
# st.session_state["openai_model"] = "gpt-3.5-turbo"
|
85 |
-
|
|
|
86 |
|
87 |
if "messages" not in st.session_state:
|
88 |
st.session_state.messages = []
|
@@ -100,7 +105,7 @@ async def main():
|
|
100 |
message_placeholder = st.empty()
|
101 |
full_response = ""
|
102 |
|
103 |
-
|
104 |
# for response in openai.ChatCompletion.create(
|
105 |
# model=st.session_state["openai_model"],
|
106 |
# messages=[
|
@@ -121,7 +126,9 @@ async def main():
|
|
121 |
1. 你需要用提问的语言来回答(如:中文提问你就用中文来回答,英文提问你就用英文来回答)。
|
122 |
2. 如果要求你输出图表,那么图的解析度dpi需要设定为300。图尽量使用seaborn库。seaborn库的参数设定:sns.set(rc={'axes.facecolor':'#FFF9ED','figure.facecolor':'#FFF9ED'}, palette='deep'。
|
123 |
3. matplotlib和seaborn图表上的字体可以设置为中文字体,如下:
|
124 |
-
|
|
|
|
|
125 |
plt.rcParams['font.family'] = myfont
|
126 |
plt.title中:fontproperties=myfont
|
127 |
plt.xlabel中:fontProperties=myfont
|
@@ -129,19 +136,20 @@ async def main():
|
|
129 |
sns.set(font='myfont')
|
130 |
|
131 |
-------------------------------------------------------------------------
|
132 |
-
"""
|
133 |
|
134 |
sns.set(font='myfont')
|
135 |
|
136 |
-
user_request = environ_settings + "\n\n"+
|
|
|
137 |
# print('user_request: \n', user_request)
|
138 |
|
139 |
-
|
140 |
files = [File.from_path(str(uploaded_file_path))]
|
141 |
|
142 |
with st.status('processing...', expanded=True, state='running') as status:
|
143 |
sns.set(font='myfont')
|
144 |
-
|
145 |
response = await session.generate_response(
|
146 |
user_request, files=files
|
147 |
)
|
@@ -153,26 +161,25 @@ async def main():
|
|
153 |
|
154 |
# for file in response.files:
|
155 |
for i, file in enumerate(response.files):
|
156 |
-
# await file.asave(f"/Users/yunshi/Downloads/360Data/Data Center/Working-On Task/演讲与培训/2023ChatGPT/Coding/code_interpreter/output{i}.png") ##working.
|
157 |
-
# st.image(file.get_image() #! working.
|
158 |
# file.show_image()
|
159 |
# st.image(file.get_image(), width=500, output_format='png')
|
160 |
-
|
161 |
-
|
|
|
162 |
|
163 |
# message_placeholder.markdown(full_response + "▌") ## orignal code.
|
164 |
# message_placeholder.markdown(full_response) ## orignal code.
|
165 |
st.write(full_response)
|
166 |
status.update(label='complete', state='complete')
|
167 |
|
168 |
-
#TODO:看看是否缩进准确? 放在这里好像可以。
|
169 |
-
await session.astop()
|
170 |
-
|
171 |
|
172 |
st.session_state.messages.append(
|
173 |
{"role": "assistant", "content": full_response})
|
174 |
|
175 |
-
|
176 |
|
177 |
if __name__ == "__main__":
|
178 |
import asyncio
|
|
|
27 |
openai.api_key = os.environ['user_token']
|
28 |
os.environ["VERBOSE"] = "True" # 可以看到具体的错误?
|
29 |
|
30 |
+
# 设置中文字体。
|
31 |
+
myfont = FontProperties(fname='YaHei.ttf') # NOTE: 注意这里的云服务器路径格式。
|
32 |
sns.set(font='myfont')
|
33 |
# plt.title("这个是数据分布图", fontsize=12, fontproperties=myfont)
|
34 |
# plt.xlabel('数据A', fontproperties=myfont)
|
|
|
44 |
# st.title("ChatGPT-like clone")
|
45 |
st.title("大语言模型商业数据分析中心")
|
46 |
st.subheader("Business Data Analytics Based Upon LLM")
|
47 |
+
uploaded_file = st.file_uploader(
|
48 |
+
"Choose a file", type=(["csv", "txt", "xlsx", "xls"]))
|
49 |
# uploaded_file = st.file_uploader("选择一个文件", type=(["csv","txt","xlsx","xls"]))
|
50 |
# st.write(uploaded_file)
|
51 |
if uploaded_file is not None:
|
52 |
# csv_file = csv.reader(uploaded_file)
|
53 |
csv_file = pd.read_csv(uploaded_file)
|
54 |
+
st.write(csv_file[:5]) # 这里只是显示文件,后面需要定位文件所在的绝对路径。
|
55 |
|
56 |
uploaded_file_name = "File_provided"
|
57 |
temp_dir = tempfile.TemporaryDirectory()
|
58 |
+
# ! working.
|
59 |
+
uploaded_file_path = pathlib.Path(temp_dir.name) / uploaded_file_name
|
60 |
with open(uploaded_file_path, 'wb') as output_temporary_file:
|
61 |
# output_temporary_file.write(uploaded_file.read())
|
62 |
+
# ! 必须用这种格式读入内容,然后才可以写入temporary文件夹中。
|
63 |
+
output_temporary_file.write(uploaded_file.getvalue())
|
64 |
# st.write(uploaded_file_path) #* 可以查看文件是否真实存在,然后是否可以
|
65 |
|
66 |
+
# how to read data inside streamlit.
|
67 |
# # files = pd.read_csv(uploaded_file)
|
68 |
# bytes_data = uploaded_file.getvalue()
|
69 |
# # st.write(bytes_data)
|
|
|
82 |
|
83 |
# openai.api_key = st.secrets["OPENAI_API_KEY"]
|
84 |
|
85 |
+
|
86 |
async def main():
|
87 |
if "openai_model" not in st.session_state:
|
88 |
# st.session_state["openai_model"] = "gpt-3.5-turbo"
|
89 |
+
# NOTE: data analysis module must use GPT-4.
|
90 |
+
st.session_state["openai_model"] = "gpt-4"
|
91 |
|
92 |
if "messages" not in st.session_state:
|
93 |
st.session_state.messages = []
|
|
|
105 |
message_placeholder = st.empty()
|
106 |
full_response = ""
|
107 |
|
108 |
+
# 原始示例 https://docs.streamlit.io/knowledge-base/tutorials/build-conversational-apps
|
109 |
# for response in openai.ChatCompletion.create(
|
110 |
# model=st.session_state["openai_model"],
|
111 |
# messages=[
|
|
|
126 |
1. 你需要用提问的语言来回答(如:中文提问你就用中文来回答,英文提问你就用英文来回答)。
|
127 |
2. 如果要求你输出图表,那么图的解析度dpi需要设定为300。图尽量使用seaborn库。seaborn库的参数设定:sns.set(rc={'axes.facecolor':'#FFF9ED','figure.facecolor':'#FFF9ED'}, palette='deep'。
|
128 |
3. matplotlib和seaborn图表上的字体可以设置为中文字体,如下:
|
129 |
+
3.1 首先从根目录下安装中文字体:
|
130 |
+
myfont = matplotlib.font_manager.FontProperties(fname='YaHei.ttf')
|
131 |
+
3.2 然后在图表中设置字体:
|
132 |
plt.rcParams['font.family'] = myfont
|
133 |
plt.title中:fontproperties=myfont
|
134 |
plt.xlabel中:fontProperties=myfont
|
|
|
136 |
sns.set(font='myfont')
|
137 |
|
138 |
-------------------------------------------------------------------------
|
139 |
+
""" # seaborn中的palette参数可以设定图表的颜色,选项包括:deep, muted, pastel, bright, dark, colorblind,Spectral。更多参数可以参考:https://seaborn.pydata.org/generated/seaborn.color_palette.html。
|
140 |
|
141 |
sns.set(font='myfont')
|
142 |
|
143 |
+
user_request = environ_settings + "\n\n" + \
|
144 |
+
"你需要完成以下任务:\n\n" + prompt + f"注:文件位置在{uploaded_file_path}"
|
145 |
# print('user_request: \n', user_request)
|
146 |
|
147 |
+
# 加载上传的文件,主要路径在上面代码中。
|
148 |
files = [File.from_path(str(uploaded_file_path))]
|
149 |
|
150 |
with st.status('processing...', expanded=True, state='running') as status:
|
151 |
sns.set(font='myfont')
|
152 |
+
# generate the response
|
153 |
response = await session.generate_response(
|
154 |
user_request, files=files
|
155 |
)
|
|
|
161 |
|
162 |
# for file in response.files:
|
163 |
for i, file in enumerate(response.files):
|
164 |
+
# await file.asave(f"/Users/yunshi/Downloads/360Data/Data Center/Working-On Task/演讲与培训/2023ChatGPT/Coding/code_interpreter/output{i}.png") ##working.
|
165 |
+
# st.image(file.get_image() #! working.
|
166 |
# file.show_image()
|
167 |
# st.image(file.get_image(), width=500, output_format='png')
|
168 |
+
# * 注意这里的设定,可以提高图片的精细程度。
|
169 |
+
st.image(file.get_image(), width=None,
|
170 |
+
output_format='PNG')
|
171 |
|
172 |
# message_placeholder.markdown(full_response + "▌") ## orignal code.
|
173 |
# message_placeholder.markdown(full_response) ## orignal code.
|
174 |
st.write(full_response)
|
175 |
status.update(label='complete', state='complete')
|
176 |
|
177 |
+
# TODO:看看是否缩进准确? 放在这里好像可以。
|
178 |
+
await session.astop() # ! 确认需要关闭。
|
|
|
179 |
|
180 |
st.session_state.messages.append(
|
181 |
{"role": "assistant", "content": full_response})
|
182 |
|
|
|
183 |
|
184 |
if __name__ == "__main__":
|
185 |
import asyncio
|