Upload app.py
Browse files
app.py
CHANGED
|
@@ -24,7 +24,7 @@ from deta import Deta # pip3 install deta
|
|
| 24 |
import requests
|
| 25 |
from codeinterpreterapi import CodeInterpreterSession, File
|
| 26 |
import streamlit as st
|
| 27 |
-
from codeinterpreterapi import CodeInterpreterSession
|
| 28 |
import openai
|
| 29 |
import os
|
| 30 |
import matplotlib.pyplot as plt
|
|
@@ -50,15 +50,15 @@ import PyPDF2 ## read the local_KB PDF file.
|
|
| 50 |
import save_database_info
|
| 51 |
from datetime import datetime
|
| 52 |
|
| 53 |
-
os.environ["OPENAI_API_KEY"] =
|
| 54 |
-
openai.api_key =
|
| 55 |
# os.environ["VERBOSE"] = "True" # 可以看到具体的错误?
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
|
| 63 |
|
| 64 |
# layout settings.
|
|
@@ -146,11 +146,9 @@ def clear_all():
|
|
| 146 |
|
| 147 |
# return None
|
| 148 |
|
| 149 |
-
|
| 150 |
-
bing_search_api_key = os.environ['bing_api_key']
|
| 151 |
bing_search_endpoint = 'https://api.bing.microsoft.com/v7.0/search'
|
| 152 |
|
| 153 |
-
|
| 154 |
def search(query):
|
| 155 |
# Construct a request
|
| 156 |
# mkt = 'en-EN'
|
|
@@ -264,10 +262,11 @@ async def text_mode():
|
|
| 264 |
# async def localKB_mode():
|
| 265 |
def localKB_mode(username):
|
| 266 |
### clear all the prior conversation.
|
| 267 |
-
st.session_state.conversation = None
|
| 268 |
-
st.session_state.chat_history = None
|
| 269 |
-
st.session_state.messages = []
|
| 270 |
-
message_placeholder = st.empty()
|
|
|
|
| 271 |
|
| 272 |
print('now starts the local KB version of ChatGPT')
|
| 273 |
# Initialize chat history
|
|
@@ -346,12 +345,16 @@ def localKB_mode(username):
|
|
| 346 |
|
| 347 |
async def data_mode():
|
| 348 |
print('数据分析模式启动!')
|
|
|
|
| 349 |
# uploaded_file_path = './upload.csv'
|
| 350 |
-
# uploaded_file_path = f'./{joejoe}_upload.csv'
|
| 351 |
uploaded_file_path = f'./{username}_upload.csv'
|
| 352 |
-
#
|
| 353 |
-
|
| 354 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
|
| 356 |
# Initialize chat history
|
| 357 |
if "messages" not in st.session_state:
|
|
@@ -395,17 +398,21 @@ async def data_mode():
|
|
| 395 |
|
| 396 |
user_request = environ_settings + "\n\n" + \
|
| 397 |
"你需要完成以下任务:\n\n" + prompt + "\n\n" \
|
| 398 |
-
f"注:文件位置在{uploaded_file_path}"
|
|
|
|
| 399 |
print('user_request: \n', user_request)
|
| 400 |
|
| 401 |
# 加载上传的文件,主要路径在上面代码中。
|
| 402 |
-
files = [File.from_path(str(uploaded_file_path))]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 403 |
|
| 404 |
with st.status('Thinking...', expanded=True, state='running') as status:
|
| 405 |
# generate the response
|
| 406 |
-
response = await session.generate_response(
|
| 407 |
-
|
| 408 |
-
)
|
| 409 |
|
| 410 |
# output to the user
|
| 411 |
print("AI: ", response.content)
|
|
@@ -436,7 +443,7 @@ async def data_mode():
|
|
| 436 |
### authentication with a local yaml file.
|
| 437 |
import yaml
|
| 438 |
from yaml.loader import SafeLoader
|
| 439 |
-
with open('
|
| 440 |
config = yaml.load(file, Loader=SafeLoader)
|
| 441 |
authenticator = stauth.Authenticate(
|
| 442 |
config['credentials'],
|
|
@@ -609,24 +616,25 @@ def upload_file(uploaded_file):
|
|
| 609 |
import localKB_construct
|
| 610 |
# st.write(upload_file)
|
| 611 |
localKB_construct.process_file(uploaded_file, username)
|
|
|
|
| 612 |
save_database_info.save_database_info(f'./{username}/database_name.csv', filename, str(datetime.now().strftime("%Y-%m-%d %H:%M")))
|
| 613 |
-
st.markdown('新知识库解析成功,请务必刷新页面,然后开启对话
|
| 614 |
# spinner = st.empty()
|
| 615 |
|
| 616 |
else:
|
| 617 |
if '.csv' in filename:
|
| 618 |
csv_file = pd.read_csv(uploaded_file)
|
| 619 |
-
csv_file.to_csv(f'./{username}
|
| 620 |
st.write(csv_file[:3]) # 这里只是显示文件,后面需要定位文件所在的绝对路径。
|
| 621 |
else:
|
| 622 |
xls_file = pd.read_excel(uploaded_file)
|
| 623 |
-
xls_file.to_csv(f'./{username}
|
| 624 |
st.write(xls_file[:3])
|
| 625 |
|
| 626 |
uploaded_file_name = "File_provided"
|
| 627 |
temp_dir = tempfile.TemporaryDirectory()
|
| 628 |
# ! working.
|
| 629 |
-
uploaded_file_path = pathlib.Path(temp_dir.name) / uploaded_file_name
|
| 630 |
# with open('./upload.csv', 'wb') as output_temporary_file:
|
| 631 |
with open(f'./{username}_upload.csv', 'wb') as output_temporary_file:
|
| 632 |
# print(f'./{name}_upload.csv')
|
|
@@ -685,9 +693,6 @@ if __name__ == "__main__":
|
|
| 685 |
if uploaded_file is not None:
|
| 686 |
# uploaded_file_path = upload_file(uploaded_file)
|
| 687 |
upload_file(uploaded_file)
|
| 688 |
-
# st.write('PDF file uploaded sucessfully!')
|
| 689 |
-
# clear_all()
|
| 690 |
-
# spinner = st.empty()
|
| 691 |
|
| 692 |
localKB_mode(username)
|
| 693 |
# asyncio.run(localKB_mode())
|
|
|
|
| 24 |
import requests
|
| 25 |
from codeinterpreterapi import CodeInterpreterSession, File
|
| 26 |
import streamlit as st
|
| 27 |
+
# from codeinterpreterapi import CodeInterpreterSession
|
| 28 |
import openai
|
| 29 |
import os
|
| 30 |
import matplotlib.pyplot as plt
|
|
|
|
| 50 |
import save_database_info
|
| 51 |
from datetime import datetime
|
| 52 |
|
| 53 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
| 54 |
+
openai.api_key = "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"
|
| 55 |
# os.environ["VERBOSE"] = "True" # 可以看到具体的错误?
|
| 56 |
|
| 57 |
+
#* 如果碰到接口问题,可以启用如下设置。
|
| 58 |
+
openai.proxy = {
|
| 59 |
+
"http": "http://127.0.0.1:7890",
|
| 60 |
+
"https": "http://127.0.0.1:7890"
|
| 61 |
+
}
|
| 62 |
|
| 63 |
|
| 64 |
# layout settings.
|
|
|
|
| 146 |
|
| 147 |
# return None
|
| 148 |
|
| 149 |
+
bing_search_api_key = '1fbcda1fb09547efbcdae7cb83a367b5'
|
|
|
|
| 150 |
bing_search_endpoint = 'https://api.bing.microsoft.com/v7.0/search'
|
| 151 |
|
|
|
|
| 152 |
def search(query):
|
| 153 |
# Construct a request
|
| 154 |
# mkt = 'en-EN'
|
|
|
|
| 262 |
# async def localKB_mode():
|
| 263 |
def localKB_mode(username):
|
| 264 |
### clear all the prior conversation.
|
| 265 |
+
# st.session_state.conversation = None
|
| 266 |
+
# st.session_state.chat_history = None
|
| 267 |
+
# st.session_state.messages = []
|
| 268 |
+
# message_placeholder = st.empty()
|
| 269 |
+
clear_all() ## reset the conversation.
|
| 270 |
|
| 271 |
print('now starts the local KB version of ChatGPT')
|
| 272 |
# Initialize chat history
|
|
|
|
| 345 |
|
| 346 |
async def data_mode():
|
| 347 |
print('数据分析模式启动!')
|
| 348 |
+
clear_all() ## reset the conversation.
|
| 349 |
# uploaded_file_path = './upload.csv'
|
|
|
|
| 350 |
uploaded_file_path = f'./{username}_upload.csv'
|
| 351 |
+
# uploaded_file_path = "/Users/yunshi/Downloads/360Data/Data Center/Working-On Task/演讲与培训/2023ChatGPT/Coding/code_interpreter/test_upload.csv"
|
| 352 |
+
print('file path:', uploaded_file_path)
|
| 353 |
+
|
| 354 |
+
# st.write(f"passed file path in data_mode: {uploaded_file_path}")
|
| 355 |
+
tmp1 = pd.read_csv(uploaded_file_path)
|
| 356 |
+
st.markdown('成功启动数据模式,以下是加载的文件内容')
|
| 357 |
+
st.write(tmp1[:5])
|
| 358 |
|
| 359 |
# Initialize chat history
|
| 360 |
if "messages" not in st.session_state:
|
|
|
|
| 398 |
|
| 399 |
user_request = environ_settings + "\n\n" + \
|
| 400 |
"你需要完成以下任务:\n\n" + prompt + "\n\n" \
|
| 401 |
+
f"注:文件位置在 {uploaded_file_path}"
|
| 402 |
+
user_request = str(prompt)
|
| 403 |
print('user_request: \n', user_request)
|
| 404 |
|
| 405 |
# 加载上传的文件,主要路径在上面代码中。
|
| 406 |
+
# files = [File.from_path(str(uploaded_file_path))]
|
| 407 |
+
files = [File.from_path("/Users/yunshi/Downloads/360Data/Data Center/Working-On Task/演讲与培训/2023ChatGPT/Coding/code_interpreter/rawdata/short_csv.csv")]
|
| 408 |
+
st.write(pd.DataFrame(files))
|
| 409 |
+
# print('session.__init__', session.__init__)
|
| 410 |
+
# print('session', session.__init__)
|
| 411 |
|
| 412 |
with st.status('Thinking...', expanded=True, state='running') as status:
|
| 413 |
# generate the response
|
| 414 |
+
# response = await session.generate_response(user_msg=user_request, files=files, detailed_error=True)
|
| 415 |
+
response = await session.generate_response(user_msg=user_request, files=files, detailed_error=True)
|
|
|
|
| 416 |
|
| 417 |
# output to the user
|
| 418 |
print("AI: ", response.content)
|
|
|
|
| 443 |
### authentication with a local yaml file.
|
| 444 |
import yaml
|
| 445 |
from yaml.loader import SafeLoader
|
| 446 |
+
with open('/Users/yunshi/Downloads/360Data/Data Center/Working-On Task/演讲与培训/2023ChatGPT/Coding/code_interpreter/config.yaml') as file:
|
| 447 |
config = yaml.load(file, Loader=SafeLoader)
|
| 448 |
authenticator = stauth.Authenticate(
|
| 449 |
config['credentials'],
|
|
|
|
| 616 |
import localKB_construct
|
| 617 |
# st.write(upload_file)
|
| 618 |
localKB_construct.process_file(uploaded_file, username)
|
| 619 |
+
## 在屏幕上展示当前知识库的信息,包括名字和加载日期。
|
| 620 |
save_database_info.save_database_info(f'./{username}/database_name.csv', filename, str(datetime.now().strftime("%Y-%m-%d %H:%M")))
|
| 621 |
+
st.markdown('新知识库解析成功,请务必刷新页面,然后开启对话 🔃')
|
| 622 |
# spinner = st.empty()
|
| 623 |
|
| 624 |
else:
|
| 625 |
if '.csv' in filename:
|
| 626 |
csv_file = pd.read_csv(uploaded_file)
|
| 627 |
+
csv_file.to_csv(f'./{username}_upload.csv', encoding='utf-8', index=False)
|
| 628 |
st.write(csv_file[:3]) # 这里只是显示文件,后面需要定位文件所在的绝对路径。
|
| 629 |
else:
|
| 630 |
xls_file = pd.read_excel(uploaded_file)
|
| 631 |
+
xls_file.to_csv(f'./{username}_upload.csv', index=False)
|
| 632 |
st.write(xls_file[:3])
|
| 633 |
|
| 634 |
uploaded_file_name = "File_provided"
|
| 635 |
temp_dir = tempfile.TemporaryDirectory()
|
| 636 |
# ! working.
|
| 637 |
+
# uploaded_file_path = pathlib.Path(temp_dir.name) / uploaded_file_name
|
| 638 |
# with open('./upload.csv', 'wb') as output_temporary_file:
|
| 639 |
with open(f'./{username}_upload.csv', 'wb') as output_temporary_file:
|
| 640 |
# print(f'./{name}_upload.csv')
|
|
|
|
| 693 |
if uploaded_file is not None:
|
| 694 |
# uploaded_file_path = upload_file(uploaded_file)
|
| 695 |
upload_file(uploaded_file)
|
|
|
|
|
|
|
|
|
|
| 696 |
|
| 697 |
localKB_mode(username)
|
| 698 |
# asyncio.run(localKB_mode())
|