Upload 2 files
Browse files
app.py
CHANGED
@@ -7,12 +7,23 @@
|
|
7 |
1. 中文字体成功。
|
8 |
from matplotlib.font_manager import FontProperties
|
9 |
myfont=FontProperties(fname='/Users/yunshi/Downloads/360Data/Data Center/Working-On Task/演讲与培训/2023ChatGPT/Coding/code_interpreter/rawdata/SimHei.ttf')
|
10 |
-
sns.set_style('whitegrid',{'font.sans-serif':['simhei','Arial']})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
|
13 |
'''
|
14 |
# TODO:1. Chinese display isssue. 2. account system.
|
15 |
|
|
|
|
|
|
|
16 |
import requests
|
17 |
from codeinterpreterapi import CodeInterpreterSession, File
|
18 |
import streamlit as st
|
@@ -31,9 +42,13 @@ from matplotlib.font_manager import FontProperties
|
|
31 |
import seaborn as sns
|
32 |
from time import sleep
|
33 |
import streamlit_authenticator as stauth
|
|
|
|
|
34 |
|
35 |
os.environ["OPENAI_API_KEY"] = os.environ['user_token']
|
36 |
openai.api_key = os.environ['user_token']
|
|
|
|
|
37 |
# os.environ["VERBOSE"] = "True" # 可以看到具体的错误?
|
38 |
|
39 |
# # #* 如果碰到接口问题,可以启用如下设置。
|
@@ -47,9 +62,10 @@ openai.api_key = os.environ['user_token']
|
|
47 |
st.title("专业版大语言模型商业智能中心")
|
48 |
st.subheader("Artificial Intelligence Backend Center for Professionals")
|
49 |
|
50 |
-
|
51 |
reset_button_key = "reset_button"
|
52 |
-
reset_button = st.button(label=("扫清世间烦恼,清除所有记录,并开启一轮新对话 ▶"),
|
|
|
53 |
if reset_button:
|
54 |
st.session_state.conversation = None
|
55 |
st.session_state.chat_history = None
|
@@ -90,8 +106,7 @@ def upload_file(uploaded_file):
|
|
90 |
return None
|
91 |
|
92 |
|
93 |
-
|
94 |
-
bing_search_endpoint = 'https://api.bing.microsoft.com/v7.0/search'
|
95 |
|
96 |
def search(query):
|
97 |
# Construct a request
|
@@ -114,6 +129,7 @@ def search(query):
|
|
114 |
|
115 |
# openai.api_key = st.secrets["OPENAI_API_KEY"]
|
116 |
|
|
|
117 |
async def text_mode():
|
118 |
# Set a default model
|
119 |
if "openai_model" not in st.session_state:
|
@@ -239,6 +255,12 @@ async def data_mode():
|
|
239 |
1. 你需要用提问的语言来回答(如:中文提问你就用中文来回答,英文提问你就用英文来回答)。
|
240 |
2. 如果要求你输出图表,那么图的解析度dpi需要设定为600。图尽量使用seaborn库。seaborn库的参数设定:sns.set(rc={'axes.facecolor':'#FFF9ED','figure.facecolor':'#FFF9ED'}, palette='dark'。
|
241 |
3. 图上所有的文字全部翻译成<英文English>来表示。
|
|
|
|
|
|
|
|
|
|
|
|
|
242 |
-------------------------------------------------------------------------
|
243 |
""" # seaborn中的palette参数可以设定图表的颜色,选项包括:deep, muted, pastel, bright, dark, colorblind,Spectral。更多参数可以参考:https://seaborn.pydata.org/generated/seaborn.color_palette.html。
|
244 |
|
@@ -284,36 +306,107 @@ async def data_mode():
|
|
284 |
# st.session_state.messages.append({"role": "assistant", "content": full_response})
|
285 |
|
286 |
|
287 |
-
# authentication.
|
288 |
-
import yaml
|
289 |
-
from yaml.loader import SafeLoader
|
290 |
-
with open('
|
291 |
-
|
292 |
-
authenticator = stauth.Authenticate(
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
299 |
|
300 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
301 |
|
302 |
if authentication_status:
|
303 |
with st.sidebar:
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
st.header(f'**欢迎 **{
|
315 |
st.write(f'_Welcome and Hope U Enjoy Staying Here!_')
|
316 |
authenticator.logout('登出', 'sidebar')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
317 |
# with st.header(body="欢迎"):
|
318 |
# st.markdown("# 欢迎使用大语言模型商业智能中心")
|
319 |
# with st.expander(label=("**重要的使用注意事项**"), expanded=True):
|
@@ -328,37 +421,43 @@ if authentication_status:
|
|
328 |
with st.text(body="说明"):
|
329 |
st.markdown("* “联网模式”与搜索引擎一致,仅限一轮对话,不会保持之前的会话记录。")
|
330 |
with st.text(body="说明"):
|
331 |
-
st.markdown(
|
|
|
332 |
with st.text(body="说明"):
|
333 |
st.markdown("* “数据模式”推荐上传csv格式的文件,部分Excel文件容易出现数据不兼容的情况。")
|
334 |
-
|
335 |
st.markdown("#### 参考资料")
|
336 |
with st.expander(label="**核心模式的专用提示词Prompt示例**", expanded=False):
|
337 |
# with st.subheader(body="提示词Prompt"):
|
338 |
-
st.code(
|
339 |
-
|
|
|
|
|
340 |
st.code(body="给我出一个迪奥2023春季发布会活动策划。", language='plaintext')
|
341 |
st.code(body="帮我按照正式会议结构写一个会邀:主题是xx手机游戏立项会议。", language='plaintext')
|
342 |
st.code(body="帮我写一个车内健康监测全场景落地的项目计划,用表格。", language='plaintext')
|
343 |
st.code(
|
344 |
body="同时掷两枚质地均匀的骰子,则两枚骰子向上的点数之和为 7 的概率是多少。", language='plaintext')
|
345 |
st.code(body="写一篇产品经理的演讲稿,注意使用以下词汇: 赋能,抓手,中台,闭环,落地,漏斗,沉淀,给到,同步,对齐,对标,迭代,拉通,打通,升级,交付,聚焦,倒逼,复盘,梳理,方案,联动,透传,咬合,洞察,渗透,兜底,解耦,耦合,复用,拆解。", language='plaintext')
|
346 |
-
|
347 |
with st.expander(label="**数据模式的专用提示词Prompt示例**", expanded=False):
|
348 |
# with st.subheader(body="提示词Prompt"):
|
349 |
st.code(body="分析此数据集并绘制一些'有趣的图表'。", language='python')
|
350 |
-
st.code(
|
|
|
351 |
st.code(body="对于这个文件中的[xxx]数据给我一个'整体的分析'。", language='python')
|
352 |
st.code(body="对于[xxx]数据给我一个'直方图',提供图表,并给出分析结果。", language='python')
|
353 |
st.code(body="对于[xxx]数据给我一个'小提琴图',并给出分析结果。", language='python')
|
354 |
st.code(
|
355 |
body="对于[X,Y,Z]数据在一个'分布散点图 (stripplot)',所有的数据在一张图上展现, 并给出分析结果。", language='python')
|
356 |
-
st.code(body="对于[X,Y]数据,进行'T检验',你需要展示图表,并给出分析结果。",
|
357 |
-
|
|
|
|
|
358 |
|
359 |
-
col1, col2= st.columns(spec=[1, 2])
|
360 |
radio_2 = col2.radio(label='模式选择', options=[
|
361 |
-
'核心模式', '联网模式', '知识库模式','数据模式'], horizontal=True, label_visibility='visible')
|
362 |
# radio_1 = col1.selectbox(label='ChatGPT版本', options=[
|
363 |
# 'GPT-3.5', 'GPT-4.0'], label_visibility='visible')
|
364 |
radio_1 = col1.radio(label='ChatGPT版本', options=[
|
@@ -367,8 +466,8 @@ if authentication_status:
|
|
367 |
elif authentication_status == False:
|
368 |
st.error('⛔ 用户名或密码错误!')
|
369 |
elif authentication_status == None:
|
370 |
-
st.warning('
|
371 |
-
|
372 |
|
373 |
if __name__ == "__main__":
|
374 |
import asyncio
|
@@ -391,4 +490,3 @@ if __name__ == "__main__":
|
|
391 |
except:
|
392 |
# st.markdown('**请先登录!**')
|
393 |
pass
|
394 |
-
|
|
|
7 |
1. 中文字体成功。
|
8 |
from matplotlib.font_manager import FontProperties
|
9 |
myfont=FontProperties(fname='/Users/yunshi/Downloads/360Data/Data Center/Working-On Task/演讲与培训/2023ChatGPT/Coding/code_interpreter/rawdata/SimHei.ttf')
|
10 |
+
sns.set_style('whitegrid',{'font.sans-serif':['simhei','Arial']})
|
11 |
+
1. 解决了account login的问题,主要格式:
|
12 |
+
## 需要严格的按照yaml文件的格式来定义如下几个字段。
|
13 |
+
for un, name, pw in zip(users, names, hashed_passwords):
|
14 |
+
# user_dict = {"name":name,"password":pw}
|
15 |
+
user_dict = {"name": un, "password": pw}
|
16 |
+
# credentials["usernames"].update({un:user_dict})
|
17 |
+
credentials["usernames"].update({un: user_dict})
|
18 |
+
|
19 |
|
20 |
|
21 |
'''
|
22 |
# TODO:1. Chinese display isssue. 2. account system.
|
23 |
|
24 |
+
from dotenv import load_dotenv # pip3 install python-dotenv
|
25 |
+
import database as db
|
26 |
+
from deta import Deta # pip3 install deta
|
27 |
import requests
|
28 |
from codeinterpreterapi import CodeInterpreterSession, File
|
29 |
import streamlit as st
|
|
|
42 |
import seaborn as sns
|
43 |
from time import sleep
|
44 |
import streamlit_authenticator as stauth
|
45 |
+
import database as db # python文件同目录下的.py程序,直接导入。
|
46 |
+
import deta
|
47 |
|
48 |
os.environ["OPENAI_API_KEY"] = os.environ['user_token']
|
49 |
openai.api_key = os.environ['user_token']
|
50 |
+
bing_search_api_key = os.environ['bing_api_key']
|
51 |
+
bing_search_endpoint = 'https://api.bing.microsoft.com/v7.0/search'
|
52 |
# os.environ["VERBOSE"] = "True" # 可以看到具体的错误?
|
53 |
|
54 |
# # #* 如果碰到接口问题,可以启用如下设置。
|
|
|
62 |
st.title("专业版大语言模型商业智能中心")
|
63 |
st.subheader("Artificial Intelligence Backend Center for Professionals")
|
64 |
|
65 |
+
# clear conversion.
|
66 |
reset_button_key = "reset_button"
|
67 |
+
reset_button = st.button(label=("扫清世间烦恼,清除所有记录,并开启一轮新对话 ▶"),
|
68 |
+
key=reset_button_key, use_container_width=True, type="secondary")
|
69 |
if reset_button:
|
70 |
st.session_state.conversation = None
|
71 |
st.session_state.chat_history = None
|
|
|
106 |
return None
|
107 |
|
108 |
|
109 |
+
|
|
|
110 |
|
111 |
def search(query):
|
112 |
# Construct a request
|
|
|
129 |
|
130 |
# openai.api_key = st.secrets["OPENAI_API_KEY"]
|
131 |
|
132 |
+
|
133 |
async def text_mode():
|
134 |
# Set a default model
|
135 |
if "openai_model" not in st.session_state:
|
|
|
255 |
1. 你需要用提问的语言来回答(如:中文提问你就用中文来回答,英文提问你就用英文来回答)。
|
256 |
2. 如果要求你输出图表,那么图的解析度dpi需要设定为600。图尽量使用seaborn库。seaborn库的参数设定:sns.set(rc={'axes.facecolor':'#FFF9ED','figure.facecolor':'#FFF9ED'}, palette='dark'。
|
257 |
3. 图上所有的文字全部翻译成<英文English>来表示。
|
258 |
+
4. 回答时尽可能地展示分析所对应的图表,并提供分析结果。 你需要按如下格式提供内容:
|
259 |
+
|
260 |
+
1. 提供详细且专业的分析结果,提供足够的分析依据。
|
261 |
+
2. 给出可能造成这一结果的可能原因有哪些?
|
262 |
+
|
263 |
+
以上内容全部用序列号格式来表达。
|
264 |
-------------------------------------------------------------------------
|
265 |
""" # seaborn中的palette参数可以设定图表的颜色,选项包括:deep, muted, pastel, bright, dark, colorblind,Spectral。更多参数可以参考:https://seaborn.pydata.org/generated/seaborn.color_palette.html。
|
266 |
|
|
|
306 |
# st.session_state.messages.append({"role": "assistant", "content": full_response})
|
307 |
|
308 |
|
309 |
+
# authentication with a local yaml file.
|
310 |
+
# import yaml
|
311 |
+
# from yaml.loader import SafeLoader
|
312 |
+
# with open('/Users/yunshi/Downloads/360Data/Data Center/Working-On Task/演讲与培训/2023ChatGPT/Coding/code_interpreter/config.yaml') as file:
|
313 |
+
# config = yaml.load(file, Loader=SafeLoader)
|
314 |
+
# authenticator = stauth.Authenticate(
|
315 |
+
# config['credentials'],
|
316 |
+
# config['cookie']['name'],
|
317 |
+
# config['cookie']['key'],
|
318 |
+
# config['cookie']['expiry_days'],
|
319 |
+
# config['preauthorized']
|
320 |
+
# )
|
321 |
+
|
322 |
+
# authentication with a remove cloud-based database.
|
323 |
+
# 导入云端用户数据库。
|
324 |
+
|
325 |
+
# DETA_KEY = "c0zegv33efm_4MBTaoQAn76GzUfsZeKV64Uh9qMY3WZb"
|
326 |
+
# load_dotenv(".env")
|
327 |
+
# DETA_KEY = os.getenv("DETA_KEY")
|
328 |
+
# print(DETA_KEY)
|
329 |
+
|
330 |
+
# deta = Deta(DETA_KEY)
|
331 |
+
|
332 |
+
# mybase is the name of the database in Deta. You can change it to any name you want.
|
333 |
+
credentials = {"usernames":{}}
|
334 |
+
# credentials = {"users": {}}
|
335 |
+
# db = db()
|
336 |
+
users = []
|
337 |
+
email = []
|
338 |
+
passwords = []
|
339 |
+
names = []
|
340 |
+
|
341 |
+
for row in db.fetch_all_users():
|
342 |
+
# users.append(row["key"])
|
343 |
+
# names.append(row["username"])
|
344 |
+
users.append(row["username"])
|
345 |
+
email.append(row["email"])
|
346 |
+
names.append(row["key"])
|
347 |
+
passwords.append(row["password"])
|
348 |
+
|
349 |
+
hashed_passwords = stauth.Hasher(passwords).generate()
|
350 |
+
|
351 |
+
|
352 |
+
## 需要严格的按照yaml文件的格式来定义如下几个字段。
|
353 |
+
for un, name, pw in zip(users, names, hashed_passwords):
|
354 |
+
# user_dict = {"name":name,"password":pw}
|
355 |
+
user_dict = {"name": un, "password": pw}
|
356 |
+
# credentials["usernames"].update({un:user_dict})
|
357 |
+
credentials["usernames"].update({un: user_dict})
|
358 |
+
|
359 |
+
# ## sign-up模块,未完成。
|
360 |
+
# database_table = []
|
361 |
+
# # print(pd.DataFrame(credentials))
|
362 |
+
# for i in credentials['usernames'].keys():
|
363 |
+
# # print("i:",i)
|
364 |
+
# # print("name",credentials['usernames'][i]['name'])
|
365 |
+
# # print("password",credentials['usernames'][i]['password'])
|
366 |
+
# database_table.append([i,credentials['usernames'][i]['name'],credentials['usernames'][i]['password']])
|
367 |
+
# print("database_table:",database_table)
|
368 |
|
369 |
+
|
370 |
+
authenticator = stauth.Authenticate(
|
371 |
+
credentials=credentials, cookie_name="joeshi_gpt", key='abcedefg', cookie_expiry_days=30)
|
372 |
+
|
373 |
+
user, authentication_status, username = authenticator.login('用户登录', 'main')
|
374 |
+
# print("name", name, "username", username)
|
375 |
+
|
376 |
+
# ## sign-up widget,未完成。
|
377 |
+
# try:
|
378 |
+
# if authenticator.register_user('新用户注册', preauthorization=False):
|
379 |
+
# # for list in database_table:
|
380 |
+
# # db.update_user(username=list[0], name=list[1], password=list[2])
|
381 |
+
# db.update_user(username=list[-1][0], name=list[-1][1], password=list[-1][2])
|
382 |
+
# # st.success('User registered successfully')
|
383 |
+
# st.success('注册成功!')
|
384 |
+
# except Exception as e:
|
385 |
+
# st.error(e)
|
386 |
|
387 |
if authentication_status:
|
388 |
with st.sidebar:
|
389 |
+
st.markdown(
|
390 |
+
"""
|
391 |
+
<style>
|
392 |
+
[data-testid="stSidebar"][aria-expanded="true"]{
|
393 |
+
min-width: 600px;
|
394 |
+
max-width: 600px;
|
395 |
+
}
|
396 |
+
""",
|
397 |
+
unsafe_allow_html=True,
|
398 |
+
)
|
399 |
+
st.header(f'**欢迎 **{username}** 来到人工智能的世界** ♠')
|
400 |
st.write(f'_Welcome and Hope U Enjoy Staying Here!_')
|
401 |
authenticator.logout('登出', 'sidebar')
|
402 |
+
|
403 |
+
## reset password widget
|
404 |
+
# try:
|
405 |
+
# if authenticator.reset_password(st.session_state["username"], 'Reset password'):
|
406 |
+
# st.success('Password modified successfully')
|
407 |
+
# except Exception as e:
|
408 |
+
# st.error(e)
|
409 |
+
|
410 |
# with st.header(body="欢迎"):
|
411 |
# st.markdown("# 欢迎使用大语言模型商业智能中心")
|
412 |
# with st.expander(label=("**重要的使用注意事项**"), expanded=True):
|
|
|
421 |
with st.text(body="说明"):
|
422 |
st.markdown("* “联网模式”与搜索引擎一致,仅限一轮对话,不会保持之前的会话记录。")
|
423 |
with st.text(body="说明"):
|
424 |
+
st.markdown(
|
425 |
+
"* “数据模式”暂时只支持1000个单元格以内的数据分析,单元格中的内容不支持中文数据(表头也尽量不使用中文)。一般运行时间在1-5分钟左右,期间需要保持网络畅通。")
|
426 |
with st.text(body="说明"):
|
427 |
st.markdown("* “数据模式”推荐上传csv格式的文件,部分Excel文件容易出现数据不兼容的情况。")
|
428 |
+
|
429 |
st.markdown("#### 参考资料")
|
430 |
with st.expander(label="**核心模式的专用提示词Prompt示例**", expanded=False):
|
431 |
# with st.subheader(body="提示词Prompt"):
|
432 |
+
st.code(
|
433 |
+
body="继续用中文写一篇关于 [文章主题] 的文章,以下列句子开头:[文章开头]。", language='plaintext')
|
434 |
+
st.code(body="将以下文字概括为 100 个字,使其易于阅读和理解。避免使用复杂的句子结构或技术术语。",
|
435 |
+
language='plaintext')
|
436 |
st.code(body="给我出一个迪奥2023春季发布会活动策划。", language='plaintext')
|
437 |
st.code(body="帮我按照正式会议结构写一个会邀:主题是xx手机游戏立项会议。", language='plaintext')
|
438 |
st.code(body="帮我写一个车内健康监测全场景落地的项目计划,用表格。", language='plaintext')
|
439 |
st.code(
|
440 |
body="同时掷两枚质地均匀的骰子,则两枚骰子向上的点数之和为 7 的概率是多少。", language='plaintext')
|
441 |
st.code(body="写一篇产品经理的演讲稿,注意使用以下词汇: 赋能,抓手,中台,闭环,落地,漏斗,沉淀,给到,同步,对齐,对标,迭代,拉通,打通,升级,交付,聚焦,倒逼,复盘,梳理,方案,联动,透传,咬合,洞察,渗透,兜底,解耦,耦合,复用,拆解。", language='plaintext')
|
442 |
+
|
443 |
with st.expander(label="**数据模式的专用提示词Prompt示例**", expanded=False):
|
444 |
# with st.subheader(body="提示词Prompt"):
|
445 |
st.code(body="分析此数据集并绘制一些'有趣的图表'。", language='python')
|
446 |
+
st.code(
|
447 |
+
body="对于这个文件中的数据,你需要要找出[X,Y]数据之间的寻找'相关性'。", language='python')
|
448 |
st.code(body="对于这个文件中的[xxx]数据给我一个'整体的分析'。", language='python')
|
449 |
st.code(body="对于[xxx]数据给我一个'直方图',提供图表,并给出分析结果。", language='python')
|
450 |
st.code(body="对于[xxx]数据给我一个'小提琴图',并给出分析结果。", language='python')
|
451 |
st.code(
|
452 |
body="对于[X,Y,Z]数据在一个'分布散点图 (stripplot)',所有的数据在一张图上展现, 并给出分析结果。", language='python')
|
453 |
+
st.code(body="对于[X,Y]数据,进行'T检验',你需要展示图表,并给出分析结果。",
|
454 |
+
language='python')
|
455 |
+
st.code(body="对于[X,Y]数据给我一个3个类别的'聚类分析',并给出分析结果。",
|
456 |
+
language='python')
|
457 |
|
458 |
+
col1, col2 = st.columns(spec=[1, 2])
|
459 |
radio_2 = col2.radio(label='模式选择', options=[
|
460 |
+
'核心模式', '联网模式', '知识库模式', '数据模式'], horizontal=True, label_visibility='visible')
|
461 |
# radio_1 = col1.selectbox(label='ChatGPT版本', options=[
|
462 |
# 'GPT-3.5', 'GPT-4.0'], label_visibility='visible')
|
463 |
radio_1 = col1.radio(label='ChatGPT版本', options=[
|
|
|
466 |
elif authentication_status == False:
|
467 |
st.error('⛔ 用户名或密码错误!')
|
468 |
elif authentication_status == None:
|
469 |
+
st.warning('🔼 请先登录!')
|
470 |
+
|
471 |
|
472 |
if __name__ == "__main__":
|
473 |
import asyncio
|
|
|
490 |
except:
|
491 |
# st.markdown('**请先登录!**')
|
492 |
pass
|
|